Start with an already selected <f:selectItem

Asked

Viewed 780 times

0

I would like when the menu is presented the "Option 1" starts selected, something like an html Selected

<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>

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 your class constructor, start the list with the values you want to be 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;
    }
}
  • It did not work, it returns the items in the list normally but is not selected

0

Have you tried using the attribute noSelectionOption?

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 attribute noSelectionOption in the item you want to leave selected. Remembering that it is necessary to pass the value of true for the attribute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.