Filling selectItems combobox (label, value) with a managed bean

I have a combo on my page that I want to fill with some keywords from the configuration. I want to use a managed bean to execute it.

Say I have a bean called Config, where there is a Category field List ...

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 this is just a list of shortcuts. I also need values. How to fill selectItems with my combo with two lines - label and value?

EDIT:

I tried to create a Combo object with labels and value fields and use repeat within 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...: - (

+4
source share
2 answers

List<String> List<javax.faces.model.SelectItem>. :

public static List<SelectItem> getComboboxOptions() {

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

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

    options.add(option);

    return options;
}

( , :-), SelectItemGroup :

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 a value", "here a label");
    options[1] = new SelectItem("here a value", "here a label");

    group.setSelectItems(options);

    groupedOptions.add(group);

    return groupedOptions;
}
+8

SelectItems. (. http://docs.oracle.com/javaee/6/api/javax/faces/model/SelectItem.html)

, .

import javax.faces.model.SelectItem;

public List<SelectItem> getCategories() {
    try {
        ArrayList<SelectItem> ret = new ArrayList<SelectItem>();
        ret.add(new SelectItem("my value", "my label"));
        return ret;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
+2

Source: https://habr.com/ru/post/1525086/


All Articles