简体   繁体   中英

Preselect items in h:selectManyCheckbox

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 (ie it is ticked already when the site is called).

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

}

Add to your backing list or array objects that return true when comparing equals to the value of the SelectItems you want to preselect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM