使用托管bean填充组合框的selectItem(标签,值)

JiK*_*Kra 4 xpages

我的页面中有一个组合,我希望在配置中填充一些关键字.我想使用托管bean来完成它.

假设我有一个名为Config的bean,其中有一个List categories字段...

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.
}
Run Code Online (Sandbox Code Playgroud)

当我将这个字段用于我的组合时,它运作良好......

<xp:comboBox>
    <xp:selectItems>
        <xp:this.value><![CDATA[#{config.categories}]]></xp:this.value>
    </xp:selectItems>
</xp:comboBox>
Run Code Online (Sandbox Code Playgroud)

但是,它只是一个标签列表.我也需要价值观.如何用两个字符串填充我的组合的selectItems - 标签和值?

编辑:

我尝试使用标签和值字段创建一个对象组合,并在我的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>
Run Code Online (Sandbox Code Playgroud)

还是行不通... :-(

Mar*_*ink 10

而不是返回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's a label");
    option.setValue("Here's a value");

    options.add(option);

    return options;
}
Run Code Online (Sandbox Code Playgroud)

使用这种方法的优点(除了不必使用非概念性的东西:-)之外,您还可以将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'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;
}
Run Code Online (Sandbox Code Playgroud)