如何使用< h:selectBooleanCheckbox>在< h:dataTable>选择多行? [英] How to use <h:selectBooleanCheckbox> in <h:dataTable> to select multiple rows?

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

问题描述

我有一个带有< h:dataTable> 的Facelets页面。在每行中有一个< h:selectBooleanCheckbox> 。如果选中该复选框,则应在bean中设置相应行后面的对象。


  1. 我该如何做?

  2. 如何在支持bean中获取所选行或其数据?

  3. 或者最好用< h:selectManyCheckbox>

  4. <你最好的选择是将 h:selectBooleanCheckbox 值绑定到一个 Map< RowId,Boolean> 属性其中 RowId 表示行标识符的类型。我们举个例子,你有一个 Item 对象,其标识符属性 id 是一个 Long

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

    与...结合使用:

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

      public class Bean {
    private Map< Long,Boolean> checked = new HashMap< Long,Boolean>();
    私人列表< Item>物品

    public void submit(){
    列表< Item> checkedItems = new ArrayList< Item>();

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

    checked.clear(); //如有必要

    //现在用checkedItems来做你的事情。
    }

    // ...
    }

    您会看到,地图将自动填充所有表项的 id 作为关键字,复选框将自动设置为与项目<$ c $相关联的地图值c> id 作为关键。


    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. 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>?

    解决方案

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

    which is to be used in combination with:

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

    and

    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.
        }
    
        // ...
    }
    

    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.

    这篇关于如何使用&lt; h:selectBooleanCheckbox&gt;在&lt; h:dataTable&gt;选择多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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