如何使用JSF的h:selectBooleanCheckbox和h:dataTable来创建一行一个对象? [英] How to use JSF's h:selectBooleanCheckbox with h:dataTable to create one object per row?

查看:430
本文介绍了如何使用JSF的h:selectBooleanCheckbox和h:dataTable来创建一行一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Facelets页面,其中包含 h:dataTable 。在 h:dataTable 的每一行中有一个 h:selectBooleanCheckbox 。如果选中该复选框,则应创建一个新对象,其中的数据不在相应的行中。

I have a Facelets page with a h:dataTable. In each row of the h:dataTable there is a h:selectBooleanCheckbox. If the checkbox is selected a new Object should be created with the data out of the corresponding row.



  1. 如何获取所选行或其在备份bean中的数据?

  2. 或者最好使用 h:selectManyCheckbox

  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?


推荐答案

最好的办法是用<$ c $绑定 h:selectBooleanCheckbox c> Map< RowId,Boolean> 属性其中 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 = new ArrayList<Item>();

        for (Item item : items) {
            if (checked.get(item.getId())) {
                checkedItems.add(item);
            }
        }

        checked.clear(); // If necessary.

        // Now do your thing with checkedItems.
    }

    // ...
}

您看到,地图自动填充所有表项的 id 作为键,并且复选框值自动设置为与<$ c $相关联的地图值c> 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.

这篇关于如何使用JSF的h:selectBooleanCheckbox和h:dataTable来创建一行一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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