Your best bet is to bind the h:selectBooleanCheckbox
value with a Map<RowId, Boolean>
property where RowId
represents the type of the row identifier. Let's take an example that you've a Item
object whose identifier property id
is a Long
:
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:selectBooleanCheckbox value="#{bean.checked[item.id]}" />
</h:column>
...
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />
which is to be used in combination with:
public class Item {
private Long id;
// ...
}
and
public class Bean {
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
private List<Item> items;
public void submit() {
for (Item item : items) {
if (checked.get(item.getId())) {
// Item is checked. Do your thing here.
}
}
}
// ...
}
You see, the map is automatically filled with the id
of all table items as key and the checkbox value is automatically set as map value associated with the item id
as key.