Bertie
Bertie

Reputation: 17697

How to do multiple selection in jsf or primefaces dataTable?

I want to try out building a simple grid that has a delete column, consisting of checkboxes, just like the usual one in email.

User can then tick the checkboxes, and press delete, and i can get all the checked records, and delete them one by one.

Im trying to find a way to achieve this, but so far im still in doubt.

These are what i have in mind, each with it's own implementation question, haha :

  1. How to get the checked row indexes ? Using actionlistener for each toggle on each checkbox ? (but how do i pass the clicked index to the actionlistener ?)
  2. Or is there a way where i can get all the grid model, and loop the data to find out which one is checked, just like swing ? (but how do i get the grid model in the jsf bean ?)
  3. Or perhaps i should bind them to a simple list that contains only the checkbox column data ? (but how do i bind each checkbox to the list using indexes ?)

Im currently using primefaces, but i think the JSF solution can also be applied to primefaces datatable.

Please share your thoughts on this !

Thank you !

Upvotes: 1

Views: 3574

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

Isn't this example from Primefaces showcase exactly what you are looking for?

It looks that it is simply adding a column to the p:dataTable this way:

<p:dataTable var="item" value="#{yourBean.allElements}"  
             selection="#{yourBean.selectedElements}">
    <p:column selectionMode="multiple" />
    ... other columns
</p:dataTable>

Upvotes: 1

Related Questions