Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Finding selected checkbox items in a JSF dataTable

  stevi        2012-01-03 03:02:46       14,618        1    

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?

Source:http://www.stevideter.com/2008/10/09/finding-selected-checkbox-items-in-a-jsf-datatable/

EXAMPLE  JAVASERVER FACES  JSF  DATATABLE  CHECKBOX 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  1 COMMENT


rupal patoliya [Reply]@ 2017-07-19 01:10:41

 Hi,

i have configured above code in my application but didn't see the Domain manager class , what it works in this code?