Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jsf 2 - How to group selectItems in selectOneMenu

I would like to use the example from the primefaces showcase 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 there is no implementation of the bean. Now I don't know, how to implement the grouping of the selectItems inside the method getCars(). And I can't find any other example.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The source code of the showcase's #{formBean} is available here. Here's an extract 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);
}

Thus, your missing key is SelectItemGroup.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...