如何在h:dataTable中的h:dataTable中映射h:selectBooleanCheckbox的值? [英] How to map the value of a h:selectBooleanCheckbox in a h:dataTable within a h:dataTable?

查看:113
本文介绍了如何在h:dataTable中的h:dataTable中映射h:selectBooleanCheckbox的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

h:selectBooleanCheckbox在h:dataTable(of Categories)中的h:dataTable(of Items)内的h:dataTable(附加)中。许多项目被显示,每个项目可以有很多其他的。

The h:selectBooleanCheckbox in question is in a h:dataTable (of Extras) within a h:dataTable (of Items) within a h:dataTable (of Categories). Many Items are displayed and each Item can have many 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我的

After the page is rendered I select a check box then select 'Add'. Inside bean.add my

Map<Integer, HashMap<Integer, Boolean>>

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

has an empty HashMap when I am expecting it to have the id of the extra mapped to the value true.

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

What is incorrect with the code, or the entire approach, above?

非常感谢和问候。

推荐答案

首先,你的 h:dataTable 如果要将复选框添加到父托管bean属性中,则需要考虑所有级别。因此,

First, your h:dataTable is three levels deep. If you want to attach the checkbox to a parent managed bean property, then you need to take all levels into account. So,

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

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

with a Map<Integer, Map<Integer, Map<Integer, Boolean>>> as property. Otherwise the selections will be overridden for every category until the selected items of the last category ends up in the map.

其次,您还需要预先制作地图和所有嵌套的地图。 JSF不会为你做这个。换句话说,

Second, you need to precreate the map and all nested maps as well. JSF won't do that for you. In other words,

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);
        }
    }

另外,您还可以考虑添加布尔值属性到额外,然后绑定到。

As an alternative, you can also consider to just add a Boolean property to Extra and bind to that instead.

这篇关于如何在h:dataTable中的h:dataTable中映射h:selectBooleanCheckbox的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆