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

时间:2015-09-17 12:19:12

标签: spring jsf datatable

我已尝试过我在Google上找到的所有解决方案。似乎没什么用。 问题是这样,我想更新表中的一行。但每次我点击更新按钮,我都会收到最后一行数据。我尝试使用地图,尝试使用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>

1 个答案:

答案 0 :(得分:1)

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

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

解决方案是拥有动态的widgetVar。

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

当然会相应地更改javascript:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>