The t:selectManyCheckbox layout="spread"
is an excellent suggestion.
As an alternative, you can also just bind the h:selectBooleanCheckbox
component to a Map<Long, Boolean>
property where Long
represents the entity ID (or whatever identifier which you can use to identify the row) and Boolean
represents the checked state.
E.g.
public class Bean {
private List<Entity> entities;
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
public void submit() {
for (Entity entity : entities) {
if (checked.get(entity.getId())) {
// Entity is checked. Do your thing here.
}
}
}
// ...
}
with
<h:dataTable value="#{bean.entities}" var="entity">
<h:column>
<h:selectBooleanCheckbox value="#{bean.checked[entity.id]}" />
</h:column>
...
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />
The Map<Long, Boolean>
will be automagically filled with the ID of all entities as map keys and the checkbox value is set as map value associated with the entity ID as key.
See also: