Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make javax.faces.model.SelectItem selected

Tags:

jsf

I'm creating a List of javax.faces.model.SelectItem (in a bean) for use with a h:selectManyCheckbox but I cannot figure out how to make a SelectItem selected.

How to do this? Must be possible, right?...

    public List<SelectItem> getPlayerList(String teamName) {
    List<SelectItem> list = new ArrayList<SelectItem>();

    TeamPage team = (TeamPage) pm.findByName(teamName);

    List<PlayerPage> players = pm.findAllPlayerPages();

    for (PlayerPage player : players) {
        boolean isMember = false;
        if (team.getPlayerPages().contains(player)) {
            isMember = true;
        }
        SelectItem item;
        if (isMember) {
            // TODO: Make SelectItem selected???
            item = null;
        } else {
            item = new SelectItem(player.getId(), createListItemLabel(player), "", false, false);
        }
        list.add(item);         
    }
    return list;
}
like image 480
mafro Avatar asked Mar 06 '09 15:03

mafro


1 Answers

Assume we have this JSF code:

<h:selectManyCheckbox value="#{bean.selectedValues}">
    <f:selectItems value="#{bean.playerList}"/>
</h:selectManyCheckbox>

then the selected values (i.e. the checked checkboxes) are stored in the bean.selectedValues property.

Thus, in your Java code, you must handle the selectValues by putting the correct ID in the selectedValues property.

like image 76
Romain Linsolas Avatar answered Sep 29 '22 08:09

Romain Linsolas