1:调整列的顺序和改变列的宽度
通过设置enableColumnMove和enableColumnResize来设置
2:选择多行数据
可以通过disableSelection来设置,默认值为false
3:添加底纹
var cumgrid = new Ext.grid.GridPanel({ renderTo: 'cumGrid', store: store, height: 200, width: 500, stripeRows: true, //添加斑马纹效果 loadMask: true, colModel: cum });
4:遮罩和提示功能
通过loadMask属性来设置
5:自定义列宽度
var cum = new Ext.grid.ColumnModel([ {header: '客户ID', dataIndex: 'memid', width: 50}, {header: '客户姓名', dataIndex: 'memName', width:80}, {header: '性别', dataIndex: 'sex', width: 50}, {header: '跟踪号', dataIndex: 'gcode', width: 50}, {header: '日期', dataIndex: 'kdtime', width: 120} ]);
使用viewConfig中的forceFit即可,它是给GridView用的配置,指示视图层重新计算所有列宽后填充表格
var cumgrid = new Ext.grid.GridPanel({ renderTo: 'cumGrid', store: store, height: 200, width: 500, stripeRows: true, //添加斑马纹效果 viewConfig: { forceFit: true //自动调整列宽度 } loadMask: true, colModel: cum });
7:控制列宽度自动伸展
除了可以使用forceFit以外,还可以考虑使用autoExpandColumn,它可以让指定列的宽度自动伸展,从而填充整个表格
var cum = new Ext.grid.ColumnModel([ {header: '客户ID', dataIndex: 'memid', width: 50}, {header: '客户姓名', dataIndex: 'memName', width:80}, {header: '性别', dataIndex: 'sex', width: 50}, {header: '跟踪号', dataIndex: 'gcode', width: 50}, {id: 'kdtime', header: '日期', dataIndex: 'kdtime', width: 120} ]);
var cumgrid = new Ext.grid.GridPanel({ renderTo: 'cumGrid', store: store, height: 200, width: 500, stripeRows: true, //添加斑马纹效果 loadMask: true, colModel: cum, autoExpandColumn: 'kdtime' //让指定列的宽度自动伸展 });
需要在columnModel列模型中使用sortable属性,如下所示:
var cum = new Ext.grid.ColumnModel([ {header: '客户ID', dataIndex: 'memid', width: 50, sortable: true}, {header: '客户姓名', dataIndex: 'memName', width:80}, {header: '性别', dataIndex: 'sex', width: 50}, {header: '跟踪号', dataIndex: 'gcode', width: 50}, {id: 'kdtime', header: '日期', dataIndex: 'kdtime', width: 120} ]);
可以看到客户ID列可以排序
9:格式化日期显示
程序:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>格式化日期显示</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="ext3.2/resources/css/ext-all.css"></link> <script type="text/javascript" src="ext3.2/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="ext3.2/ext-all.js"></script> <script type="text/javascript" src="ext3.2/src/local/ext-lang-zh_CN.js"></script> <script type="text/javascript"> Ext.onReady(function() { //定义ColumnModel var cum = new Ext.grid.ColumnModel([ {header: '客户ID', dataIndex: 'memid', width: 50, sortable: true}, {header: '客户姓名', dataIndex: 'memName', width:80}, {header: '性别', dataIndex: 'sex', width: 50}, {header: '跟踪号', dataIndex: 'gcode', width: 50}, {id: 'kdtime', header: '日期', dataIndex: 'kdtime', width: 120, type: 'date', renderer: Ext.util.Format.dateRenderer('Y年m月d日')} ]); var cumdata = [ //定义数据 ['1', 'hello', '男', '1', '2010-05-22T02:58:04'], ['2', 'ASP', '女', '2', '2010-06-07T02:58:04'], ['3', 'JSP', '男', '3', '2011-05-05T02:58:04'], ['4', 'Java', '男', '5', '2011-07-05T02:58:04'], ['5', 'ExtJs', '男', '5', '2011-06-01T02:58:04'], ['6', '.Net', '女', '6', '2011-09-03T02:58:04'] ]; var store = new Ext.data.Store({ proxy: new Ext.data.MemoryProxy(cumdata), reader: new Ext.data.ArrayReader({}, [ {name: 'memid'}, {name: 'memName'}, {name: 'sex'}, {name: 'gcode'}, {name: 'kdtime', type:'date', dateFormat: 'Y-m-dTH:i:s'} ]) }); store.load(); var cumgrid = new Ext.grid.GridPanel({ renderTo: 'cumGrid', store: store, height: 200, width: 500, stripeRows: true, //添加斑马纹效果 loadMask: true, colModel: cum, autoExpandColumn: 'kdtime' //让指定列的宽度自动伸展 }); }); </script> </head> <body> <div id="cumGrid"> </div> </body> </html>
其中:
var cum = new Ext.grid.ColumnModel([ {header: '客户ID', dataIndex: 'memid', width: 50, sortable: true}, {header: '客户姓名', dataIndex: 'memName', width:80}, {header: '性别', dataIndex: 'sex', width: 50}, {header: '跟踪号', dataIndex: 'gcode', width: 50}, {id: 'kdtime', header: '日期', dataIndex: 'kdtime', width: 120, type: 'date', renderer: Ext.util.Format.dateRenderer('Y年m月d日')} ]);
var store = new Ext.data.Store({ proxy: new Ext.data.MemoryProxy(cumdata), reader: new Ext.data.ArrayReader({}, [ {name: 'memid'}, {name: 'memName'}, {name: 'sex'}, {name: 'gcode'}, {name: 'kdtime', type:'date', dateFormat: 'Y-m-dTH:i:s'} ]) });