Finding selected checkbox items in a JSF dataTable
This is one of those problems that I couldn’t find a complete example for when I needed it, so hopefully this will save somebody else the extra time it took me to piece it together.
We frequently need to have data tables in our UI, and allow the user to select a subset of those items for action. In JavaServer Faces, this means having a DataTable, each row having its own checkbox. But when the action is triggered, how to we find which items the user has selected.
The first step is to add a boolean property to your objects that can represent the selection. If you have a lot objects in your domain that will need this property, you may want to consider adding this to an interface or parent bean, otherwise, you can add it directly to your domain object. As a caveat, I don’t like adding properties to my domain objects that are for UI use only, but in this case, I’m keeping this as simple as possible. Sometimes pragmatism wins.
package com.stevideter.domain; public class SelectableItem { private Integer id; private String name; private boolean selected; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }
In your view, you’ll use the dataTable to display the items, including the checkbox:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view><html><body>
<h:form>
<h:dataTable id="itemsTable"
value="#{SelectionBean.selectedItems}" var="item" >
<f:facet name="header">
<h:outputText value="Items" />
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:selectBooleanCheckbox value="#{item.selected}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="name" />
</f:facet>
<h:outputText value="#{item.name}"></h:outputText>
</h:column>
</h:dataTable>
<h:commandButton title="selectItems"
value="Select Items"
actionListener="#{SelectionBean.submitSelections}" />
</h:form>
</body></html></f:view>Now when you click the command button to trigger the submitSelections event, the boolean values for selected items will be set, and you can iterate through your list to find the selected items and act on them as needed.
package com.stevideter.BackingBean; import java.util.List; import javax.faces.event.ActionEvent; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import com.stevideter.domain.SelectableItem; import com.stevideter.manager.DomainManager; public class SelectionBean { private DataModel selectableItems; private DomainManager manager; public void populateSelectableItems(ActionEvent event) { selectableItems = new ListDataModel(); selectableItems.add(manager.getSelectableItems()); } public void submitSelections(ActionEvent event) { List<SelectableItem> items = (List<SelectableItem>)selectableItems.getWrappedData(); List<SelectableItem> selectedItems = new ArrayList<SelectableItem>(); for (SelectableItem item : items) { if (item.isSelected()) { selectedItems.add(item); } } // do what you need to do with selected items } public DataModel getSelectableItems() { return selectableItems; } public void setSelectableItems(DataModel selectableItems) { this.selectableItems = selectableItems; } }
As you can see, this is a very simple solution. Have you seen any other approaches that work?
Related posts:
- Annotation Mapping for Ordered Lists in Hibernate Here’s another problem that has a simple solution that took...
- Why Are All My Collection Elements the Same as the Last One I Added? I’ve seen several programmers struggle with a similar question. They...
Related posts brought to you by Yet Another Related Posts Plugin.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
8 Responses to “Finding selected checkbox items in a JSF dataTable”.
Hi,
verry nice and simple solution.
Thanks a lot!
Andy
Hi :
What is the purpose of DomainManager class?
Would you please paste the source code of that class?
Thanks
thank you
yummy,
The DomainManager represents the business layer of the application in this example.
That’s where you’d put the logic for getting your items from the database, for example, to abstract it from the UI/view layer.
I didn’t include it in the post since it would be specific to your needs. I should have explicitly said that in the post, so thanks for pointing out that I didn’t.
Yep it was good….keep write articles about JSF
will be helpful for freshers.
Hai Stevi Deter,
Im new to JSF im developing an applicatiopn in that I’ve a problem:
Actually i want to edit the user registered values dynamically i used datatable to retrive the values from database and i used radio button to select the perticular record, but it was not working properly so i used java script for selecting the radio-button . now problem is if table having only one row then only it took the values dynamically to the next page to edit if table contains more records then it will not working properly please help me out ..
Hope u will reply me..
Very Nice Article !!!
Ujjwal B Soni
Simple and Useful,
Thanks, very nice article.
Leave a comment.