虽然 Richfaces 扩展数据表支持选择管理,但 Richfaces 数据表不支持。
我发现从列表中检索以某种方式选择的项目的最简单方法是为每一行添加一个图标。为此,将命令按钮放入数据表本身:
<rich:dataTable id="itemTable" value="#{backingBean.itemsList}" var="i" >
<rich:column>
<h:inputText id="myId" value="#{i.value}" />
<h:commandButton id="saveB" action="#{backingBean.doSave}" />
</rich:column>
</rich:dataTable>
在 bean 代码中,提供方法doSave
但带有附加参数“ActionEvent”
public String doSave(ActionEvent ev) {
Item selectedItem = null;
UIDataTable objHtmlDataTable = retrieveDataTable((UIComponent)ev.getSource());
if (objHtmlDataTable != null) {
selectedItem = (Item) objHtmlDataTable.getRowData();
}
}
private static UIDataTable retrieveDataTable(UIComponent component) {
if (component instanceof UIDataTable) {return (UIDataTable) component;}
if (component.getParent() == null) {return null;}
return retrieveDataTable(component.getParent());
}
您会看到,ActionEvent ev
为您提供了源元素(UIComponent)ev.getSource()
。遍历它,直到您点击该UIDataTable
元素并使用它的行数据。
可能的方法二是使用函数调用将元素作为参数:
<rich:dataTable id="itemTable" value="#{backingBean.itemsList}" var="i" >
<rich:column>
<h:inputText id="myId" value="#{i.value}" />
<h:commandButton id="saveB" action="#{backingBean.doSave(i)}" />
</rich:column>
</rich:dataTable>
在豆子里
public String doSave(Item item) {
// do stuff
}
这不是很干净,但也应该与 EL 一起使用。希望能帮助到你...