h:dataTable, HtmlDataTable 继续读取最后一行

h:dataTable, HtmlDataTable keep reading the last row

我已经尝试了使用 Google 找到的所有解决方案。似乎没有任何效果。 问题是这样的,我想更新 table 中的一行。但每次我点击更新按钮时,我都会收到最后一行数据。我试过用地图,试过用HtmlDataTable,还是不能解决问题。

我正在使用:

<!-- JSF Dependencies -->
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.1.14</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.1.14</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
</dependency>

我的豆子:

@ManagedBean(name = "identifierManager")
@RequestScoped
public class IdentifiersManager {

    private HtmlDataTable dataTable;

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public void updateNormalIdentifier(NormalIdentifier normalIdentifier) {
        logger.info(String.format(LOG_USER_ABOUT_TO_UPDATE_IDENTIFIER, loginController.getUserName(), normalIdentifier));
        // Get selected MyData item to be edited.
        NormalIdentifier normalIdentifier_ = (NormalIdentifier) dataTable.getRowData();
        int index = dataTable.getRowIndex();
        int count = dataTable.getRowCount();
    }

    //...
}    

我的 JSF 页面:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <h:outputStylesheet library="css" name="table-style.css" />
    </h:head>

    <h:form id="ni">
        <h:dataTable binding="#{identifierManager.dataTable}"
                     value="#{normalIdentifiers}" var="i" styleClass="order-table"
                     headerClass="order-table-header"
                     rowClasses="order-table-odd-row,order-table-even-row"
                     columnClasses="order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column,order-column">

            <h:column><f:facet name="header">User Id</f:facet>#{i.userId}</h:column>
            <h:column><f:facet name="header">Identifier</f:facet>#{i.identifier}</h:column>

            <h:column>
                <f:facet name="header"></f:facet>
                <p:commandButton id="niUpdate"
                                 value="Update"
                                 onclick="niConfirmationUpdate.show()" type="button" />
                <p:confirmDialog id="niUpdateConfirmDialog"
                                 message="Are you sure you want to Update this Identifier?"
                                 showEffect="drop" hideEffect="drop" header="Update Identifier"
                                 severity="alert" widgetVar="niConfirmationUpdate" modal="true">
                    <h:commandButton value="Yes"
                                     oncomplete="niConfirmationUpdate.hide()"
                                     actionListener="#{identifierManager.updateNormalIdentifier(i)}"
                                     style="width: 80px;height: 24px;margin: 10px 5px;"
                                     immediate="true" />
                    <h:commandButton value="No" onclick="niConfirmationUpdate.hide()"
                                     type="button" style="width: 80px;height: 24px" />
                </p:confirmDialog>
            </h:column>

            <h:column>
                <f:facet name="header"></f:facet>
                <p:commandButton id="niDelete" value="Delete"
                                 onclick="niConfirmationDelete.show()" type="button" />
                <p:confirmDialog id="niDeleteConfirmDialog"
                                 message="Are you sure you want to delete this Identifier?"
                                 showEffect="drop" hideEffect="drop" header="Delete"
                                 severity="alert" widgetVar="niConfirmationDelete" modal="true">
                    <h:commandButton value="Yes"
                                     oncomplete="niConfirmationDelete.hide()"
                                     actionListener="#{identifierManager.removeNormalIdentifier(i)}"
                                     style="width: 80px;height: 24px;margin: 10px 5px;" />
                    <h:commandButton value="No" onclick="niConfirmationDelete.hide()"
                                     type="button" style="width: 80px;height: 24px" />
                </p:confirmDialog>
            </h:column>
        </h:dataTable>
    </h:form>
</ui:composition>
            <p:commandButton id="niDelete" value="Delete"
                             onclick="niConfirmationDelete.show()" type="button" />
            <p:confirmDialog id="niDeleteConfirmDialog"
                             message="Are you sure you want to delete this Identifier?"
                             showEffect="drop" hideEffect="drop" header="Delete"
                             severity="alert" widgetVar="niConfirmationDelete" modal="true">

每次循环访问数据表时都会重复使用同一个对话框。所以当然最后对话框内的值将绑定到最后一项。

解决方案是使用动态 widgetVar。

widgetVar="myWidget-#{item.id}"

当然还要相应地更改 javascript :

 PF('myWidget-#{item.id}').show;