首页 文章

使用c:forEach动态设置h:selectOneMenu的值

提问于
浏览
0

我正在开展一个项目,要求我显示并能够为产品选择和存储标签 . 标签以树状结构提供 . 我不能假设标签树的最大深度 .

我希望显示按级别分割的标签,使用c:forEach - p:selectManyCheckbox - f:selectItems,并使用p:ajax组件处理选择 .

我使用以下类型在Tree对象中存储可能的值和选择:

HashMap<Long, ArrayList<Tag>> tree;
HashMap<Long, Object[]> selected;

Hashmap键等于“标记级别” .

为了显示值,我使用以下代码进行测试:

<p:panelGrid id="tagDisplay" columns="2">
    <c:forEach begin="1" end="5" var="idx">
        <p:outputLabel value="#{idx}"></p:outputLabel>
        <p:selectManyCheckbox value="#{product.tags.selected[1]}">
            <f:selectItems value="#{product.tags.tree[1]}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag.id}" />
            <p:ajax listener="#{product.selectorListener}" update="tagDisplay" />
        </p:selectManyCheckbox>
    </c:forEach>
</p:panelGrid>

代码似乎运行良好,但显示五次 .

现在我不得不尝试动态地将Hashmaps绑定到选择器 . 当我用“idx”替换“1”时,我没有得到任何结果 .

我尝试使用ui-repeat和一个虚拟表,但后来我丢失了panelgrid结构 .

任何帮助将不胜感激!

我的环境 - Websphere 8.5,JSF 2.2,Primefaces 5.2

1 回答

  • 1

    <c:forEach begin end> 仅用于静态迭代,不适用于动态迭代 .

    你最好在 <c:forEach items> 中迭代 #{product.tags.tree} 本身 . 对 Map 的每次迭代都会返回 Map.Entry ,而后者又有 getKey()getValue() 方法 .

    <p:panelGrid ...>
        <c:forEach items="#{product.tags.tree}" var="entry" varStatus="loop">
            <p:outputLabel value="#{loop.index}"></p:outputLabel>
            <p:selectManyCheckbox value="#{product.tags.selected[entry.key]}">
                <f:selectItems value="#{entry.value}" ... />
                ...
            </p:selectManyCheckbox>
        </c:forEach>
    </p:panelGrid>
    

    那就是说,它真的应该是 HashMap 吗?难道你不想要一个固定的订购 LinkedHashMap

相关问题