Dipendra Singh
Dipendra Singh

Reputation: 542

Primefaces datatable: Unable to retrieve selected rows and selection get lost with pagination

I am using primefaces datatable for checkbox based selection and try to implement given example. They used Model say, userModel which could implement SelectableDataModel interface. I don't want to use model so I used rowKey for this purpose.

E.g

datatable.xhtml

<p:dataTable id="table" var="item" value="#{userBean.allItems}"
       paginatorPosition="bottom" paginator="true" rows="3"
       selection="#{userBean.selectedItems}" rowKey="#{item[0]}">

       <p:column selectionMode="multiple"/>

       <p:columns value="#{userBean.itemColHeader}" columnIndexVar="colIndex" var="colName" >
       <f:facet name="header" >
           <h:outputText value="#{colName}"/>
       </f:facet>
           <h:outputText value="#{item[colIndex]}"/>
       </p:columns>
</p:dataTable>

Here,

allItems = ArrayList<ArrayList<String>>

selectedItems = ArrayList<ArrayList<String>>

selectedItems = ArrayList<String>

userBean.java

@ManagedBean(name="userBean")
@ViewScoped
public class userBean implements SelectableDataModel {

    private ArrayList<ArrayList<String>> selectedItems;

    public ArrayList<ArrayList<String>> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(ArrayList<ArrayList<String>> selectedUsers) {
        this.selectedItems = selectedItems;
    }
}  

My problem:

1) When I select mulitple rows, selectedUsers remains empty.
2) After selecting next page, previous selection get lost. 

I went through @BelusC blog and found that binding is possible solution but unable to solve my problem with his instructions. Is converter needed..? Is there any thing wrong with my approach. Thanks

Update:1

The reason behind using Arraylist of Arraylist(allItems) is only to make datatable generic. I need not to bother about the no of columns while drawing datatables. That is why I want to retrieve the selected items which should not depend on object(like: car[ ] selectedcars)

Upvotes: 0

Views: 8904

Answers (3)

caglarturkurka
caglarturkurka

Reputation: 116

Correct Answer : Primefaces datatable with checkbox selection ONLY

You can solve this problem using rowSelectionMode="add"

Upvotes: 0

phoenix7360
phoenix7360

Reputation: 2907

I had similar issues and I solved it using <p:ajax>.

First I would do as akoskm says and have a unique row key.

Then I would use an array (List<String>[]) (List is more generic than ArrayList) for selectedItems

And finally you can add:

<p:dataTable ...>
    <p:ajax event="rowSelectCheckbox" process="@this"/>
    <p:ajax event="rowUnselectCheckbox" process="@this"/>
    <p:ajax event="toggleSelect" process="@this"/>
</p:dataTable>

Just make sure you have an <h:form> around the table

That will make sure that the component pushes the data to the bean. I'm not sure if it will solve the pagination problem though, let me know.

Upvotes: 2

Akos K
Akos K

Reputation: 7133

rowKey="#{item[0]}" makes no sense to me. it will point to the same (0) object in every iteration. While in the dataTable demo it points to the current car.model.

Have you tried to change to #{item}?

Upvotes: 2

Related Questions