【问题标题】:How to map the value of a h:selectBooleanCheckbox in a h:dataTable within a h:dataTable?如何将 h:selectBooleanCheckbox 的值映射到 h:dataTable 中的 h:dataTable 中?
【发布时间】:2011-04-05 02:32:00
【问题描述】:

有问题的 h:selectBooleanCheckbox 位于 h:dataTable(Extras)内的 h:dataTable(项目)内 h:dataTable(类别)内。显示了许多项目,每个项目可以有许多附加值。

<h:dataTable value="#{bean.categoryList}" var="category">
    <h:column>
        <h:dataTable value="#{category.itemList}" var="item">
            <h:column>
                <h:dataTable value="#{item.extraList}" var="extra">
                    <h:column>
                        <!-- The h:selectBooleanCheckbox in question //-->
                        <h:selectBooleanCheckbox value="#{bean.extraSelectedMap[item.id][extra.id]}"/>
                    </h:column>
                    <h:commandLink action="#{bean.add}" value="Add">
                </h:dataTable>
            </h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

页面呈现后,我选择一个复选框,然后选择“添加”。里面的bean.add我的

Map<Integer, HashMap<Integer, Boolean>>

当我期望额外的 id 映射到值 true 时,它​​有一个空的 HashMap。

上面的代码或整个方法有什么问题?

非常感谢和问候。

【问题讨论】:

    标签: jsf datatable hashmap


    【解决方案1】:

    首先,您的h:dataTable 有三层深度。如果要将复选框附加到父托管 bean 属性,则需要考虑所有级别。所以,

    <h:selectBooleanCheckbox value="#{bean.extraSelectedMap[category.id][item.id][extra.id]}"/>
    

    Map&lt;Integer, Map&lt;Integer, Map&lt;Integer, Boolean&gt;&gt;&gt; 作为属性。否则,每个类别的选择都将被覆盖,直到最后一个类别的选定项目最终出现在地图中。

    其次,您需要预先创建地图以及所有嵌套地图。 JSF 不会为您这样做。换句话说,

    public Bean() {
        extraSelectedMap = new HashMap<Integer, Map<Integer, Map<Integer, Boolean>>>();
        for (Category category : categoryList) {
            Map<Integer, Map<Integer, Boolean>> selectedExtrasPerCategory = new HashMap<Integer, Map<Integer, Boolean>>();
            extraSelectedMap.put(category.getId(), selectedExtrasPerCategory);
            for (Item item : category.getItemList()) {
                Map<Integer, Boolean> selectedExtrasPerItem = new HashMap<Integer, Boolean>();
                selectedExtrasPerCategory.put(item.getId(), selectedExtrasPerItem);
            }
        }
    

    作为替代方案,您也可以考虑仅将 Boolean 属性添加到 Extra 并绑定到该属性。

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 2011-02-01
      • 1970-01-01
      • 2011-11-22
      • 2016-03-26
      • 2012-06-16
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多