It's not a secret anymore!*
The moose likes JSF and the fly likes Creating a table with a checkbox column dynamically in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Creating a table with a checkbox column dynamically in JSF" Watch "Creating a table with a checkbox column dynamically in JSF" New topic
Author

Creating a table with a checkbox column dynamically in JSF

Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Dear Sirs and Madams,
I am very new to JSF and would appreciate any help on the current issue am having. I have data being displayed on a browser via a JSF table, and the table is bound to a userVOArray attribute on the managed bean. All works well and I have a able of users displayed (first name, last name, user type etc). As the array is populated by data pulled from a database, its size changes depending on users being added or deleted. I wish to add a column of checboxes to the table allowing me to delete a user whose corresponding checkbox has been checked. How would I go about this please?

Thanking you profusely in advance,
Nick


You are what you know
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

JSF is based on the Model/View/Controller architecture. So the view (datatable) can only display what's in the model.

Your problem is that the original rows of the datatable don't possess a "delete me" attribute.

You get around this by building a model that does. In other works, instead of binding the datamodel directly to the view, construct a fa├žade class that includes the original attributes of a datamodel row plus a boolean item that indicates deletion and build your view model using instances of this class. You can then reference this view model when looking for items to delete.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can find here two examples: http://balusc.blogspot.com/2006/06/using-datatables.html#SelectMultipleRows
Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Thanking you both for your prompt responses, I will try your suggestions and get back to you with any further queries.

Thanks again,
Nick
Phoenix Kilimba
Ranch Hand

Joined: Oct 10, 2006
Posts: 64
Hello again, am pleased to say the suggestions made came in extremely useful and I have sorted out the dynamic checkbox issue, thanks again! However I now have a similar issue: I would like to have a new column next to the checkbox column, also dynamic with a button so that when that button is clicked on, that row is selected to be edited (However if there is a better way of fulfilling such a use case please let me know). Am using a Table as opposed to a datatable from the palette in netbeans, not sure if that makes a difference but thought I would add the info.

Thanking you profusely in advance for any input,
Nick
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Make use of the rendered attribute.



Or something simliar that.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Google for "JSF for Nonbelievers" by Rick Hightower - it's a 4-part series of articles in the IBM Developerworks collection. He presents an excellent illustration of this kind of thing.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating a table with a checkbox column dynamically in JSF