Ext的CheckboxSelectionModel默认选中

说明:勾选某几条记录后,翻页再翻回来,保持原来的选中状态。

原理:在勾选或取消勾选的时候用recordIds保存或删除勾选中的Record的主键id值,
   每次store加载(load事件)完后,遍历store,比较每条Record的主键列id是否在recordIds中,
   若存在则将Record保存到临时变量records中,最后调用selMod.selectRecords(records, true);
   
写Array的remove与contains方法,搞得有点像java.util.HashSet(不让Array存重复值)

Js代码   收藏代码
  1. function RemoveArray(array,attachId){  
  2.     for(var i=0,n=0;i<array.length;i++){  
  3.         if(array[i]!=attachId){  
  4.             array[n++]=array[i]  
  5.         }  
  6.     }  
  7.     array.length -= 1;  
  8. }  
  9. function containsArray(array,attachId){  
  10.     for(var i=0;i<array.length;i++){  
  11.         if(array[i]==attachId){  
  12.             return true;  
  13.             break;  
  14.         }  
  15.     }  
  16.    return false;  
  17. }   
  18. Array.prototype.remove = function (obj) {  
  19.     return RemoveArray(this,obj);  
  20. };   
  21. Array.prototype.contains = function (obj) {  
  22.     return containsArray(this,obj);  
  23. };   

 

Js代码   收藏代码
  1. var recordIds=new Array();// 选中的Record主键列id列表  
  2. var recordsChecked=new Array();// 选中的Record列表  
  3.   
  4. var store = new Ext.data.JsonStore({  
  5.                 url : 'viewXXX.do',  
  6.                 pruneModifiedRecords : true,  
  7.                 totalProperty : 'totalProperty',  
  8.                 root : 'root',  
  9.                 fields : [{name : 'id'},   
  10.                           {name : 'status'}  
  11.                           ],  
  12.                 listeners : {  
  13.                     load : function() {  
  14.                         var records = new Array();  
  15.                         store.each(function(record) {  
  16.                             if (recordIds.contains(record.data.id))  
  17.                                  records.push(record);  
  18.                         });  
  19.                         selMod.selectRecords(records, true);// 以后每次load数据时,都会默认选中  
  20.                     }  
  21.                 }  
  22.             });  
  23.       
  24.     var selMod = new Ext.grid.CheckboxSelectionModel({  
  25.         handleMouseDown : Ext.emptyFn,  
  26.                 listeners : {  
  27.                     "rowdeselect" : {  
  28.                         fn : function(e, rowIndex, record) {  
  29.                             if (recordIds.contains(record.data.id)) {  
  30.                                 recordIds.remove(record.data.id);  
  31.                                 recordsChecked.remove(record);  
  32.                             }  
  33.                         }  
  34.                     },  
  35.                     "rowselect" : {  
  36.                         fn : function(e, rowIndex, record) {  
  37.                             if (!recordIds.contains(record.data.id))  
  38.                                 recordIds.push(record.data.id);  
  39.                             recordsChecked.push(record);  
  40.                         }  
  41.                     }  
  42.                 }  
  43.             });  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值