下拉列表中的 JSF 树

发布于 2024-10-03 07:20:40 字数 77 浏览 0 评论 0原文

我想在 JSF 的下拉列表中显示树状结构。基本上,选择的项目位于层次结构中,我希望这一点在下拉列表中显而易见。

是否可以 ?

I want to display a tree like structure in my dropdown in JSF. Basically the select items are in a hierarchy and I would like that to be evident in the dropdown.

Is it possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-10-10 07:20:40

那么,您基本上想要一个 HTML 吗?使用 SelectItemGroup

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 视图:

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

生成的 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>

它在浏览器中的样子:

alt text

So, you basically want a HTML <optgroup>? Use SelectItemGroup.

JSF bean (I assume 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:

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

Generated HTML example:

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

alt text

叫嚣ゝ 2024-10-10 07:20:40

我不确定我是否明白你在问什么。假设您希望菜单中的子类别稍微缩进?
如果是这种情况,如何从服务器端/处理程序发送已经用“ ”(空格)或“-”解析的项目数组。
换句话说,你无法使用javascript来解析和理解类别层次结构。您有 2 个选择 - 要么通过 JSF 运行递归(对于需要设计页面的 UI 人员来说听起来很复杂且丑陋),要么在服务器端进行排序,为 JSF 提供已经缩进的条目。

希望这有帮助,

伊莎伊

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 " "(space) or with a"-".
In other words, you can't use javascript to parse and understand category hierarchy. 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.

Hope this helps,

Yishai

扶醉桌前 2024-10-10 07:20:40

但嵌套组的可视化不正确。它们显示为项目而不是组。

But nested groups are not visualized correctly. They appear as item not as group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文