How to group selectItems in selectOneMenu

I would like to use this example from showcase examples to group selectItems in selectOneMenu:

<h:outputText value="Grouping: " /> <p:selectOneMenu value="#{formBean.car}"> <f:selectItem itemLabel="Select One" itemValue="" /> <f:selectItems value="#{formBean.cars}" /> </p:selectOneMenu> 

My problem is that the bean implementation is missing. Now I do not know how to implement selectItems grouping inside the getCars () method. And I can not find another example.

+8
jsf-2 selectonemenu primefaces
source share
2 answers

The source code for showcase #{formBean} is available here . Here's an excerpt of relevance:

 private List<SelectItem> cars; public FormBean() { SelectItemGroup g1 = new SelectItemGroup("German Cars"); g1.setSelectItems(new SelectItem[] {new SelectItem("BMW", "BMW"), new SelectItem("Mercedes", "Mercedes"), new SelectItem("Volkswagen", "Volkswagen")}); SelectItemGroup g2 = new SelectItemGroup("American Cars"); g2.setSelectItems(new SelectItem[] {new SelectItem("Chrysler", "Chrysler"), new SelectItem("GM", "GM"), new SelectItem("Ford", "Ford")}); cars = new ArrayList<SelectItem>(); cars.add(g1); cars.add(g2); } 

So your missing SelectItemGroup key.

See also:

  • Our selectOneMenu wiki page - "Dynamic list with groups"
+16
source share

In this example, getCars() returns a list of javax.faces.model.SelectItem objects. There is one subclass of this class called SelectItemGroup that represents the group in selectOneMenu . The value field of this object is ignored and only the label is used. Therefore, in your list, you can mix SelectItem and SelectItemGroup objects to arrange the list in groups. Note that the SelectItem objects that are part of the group are represented as an array in the SelectItemGroup object. You can set this array through the constructor or setter ( setSelectItems() ).

+1
source share

All Articles