0

在我的 JSF 应用程序中,我使用如下 Rich: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的bean代码:

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

在 doSave()-Method 中,我需要知道当前 Item 的行索引,有没有办法做到这一点?

4

1 回答 1

0

虽然 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 一起使用。希望能帮助到你...

于 2013-11-27T17:19:05.550 回答