Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the selected values from selectManyListbox / selectManyMenu / selectManyCheckbox?

How do I collect all selected values from UISelectMany components such as h:selectManyListbox, h:selectManyMenu, h:selectManyCheckbox, p:selectManyListbox, p:selectManyMenu, p:selectManyCheckbox, etc in backing bean?

If someone can help with an example, that would really help.

like image 456
Abhishek Dhote Avatar asked Dec 05 '10 10:12

Abhishek Dhote


1 Answers

As with every other input component, just bind its value attribute with a managed bean property. It can map to a List or an array of the same value type as you've used in f:selectItem(s). If the value type is not one of the standard EL types (String, Number or Boolean), then you have to supply a Converter as well.

Here's an example with a value type of String:

<h:selectManyListbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>
<h:commandButton value="Submit" action="#{bean.submit}" />

with

public class Bean {

    private Map<String, String> availableItems; // +getter (no setter necessary)
    private List<String> selectedItems; // +getter +setter

    @PostConstruct
    public void init() {
        availableItems = new LinkedHashMap<String, String>();
        availableItems.put("Foo label", "foo");
        availableItems.put("Bar label", "bar");
        availableItems.put("Baz label", "baz");
    }

    public void submit() {
        System.out.println(selectedItems); // It's already set at that point.
    }

    // ...
}

See also:

  • Our selectOneMenu wiki page
  • How to populate options of h:selectOneMenu from database?
like image 156
BalusC Avatar answered Nov 15 '22 11:11

BalusC