建立GridPanelajax
要使用GridPanel,首先要定义Store,而在建立Store的时候必需要有Model,所以咱们首先来定义Model:服务器
//1.定义Model Ext.define("MyApp.model.User", { extend: "Ext.data.Model", fields: [ { name: 'name', type: 'string' }, { name: 'age', type: 'int' }, { name: 'phone', type: 'string' } ] });
而后建立Store:异步
//2.建立store var store = Ext.create("Ext.data.Store", { model: "MyApp.model.User", autoLoad: true, pageSize: 5, proxy: { type: "ajax", url: "GridHandler.ashx", reader: { root: "rows" } } });
接下来才是GridPanel的代码:编辑器
//3.建立grid var grid = Ext.create("Ext.grid.Panel", { xtype: "grid", store: store, width: 500, height: 200, margin: 30, columnLines: true, renderTo: Ext.getBody(), selModel: { injectCheckbox: 0, mode: "SIMPLE", //"SINGLE"/"SIMPLE"/"MULTI" checkOnly: true //只能经过checkbox选择 }, selType: "checkboxmodel", columns: [ { text: '姓名', dataIndex: 'name' }, { text: '年龄', dataIndex: 'age', xtype: 'numbercolumn', format: '0', editor: { xtype: "numberfield", decimalPrecision: 0, selectOnFocus: true } }, { text: '电话', dataIndex: 'phone', editor: "textfield" } ], plugins: [ Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 }) ], listeners: { itemdblclick: function (me, record, item, index, e, eOpts) { //双击事件的操做 } }, bbar: { xtype: "pagingtoolbar", store: store, displayInfo: true } });
这个GridPanel的效果以下:ide
在这个GridPanel中,咱们能够经过复选框勾选数据行,能够编辑“年龄”和“电话”列,还能够对数据进行客户端排序。flex
Extjs GridPanel的列有多种类型,例如:文本列、数字列、日期列、选择框列、操做列等。咱们能够经过xtype来指定不一样的列类型。url
下面是列的经常使用配置项:idea
控制Extjs GridPanel行选择模型的两个配置项是selType和selModel。默认状况下,selType为rowmodel,对应的Ext.selection.Model。这种选择模型不会在grid中添加复选框,它经过点击行进行选中,默认为多选“MULTI”。spa
若是咱们要使用复选框来选择行,咱们须要使用下面的配置:插件
selType: "checkboxmodel",
而后,咱们能够经过selModel来配置selType:
selModel: { injectCheckbox: 0, mode: "SIMPLE", //"SINGLE"/"SIMPLE"/"MULTI" checkOnly: true //只能经过checkbox选择 },
除了界面操做来选中行,咱们还能够经过代码来选中行:
//选择行,并保持其余行的选择状态 grid.getSelectionModel().select(records, true); //选择全部 grid.getSelectionModel().selectAll(); //根据row index选择 grid.getSelectionModel().selectRange(startRow, endRow, true)
获取选中行,仍然须要经过SelectionModel来完成:
var records = grid.getSelectionModel().getSelection();
默认状况下Extjs GridPanel是不显示行号的,咱们须要手动添加行号列。
columns: [ { xtype:
"rownumberer", text: "序号"
, width:40 }, { text: '姓名', dataIndex: 'name' }, { text: '年龄', dataIndex: 'age', xtype: 'numbercolumn', format: '0', editor: { xtype: "numberfield", decimalPrecision: 0, selectOnFocus: true } }, { text: '电话', dataIndex: 'phone', editor: "textfield" } ],
咱们能够设置行号的列头和宽度。
Extjs GridPanel的异步加载数据是经过Store来实现的。咱们以前已经介绍过Extjs Store的各类代理方式,能够参考我以前的文章:
异步加载一般采用ajax代理,例如咱们代码中用到的:
//2.建立store var store = Ext.create("Ext.data.Store", { model: "MyApp.model.User", autoLoad: true, pageSize: 5, proxy: { type: "ajax", url: "GridHandler.ashx", reader: { root: "rows" } } });
服务器端返回的数据格式以下:
{ "rows": [ { "name": "Tom", "age": 12, "phone": "1233455" }, { "name": "Jerry", "age": 12, "phone": "1233455" }, { "name": "Sinbo", "age": 12, "phone": "1233455" }, { "name": "Jack", "age": 12, "phone": "1233455" }, { "name": "Johnson ", "age": 12, "phone": "1233455" } ], "total": 5 }
当GridPanel中数据量大的时候,咱们就须要使用分页了。
分页的实现由两部来完成,首先是在Store中添加pageSize配置项,用来肯定每页显示多少行数据;而后须要为GridPanel添加PagingToolbar。
1. Store添加pageSize配置项
var store = Ext.create("Ext.data.Store", { model: "MyApp.model.User", autoLoad: true, pageSize: 5, proxy: { type: "ajax", url: "GridHandler.ashx", reader: { root: "rows" } } });
在分页参数加上以后,Extjs在执行ajax请求的时候会添加三个参数:
2. GridPanel添加PagingToolbar
bbar: { xtype: "pagingtoolbar", store: store, displayInfo: true }
在完成这两项配置之后,GridPanel就可使用分页了。
Extjs GridPanel能够方便的实现列编辑。要实现这个功能须要两步:
1. 添加GridPanel的编辑插件
plugins: [ Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 }) ],
2. 为须要编辑的列指定编辑器
columns: [ { xtype: "rownumberer", text: "序号", width:40 }, { text: '姓名', dataIndex: 'name' }, { text: '年龄', dataIndex: 'age', xtype: 'numbercolumn', format: '0', editor: { xtype: "numberfield", decimalPrecision: 0, selectOnFocus: true } }, { text: '电话', dataIndex: 'phone', editor: "textfield" }
编辑器能够是一个field的配置,也能够是一个xtype。
对于编辑后的单元格,会在左上角出现一个红色的标识,说明该数据是编辑过的,要想去掉这个红色箭头,须要调用record的commit()方法。
grid.on('edit', function (editor, e) { // commit the changes right after editing finished e.record.commit(); });
除了单元格编辑外,Extjs还支持行编辑功能,只须要将插件替换为RowEditing便可,此处再也不进行演示。
在默认状况下,Extjs GridPanel不容许进行选中单元格中的内容,因为不能选中,咱们就不可能来复制单元格中的内容。若是要实现这种功能,咱们须要经过viewConfig来实现。
代码以下:
viewConfig:{ stripeRows:true,//在表格中显示斑马线 enableTextSelection:true //能够复制单元格文字 }
Extjs GridPanel的列,当咱们点击列头的时候,会出现一个菜单:
若是咱们要禁用这个菜单,能够将每一个column定义属性menuDisabled指定为true,代码以下:
{header: 'Idno', dataIndex: 'idno', width:150,menuDisabled:true}
本文的示例代码适用于Extjs 4.x和Extjs 5.x,在Extjs 4.2.1 和Extjs 5.0.1中可用!