如何将 ah:selectBooleanCheckbox 的值映射到 ah:dataTable 中的 ah:dataTable 中?

发布于 2024-09-16 01:11:35 字数 1090 浏览 2 评论 0 原文

有问题的 h:selectBooleanCheckbox 位于 ah:dataTable(类别)内的 ah:dataTable(项目)内的 ah: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>>

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

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

非常感谢和问候。

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>

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

Map<Integer, HashMap<Integer, Boolean>>

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?

Many thanks and regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2024-09-23 01:11:35

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

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

使用 Map> 作为属性。否则,每个类别的选择都会被覆盖,直到最后一个类别的选定项目最终出现在地图中。

其次,您还需要预先创建地图和所有嵌套地图。 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);
        }
    }

作为替代方案,您还可以考虑仅向 Extra 添加一个 Boolean 属性并绑定到它。

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]}"/>

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文