【问题标题】:How to know which lines were selected from a DataTable如何知道从 DataTable 中选择了哪些行
【发布时间】:2011-09-02 18:52:56
【问题描述】:

我有一个带有“rich:DataTable”的页面。对于每个表格行,添加一个复选框以允许用户在执行操作之前选择多行。如何检索被选中的行?

【问题讨论】:

    标签: datatable richfaces rows selected


    【解决方案1】:

    只需向代表rich:DataTable 行的POJO 添加一个布尔值,可能称为selected。并将此布尔值绑定到<rich:column> 中的<h:selectBooleanCheckbox> <rich:dataTable> 中的<rich:dataTable>

    例如,您的 beans、POJO 和视图可能如下所示:

    <rich:dataTable value="#{myBean.customerList}" var="customer">
      <rich:column>
       <h:selectBooleanCheckbox value="#{customer.selected}" /> 
      </rich:column>    
      <rich:column>
        <h:outputText value="#{customer.name}" />
      </rich:column>
      <rich:column>
        <h:outputText value="#{customer.address}" />
      </rich:column>
    </rich:dataTable>
    
    
    public class MyBean {
    
        private List<Customer> customerList;
    
        //getter and setter for the customerList
    }
    
    
    public class Customer{
    
        private boolean selected;
        private String name; 
        private String address;
    
        //getter and setter for the properties
    }
    

    要检索被选中的行,只需迭代MyBean.customerList 并检查Customerselected 属性是否为true

    【讨论】:

      【解决方案2】:

      [已解决] 我另有决定。以这种方式添加了“地图”和带有“复选框”的列:“”。通过提交表单,被选中的“地图”行将获得布尔属性等于“true”。

      我的豆子:

      public class Bean<T extends Object> {
          private Map<T, Boolean> selectedRowsMap = new HashMap<T, Boolean>(0);
          ...
          public Set<T> getSelectedRows() {
              selectedRows.clear();
              for (T key : getSelectedRowsMap().keySet()) {           
                  if (getSelectedRowsMap().get(key) == true){
                      selectedRows.add(key);              
                  }
              }
      
              return selectedRows;
          }
      }
      

      我的 XHTML:

      <rich:dataTable>
          <rich:column>
              <h:selectBooleanCheckbox value="#{bean.selectedRowsMap[row]}" />
          </rich:column>
          <rich:column>
              <h:outputText value="${row.age}" />
          </rich:column>
          ...
      <rich:dataTable>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-28
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 2017-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多