snabel
snabel

Reputation: 343

h:dataTable, HtmlDataTable keep reading the last row

I've tried every solution I found with Google. Nothing seems to work. the issue is this, I want to update a row in the table. but each time I click on update button, I receive the last row data. I tried using map, tried using HtmlDataTable, still can't solve the issue.

I'm using:

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

My bean:

@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();
    }

    //...
}    

My JSF page:

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

Upvotes: 1

Views: 169

Answers (1)

Ced
Ced

Reputation: 17327

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

The same dialog is reused each time you loop through the datatable. So of course at the end the values inside the dialogs will be bound to the last item.

The solution is to have dynamic widgetVar.

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

and of course change the javascript accordingly :

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

Upvotes: 1

Related Questions