Extjs4 grid按字段排序

Extjs版本ext-4.0.2a 
grid支持两种排序方法 
一种是前段排序,不请求后台本页面列字段进行排序在grid的列中设置属性sortable: true, 
第二种排序是后台排序,请求后台对整个数据进行排序,在store中设置属性remoteSort: true, 
第一种排序代码: 

Js代码    收藏代码
  1. var grid = Ext.create('Ext.grid.Panel', {  
  2.   title: '任务计划管理列表',  
  3.   region: 'center',  
  4.   store: store,  
  5.   columns: [ {  
  6.       header: '任务名称',  
  7.       sortable: true//设置这个属性进行排序  
  8.       dataIndex: 'task_name'  
  9.   }]  
  10. });  


第二种后来排序代码: 

Js代码    收藏代码
  1. var store = Ext.create('Ext.data.ArrayStore', {  
  2.     model: 'Task',  
  3.     autoLoad: true,  
  4.     pageSize: 20,  
  5.     // 设置服务器端映射。  
  6.     proxy: {  
  7.         type: 'ajax',  
  8.         url: 'task/getTaskInfo',  
  9.         // 定义数据结构  
  10.         reader: {  
  11.             type: 'json',  
  12.             totalProperty: 'totalProperty',  
  13.             root: 'root'  
  14.         }  
  15.     },  
  16.     remoteSort: true //设置属性进行请求后台排序  
  17. });  


后台java中会接收到几个固定的字符串 
json格式的名为sort的字符串里面包括两个属性一个是property是要排序的字段名,另一个是direction里面会有两个值( ASC 或DESC )我是用google的开源解析json的工具包gson来对其进行解析,代码如下: 

Java代码    收藏代码
  1. public static Map<String,String> getSort(String sort){  
  2.     Gson gson = new Gson();  
  3.     Map<String,String> map = new HashMap<String,String>();  
  4.     if(sort == null || "".equals(sort)){  
  5.       map.put("direction""");  
  6.         map.put("property""");  
  7.     }else{  
  8.       List<SortData> sortData = gson.fromJson(sort, new TypeToken<List<SortData>>(){}.getType());  
  9.         map.put("direction", sortData.get(0).getDirection());  
  10.         map.put("property", sortData.get(0).getProperty());  
  11.     }  
  12.     return map;  
  13.   }  


取得数据的代码片段如下: 

Java代码    收藏代码
  1. if(sort != null){  
  2.   Map<String,String> map = new HashMap<String,String>();  
  3.   map = CommonUtils.getSort(sort);  
  4.   String property = map.get("property");//排序的字段名和前台mode中字段名一致  
  5.   String direction= map.get("direction");//字符串( ASC 或DESC )  

然后请求后台数据库进行排序,在制造的时候前台的字段名尽量和数据库的字段名保持一致,这样方便了很多东西。 
一起同store请求传过来的参数还有名为start的int型参数,名为limit的int型数据,他们是分页条件,start是开始记录数,limit是本页最大记录数。

原文地址:http://liubl2011.iteye.com/blog/1246358

转载于:https://www.cnblogs.com/july2012/archive/2012/06/20/2556215.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值