在JSF DataTable 中实现多选

参考了http://blog.csdn.net/Samland/archive/2006/09/17/1232585.aspx。决定通过修改值对象的方法在
JSF DataTable中实现多选的功能。

首先,给数据模型对象bean中添加一个boolean的属性selected.
  1. public class Item {
  2.  
  3.   private Integer id;
  4.   ...
  5.   private boolean selected;
  6.  
  7.   public Integer getId() {
  8.     return id;
  9.   }
  10.  
  11.   public void setId(Integer id) {
  12.     this.id = id;
  13.   }
  14.  ....
  15.   public boolean isSelected() {
  16.     return selected;
  17.   }
  18.  
  19.   public void setSelected(boolean selected) {
  20.     this.selected = selected;
  21.   }
  22. }
2、在JSF 页面的dataTable中加入一列 <h:selectBooleanCheckbox/>,其value的值和selected属性相对应,这样就可以通过点击checkbox来确定对象是否被选择。
  1. <h:form>
  2. <h:dataTable id="itemsTable" 
  3.     value="#{ListBean.List}" var="row"  >
  4. <h:column>
  5.   <f:facet name="header">
  6.     <h:outputText value="Select" />
  7.   </f:facet>
  8.   <h:selectBooleanCheckbox value="#{row.selected}" />
  9. </h:column>
  10. <h:column>
  11.   <f:facet name="header">
  12.     <h:outputText value="ID" />  
  13.   </f:facet> 
  14.     <h:outputText value="#{row.id}"></h:outputText>
  15. </h:column>
  16. ...
  17. </h:dataTable>
  18. <h:commandButton value="Get Selected"
  19.     actionListener="#{ListBean.getSelectedItems}" />
  20. </h:form>
3.在managed beans中通过如下的方式获得被选择的对象.

  1.    1public class ListBean {
  2.    2.  
  3.    3.  private List<Item> list;
  4.    4.
  5.    5public getList()
  6.    6. {
  7.    7.    //init list
  8.    8.    return list;
  9.    9. }
  10.   10.  
  11.   11.  
  12.   12.   public void getSelectedItems(ActionEvent event) {
  13.   13.     for (Item item : list) {
  14.   14.       if (item.isSelected()) {
  15.   15.         // do sth with selected items
  16.   16.       }
  17.   17.     }
  18.   18.   }
  19.   19. }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值