tags:

views:

72

answers:

2

I have a simply facelet which display a list of products in tabular format. In the last column of each row, there is a checkbox used to mark the product for deletion. Until now i have to put a selectBooleanCheckBox on each row and have a "mark for deletion" property in the Product entity but i think it's ugly because i have some presentation stuff in my model bean.

Is there anyway to have a h:selectManyCheckBox which has its f:selectItem distribute on each row of the dataTable ?

Thank you

+2  A: 

You can, using MyFaces Tomahawk's <t:selectManyCheckbox> with layout="spread"

Bozho
+2  A: 

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:

BalusC