Start with a f: selectItem already selected

0

I would like that when the menu is displayed "Option 1" gets selected, something like an html

<p:selectManyMenu id="basic" value="#{selectManyView.selectedOptions}">
    <f:selectItem itemLabel="Option 1" itemValue="1" />
    <f:selectItem itemLabel="Option 2" itemValue="2" />
    <f:selectItem itemLabel="Option 3" itemValue="3" />
</p:selectManyMenu>
    
asked by anonymous 14.08.2017 / 22:08

2 answers

2

In your Java code you probably have something like this:

public class SelectManyView{
    private List<String> selectedOptions;

    public List<String> getSelectedOptions() {
        return selectedOptions;
    }
}

In the constructor of your class, start the list with the values you want already selected. Something like this:

public class SelectManyView{
    private List<String> selectedOptions;

    public SelectManyView(){
       this.selectedOptions = new ArrayList<String>();
       this.selectedOptions.add("1"); // é o campo itemValue do Option 1
    }

    public List<String> getSelectedOptions() {
        return selectedOptions;
    }
}
    
14.08.2017 / 22:22
0

Have you tried using the noSelectionOption attribute?

Example:

<p:selectManyMenu id="basic" value="#{selectManyView.selectedOptions}">
    <f:selectItem itemLabel="Option 1" itemValue="1" noSelectionOption ="true"/>
    <f:selectItem itemLabel="Option 2" itemValue="2" />
    <f:selectItem itemLabel="Option 3" itemValue="3" />
</p:selectManyMenu>

You add the noSelectionOption attribute to the item you already want to leave selected. Remember that you need to pass the true value to the attribute.

    
19.08.2017 / 00:39