Populating selectItems of the combobox (label, value) using a managed bean

xpages

Solution

Instead of returning a `List<String>` your function should return a `List<javax.faces.model.SelectItem>`. Here's a sample:

public static List<SelectItem> getComboboxOptions() {

    List<SelectItem> options = new ArrayList<SelectItem>();

    SelectItem option = new SelectItem();
    option.setLabel("Here's a label");
    option.setValue("Here's a value");

    options.add(option);

    return options;
}

Advantage of using this method (besides not having to use that nonconceptual stuff :-) is that you can also the `SelectItemGroup` class to group the options:

public static List<SelectItem> getGroupedComboboxOptions() {

    List<SelectItem> groupedOptions = new ArrayList<SelectItem>();

    SelectItemGroup group = new SelectItemGroup("A group of options");

    SelectItem[] options = new SelectItem[2];

    options[0] = new SelectItem("here's a value", "here's a label");
    options[1] = new SelectItem("here's a value", "here's a label");

    group.setSelectItems(options);

    groupedOptions.add(group);

    return groupedOptions;
}

Problem

I have a combo in my page which I want to have populated with some keywords from configuration. I want to use a managed bean to accomplish it. Let's say that I have a bean called Config, where there is a List categories field. .. ``` public class Configuration implements Serializable { private static final long serialVersionUID = 1L; private List<String> categories; public List<String> getCategories() { if (categories == null) categories = getCats(); return categories; } //... etc. } ``` When I use this field for my combo, it works well... ``` <xp:comboBox> <xp:selectItems> <xp:this.value><![CDATA[#{config.categories}]]></xp:this.value> </xp:selectItems> </xp:comboBox> ``` But, it's only a list of labels. I need values, too. How do I populate selectItems of my combo with TWO strings - a label and a value? EDIT: I tried to create an object Combo with label and value fields and use a repeat inside my comboBox. ``` <xp:comboBox> <xp:repeat id="repeat1" value="#{config.combo}" var="c" rows="30"> <xp:selectItem itemLabel="#{c.label}" itemValue="#{c.value}" /> </xp:repeat> </xp:comboBox> ``` Still not working... :-(

Original source