0
votes

In my JSF page, I have a datatable like so:

<p:datatable var="item" value="#{bean.dataModel}" rowkey="#{item.pkey}" selectionMode="multiple" selection="#{bean.selectedItems}">

In my managedbean, I have declared selectedItems as a list like so

private List<SelectedItemsClass> selectedItems;
public void doInit() {
   selectedItems = new ArrayList<SelectedItemsClass>();
}

I have also declared the getter and setter for selectedItems. Now, when I try to debug one of my actions that checks for the selectedItems, the value of selectedItems size is always 0. Am I missing something in my code? I've tried to search the web and the answer is always to use selection="" in datatable. Please advise!

UPDATE: The cause of the problem had nothing to do with the data table and the datamodel itself. As it turns out, the problem was the data type I used for my rowkey in my datamodel class. I've set string value for the rowkey but my database is returns the key as type long. By changing the data type of my rowkey, the selectedItems are now passed successfully to my managedbean and I am able to process the selected Items from there. thank you very much for your time.

1
1) What scope is your bean? 2) Do you have a preRenderView event listener on your xhtml? 3)When does doInit() get called, is it a @PostConstruct method? I've never tried a dataModel with multiple selection, so I want to replicate this scenario in order to give you an answer!rion18
1) Not sure if it's the correct answer but I used the annotation ViewScoped 2) No, I did not use preRenderView event listener 3) doInit() only gets called when the page is loaded and yes, it's a @PostConstruct method.user1597438

1 Answers

2
votes

You need add ajax events in jsf datatable configuration, like this:

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