【问题标题】:Populating selectItems of the combobox (label, value) using a managed bean使用托管 bean 填充组合框的选择项(标签、值)
【发布时间】:2014-02-03 16:35:30
【问题描述】:

我的页面中有一个组合,我想用配置中的一些关键字填充该组合。我想使用托管 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.
}

当我将此字段用于我的组合时,它运行良好...

<xp:comboBox>
    <xp:selectItems>
        <xp:this.value><![CDATA[#{config.categories}]]></xp:this.value>
    </xp:selectItems>
</xp:comboBox>

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

编辑:

我尝试创建一个带有标签和值字段的对象 Combo,并在我的组合框中使用重复。

<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>

还是不行... :-(

【问题讨论】:

  • 使用管道 (|) 分隔符...例如someList.add("I'm a label|I'm a value");
  • 好的,管道可以工作,但这是一个愚蠢的解决方案。如果该关键字字符串包含管道字符怎么办?难道没有使用简单对象和某种转换器的另一种解决方案吗?
  • 我不会说这很傻。几十年来,通过管道分隔标签和值在Notes中很常见,例如用于对话框列表选择。您仍然可以用另一个字符替换数据中的管道。但老实说,哪些数据本质上包含管道?
  • 我知道这是 Notes 开发的传统,老实说,我希望这种非概念性的东西在 JSF 开发中消失​​。
  • 现在我明白了,Mark 的代码当然比使用管道要好得多,尤其是 SelectItemGroup 是一个不错的附加:)

标签: xpages


【解决方案1】:

您的函数应该返回List&lt;javax.faces.model.SelectItem&gt;,而不是返回List&lt;String&gt;。这是一个示例:

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;
}

使用这种方法的优点(除了不必使用那些非概念性的东西:-) 是您还可以 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;
}

【讨论】:

  • 谢谢。这正是我所需要的。 :-)
  • 马克,你能举一个简单的例子来说明如何使用它吗?也许是因为现在是星期五下午,我一小时后离开,但我无法找出调用它的正确方法。谢谢。
  • 把上面的代码放在你数据库的一个Java类中。在我的示例中,类是静态的,因此您不需要先创建 Java bean。您可以使用问题中的语法在xp:comboBox 中使用它:&lt;xp:comboBox id="comboBox1"&gt;&lt;xp:selectItems&gt;&lt;xp:this.value&gt;&lt;![CDATA[#{javascript:eu.linqed.YourClass.getComboboxOptions()}]]&gt;&lt;/xp:this.value&gt;&lt;/xp:selectItems&gt;&lt;/xp:comboBox&gt;
  • 马克,谢谢!!我在“数据”中拥有它,而不是在“价值”中。我知道这是愚蠢的。恭喜你的国家队参加了世界杯。
【解决方案2】:

您可以使用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;
    }
}

【讨论】:

    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多