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

时间:2015-12-22 09:55:52

标签: jsf jsf-2 primefaces jstl

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

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

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

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

Hashmap键等于“tag level”。

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

<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 个答案:

答案 0 :(得分:1)

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

您最好在#{product.tags.tree}中迭代<c:forEach items>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吗?

相关问题