0
votes

I have a DataTable that presnt a list of product :

     <p:dataTable var="product" value="#{myModel.products}"
            scrollable="true" id="productsTableId" liveResize="true"
            resizableColumns="true">

            <f:facet name="header">  
                List of products
            </f:facet>
            <p:column headerText="Id" sortBy="id">
                <h:outputText value="#{product.id}" > 
                </h:outputText>
            </p:column>

            <p:column headerText="Name" sortBy="name">
                <h:outputText value="#{product.name}" title="#{product.name}" />
            </p:column>

            <p:column headerText="Remove product">
                <p:commandButton icon="ui-icon-close" title="delete product"
                    value="Disconnect"
                    action="#{controller.delete(product)}"
                    update="productTableId">
                    <p:confirm header="Confirmation"
                        message="Are you sure you want to delete this product"
                        icon="ui-icon-alert" />
                </p:commandButton>
            </p:column>
</p:dataTable>

but the problem comes when i try to sort the datatable, the rows change their diplay order but the buttons functionalities keep the initial order ,i.e. if i have for example two products :

Id        Name      Remove product

1         B         Delete B
2         A         Delete A

after sorting by name

Id        Name      Remove product

2         A       Delete A
1         B       Delete B

If i click 'Delete A' then the product B is removed , is this a bug in PrimeFaces or i'm missing something .

i'm using PrimeFaces 4.0 ,JSF Mojarra 2.1.20 on jetty-9.1.2.

1
I think you need to override hashCode() and equals() on the Product entity - Jaqen H'ghar
it's already done , but no way. - Mifmif

1 Answers

0
votes

I've found the source of problem, the bean myModel was set to a request scope, i've change it to view scope.

Explanation :

When we load the page for the first time one managed bean myModel is created and used, when we sort data , a second managed bean is created , when we click on delete button we ask server to delete the product that has to index order of the pressed button , the server try first to create instance of the managed bean myModel , then it look for the product with the index order posted and remove it. this senario cause the issue. to avoid this we use the view scope to keep using the sapce instance of myModel created when the page is loaded. with the view scope , only one instance will be created and used in the same senario specified here.