Extjs版本ext-4.0.2a
grid支持两种排序方法
一种是前段排序,不请求后台本页面列字段进行排序在grid的列中设置属性sortable: true,
第二种排序是后台排序,请求后台对整个数据进行排序,在store中设置属性remoteSort: true,
第一种排序代码:
- var grid = Ext.create('Ext.grid.Panel', {
- title: '任务计划管理列表',
- region: 'center',
- store: store,
- columns: [ {
- header: '任务名称',
- sortable: true, //设置这个属性进行排序
- dataIndex: 'task_name'
- }]
- });
第二种后来排序代码:
- var store = Ext.create('Ext.data.ArrayStore', {
- model: 'Task',
- autoLoad: true,
- pageSize: 20,
- // 设置服务器端映射。
- proxy: {
- type: 'ajax',
- url: 'task/getTaskInfo',
- // 定义数据结构
- reader: {
- type: 'json',
- totalProperty: 'totalProperty',
- root: 'root'
- }
- },
- remoteSort: true //设置属性进行请求后台排序
- });
后台java中会接收到几个固定的字符串
json格式的名为sort的字符串里面包括两个属性一个是property是要排序的字段名,另一个是direction里面会有两个值( ASC 或DESC )我是用google的开源解析json的工具包gson来对其进行解析,代码如下:
- public static Map<String,String> getSort(String sort){
- Gson gson = new Gson();
- Map<String,String> map = new HashMap<String,String>();
- if(sort == null || "".equals(sort)){
- map.put("direction", "");
- map.put("property", "");
- }else{
- List<SortData> sortData = gson.fromJson(sort, new TypeToken<List<SortData>>(){}.getType());
- map.put("direction", sortData.get(0).getDirection());
- map.put("property", sortData.get(0).getProperty());
- }
- return map;
- }
取得数据的代码片段如下:
- if(sort != null){
- Map<String,String> map = new HashMap<String,String>();
- map = CommonUtils.getSort(sort);
- String property = map.get("property");//排序的字段名和前台mode中字段名一致
- String direction= map.get("direction");//字符串( ASC 或DESC )
- }
然后请求后台数据库进行排序,在制造的时候前台的字段名尽量和数据库的字段名保持一致,这样方便了很多东西。
一起同store请求传过来的参数还有名为start的int型参数,名为limit的int型数据,他们是分页条件,start是开始记录数,limit是本页最大记录数。
原文地址:http://liubl2011.iteye.com/blog/1246358