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
行データを使用するまで、それをトラバースします。
可能な方法 2 は、関数呼び出しで要素をパラメーターとして指定することです。
<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 でも動作するはずです。それが役に立てば幸い...