简体   繁体   English

下拉列表中的JSF树

[英]JSF Tree in a dropdown

I want to display a tree like structure in my dropdown in JSF. 我想在JSF的下拉列表中显示一个树状结构。 Basically the select items are in a hierarchy and I would like that to be evident in the dropdown. 基本上,选择项目在层次结构中,我希望在下拉列表中显而易见。

Is it possible ? 可能吗 ?

So, you basically want a HTML <optgroup> ? 那么,你基本上想要一个HTML <optgroup> Use SelectItemGroup . 使用SelectItemGroup

JSF bean (I assume JSF 1.x): JSF bean(我假设JSF 1.x):

private String option; // +getter +setter
private List<SelectItem> options; // +getter

public Bean() {
    options = new ArrayList<SelectItem>();

    SelectItemGroup group1 = new SelectItemGroup("Group 1");
    group1.setSelectItems(new SelectItem[] {
        new SelectItem("Group 1 Value 1", "Group 1 Label 1"),
        new SelectItem("Group 1 Value 2", "Group 1 Label 2"),
        new SelectItem("Group 1 Value 3", "Group 1 Label 3")
    });
    options.add(group1);

    SelectItemGroup group2 = new SelectItemGroup("Group 2");
    group2.setSelectItems(new SelectItem[] {
        new SelectItem("Group 2 Value 1", "Group 2 Label 1"),
        new SelectItem("Group 2 Value 2", "Group 2 Label 2"),
        new SelectItem("Group 2 Value 3", "Group 2 Label 3")
    });
    options.add(group2);
}

JSF view: JSF视图:

<h:selectOneMenu value="#{bean.option}">
    <f:selectItems value="#{bean.options}" />
</h:selectOneMenu>

Generated HTML example: 生成的HTML示例:

<select name="j_idt6:j_idt7" size="1">
    <optgroup label="Group 1">
        <option value="Group 1 Value 1">Group 1 Label 1</option>
        <option value="Group 1 Value 2">Group 1 Label 2</option>
        <option value="Group 1 Value 3">Group 1 Label 3</option>
    </optgroup>
    <optgroup label="Group 2">
        <option value="Group 2 Value 1">Group 2 Label 1</option>
        <option value="Group 2 Value 2">Group 2 Label 2</option>
        <option value="Group 2 Value 3">Group 2 Label 3</option>
    </optgroup>
</select>

How it look like in browser: 它在浏览器中的样子:

替代文字

I am not sure I understand what you're asking. 我不确定我明白你在问什么。 Assuming you want the sub-categories in the menu to be slightly indented? 假设您希望菜单中的子类略微缩进? If that's the case, how about sending from the server-side/handler the array of items already parsed with an "&nbsp"(space) or with a"-". 如果是这种情况,如何从服务器端/处理程序发送已经用“&nbsp”(空格)或“ - ”解析的项目数组。
In other words, you can't use javascript to parse and understand category hierarchy. 换句话说,您不能使用javascript来解析和理解类别层次结构。 You have 2 options - either run the recursion via JSF (sounds complicated, and ugly for the UI person who needs to design the page), or do the sorting on the server side, providing the JSF with aleardy indented entries. 你有两个选择 - 通过JSF运行递归(听起来很复杂,对于需要设计页面的UI人员来说很难看),或者在服务器端进行排序,为JSF提供aleardy缩进的条目。

Hope this helps, 希望这可以帮助,

Yishai 伊沙伊

But nested groups are not visualized correctly. 但嵌套组无法正确显示。 They appear as item not as group. 它们显示为项目而不是组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM