Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preselect items in h:selectManyCheckbox

Tags:

jsf

jsf-2

How do I preselect the elements in a h:selectManyCheckbox component? I've searched through the properties of the f:selectItem tag but not yet have found how to pre-select this item (i.e. it is ticked already when the site is called).

like image 398
Simon Voggeneder Avatar asked Jul 11 '11 12:07

Simon Voggeneder


1 Answers

The value attribute of h:selectManyCheckbox can accept an array of string from the managed bean. You can directly set the default values to this array when the managed bean is initialized.

For example , in the view :

<h:selectManyCheckbox value="#{MBean.choice}">
    <f:selectItem itemValue="A" itemLabel="Choice A" />
    <f:selectItem itemValue="B" itemLabel="Choice B" />
    <f:selectItem itemValue="C" itemLabel="Choice C"/>
    <f:selectItem itemValue="D" itemLabel="Choice D" />
</h:selectManyCheckbox>

Then in the MBean :

public class MBean{

    //Preselect the "Choice A" and "Choice C" 
    private String[] choice= {"A","C"};

    //Getter and setter of choice

}
like image 102
Ken Chan Avatar answered Sep 28 '22 00:09

Ken Chan