<h:selectBooleanCheckbox> 的使用方法在&lt;h:dataTable&gt;或 &lt;ui:repeat&gt;选择多个项目? [英] How to use &lt;h:selectBooleanCheckbox&gt; in &lt;h:dataTable&gt; or &lt;ui:repeat&gt; to select multiple items?

查看:23
本文介绍了<h:selectBooleanCheckbox> 的使用方法在&lt;h:dataTable&gt;或 &lt;ui:repeat&gt;选择多个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 的 Facelets 页面.在每一行中有一个 .如果选中复选框,则应在 bean 中设置相应行后面的对象.

I have a Facelets page with a <h:dataTable>. In each row there is a <h:selectBooleanCheckbox>. If the checkbox is selected the object behind the corresponding row should be set in the bean.

  1. 我该怎么做?
  2. 如何在支持 bean 中获取选定的行或其数据?
  3. 或者使用 会更好吗?
  1. How do I do this?
  2. How to get the selected rows or their data in a backing bean?
  3. Or would it be better to do it with <h:selectManyCheckbox>?

推荐答案

最好的办法是将 h:selectBooleanCheckbox 值与 Map 绑定属性,其中 RowId 表示行标识符的类型.让我们举一个例子,你有一个 Item 对象,它的标识符属性 id 是一个 Long:

Your best bet is to bind the h:selectBooleanCheckbox value with a Map<RowId, Boolean> property where RowId represents the type of the row identifier. Let's take an example that you've a Item object whose identifier property id is a Long:

<h:dataTable value="#{bean.items}" var="item">
    <h:column>
        <h:selectBooleanCheckbox value="#{bean.checked[item.id]}" />
    </h:column>
    ...
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />

要结合使用:

public class Item {
    private Long id;
    // ...
}

public class Bean {
    private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
    private List<Item> items;

    public void submit() {
        List<Item> checkedItems = checked.entrySet().stream()
            .filter(Entry::getKey)
            .map(Entry::getValue)
            .collect(Collectors.toList());

        checked.clear(); // If necessary.

        // Now do your thing with checkedItems.
    }

    // ...
}

你看,地图自动填充所有表项的id作为键,复选框值自动设置为与项目id关联的地图值作为键.

You see, the map is automatically filled with the id of all table items as key and the checkbox value is automatically set as map value associated with the item id as key.

这篇关于<h:selectBooleanCheckbox> 的使用方法在&lt;h:dataTable&gt;或 &lt;ui:repeat&gt;选择多个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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