如何在h:dataTable中的h:dataTable中映射h:selectBooleanCheckbox的值?

时间:2010-08-28 04:31:21

标签: jsf datatable hashmap

有问题的h:selectBooleanCheckbox位于h:dataTable(类别)中h:dataTable(Items)中的h:dataTable(Extras)中。显示许多项目,每个项目可以有许多额外项目。

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

上面的代码或整个方法有什么不对?

非常感谢和问候。

1 个答案:

答案 0 :(得分:3)

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

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

Map<Integer, Map<Integer, Map<Integer, Boolean>>>为属性。否则,将覆盖每个类别的选择,直到最后一个类别的所选项目在地图中结束。

其次,您还需要预先创建地图和所有嵌套地图。 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并将其绑定到该属性。

相关问题