2013-10-04 38 views
0

在我的JSF應用程序中,我使用了豐富的:dataTable的如下:獲取豐富的rowIndex:dataTable的行

<rich:dataTable id="itemTable" value="#{backingBean.itemsList}" var="i" > 
    <rich:column> <f:facet name="header">ItemValue</f:facet> 
     <h:inputText id="myId" value="#{i.value}" style="width: 30px" /> 
    </rich:column> </rich:dataTable> 

<h:commandButton id="saveB" value="Save" action="#{backingBean.doSave()}" /> 

DoSave就會的豆代碼:

public String doSave() { 
    Iterator<Item> = itemsList.iterator(); 
    while(iter.hasNext()) { 
     //do something 
    } 
} 

在DoSave就會() - 方法,我需要知道當前Item的行索引,有沒有辦法實現這一點?

回答

0

雖然Richfaces擴展數據表支持selection management,但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元素並使用它的行數據。

可能的辦法二是元素justgive作爲參數和函數調用:

<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> 

,並在bean

public String doSave(Item item) { 
    // do stuff 
} 

這是不是乾淨,但應與EL工作,太。希望它有助於...

+0

@ Mr.Radar這個解決方案是否能幫助你? –