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)
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>[]) (Listis more generic thanArrayList) forselectedItemsAnd finally you can add:
Just make sure you have an
<h:form>around the tableThat 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.