How to group selectItems in selectOneMenu

前端 未结 2 522
夕颜
夕颜 2020-12-20 15:47

I would like to use the example from the primefaces showcase to group selectItems in selectOneMenu:

  
    

        
相关标签:
2条回答
  • 2020-12-20 16:20

    In this example getCars() returns a list of javax.faces.model.SelectItem objects. There is one subclass of this class, named SelectItemGroup which represent a grup in selectOneMenu. Value field of this object is ignored and just label is used. So in your list you can mix SelectItem and SelectItemGroup objects to organize your list in groups. Note, that SelectItem objects which are part of group are present as array in SelectItemGroup object. You can set that array through constructor or setter (setSelectItems()).

    0 讨论(0)
  • 2020-12-20 16:34

    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:

    • Our selectOneMenu wiki page - "Dynamic list with groups"
    0 讨论(0)
提交回复
热议问题