Extjs GridPanel用法详解

建立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的列

Extjs GridPanel的列有多种类型,例如:文本列、数字列、日期列、选择框列、操做列等。咱们能够经过xtype来指定不一样的列类型。url

下面是列的经常使用配置项:idea

  • xtype:列类型
  • text:列头显示的文字
  • dataIndex:绑定的字段名
  • width:宽度
  • flex:自动适应的宽度
  • sortable:是否可排序,默认为是
  • hideable:是否可隐藏,默认为是
  • locked:锁定列,将列锁定在grid的开头,当grid出现滚动条的时候该属性比较有用。默认为否
  • lockable:是否可锁定,默认为否
  • format:格式化字符串,经常使用于日期、数字的格式化。日期:'Y-m-d';日期时间:'Y-m-d H:i:s';数字:'0,000.00'(带有千位分隔符、保留两位小数)、'0.00'(保留两位小数),'0'(不保留小数)
  • renderer:自定义绘制方法,能够是Ext.util.Format中定义好的方法名称,也能够是自定义否function,该方法接收下面的参数:value、metadata、record、rowIndex、colIndex、store、view,并须要一个用来显示的返回值。
  • editor:编辑器,当使用编辑插件的时候才会起做用。

 

Extjs GridPanel行选择模型(SelectionModel)

控制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选择
},

Extjs GridPanel行选择

除了界面操做来选中行,咱们还能够经过代码来选中行:

//选择行,并保持其余行的选择状态
grid.getSelectionModel().select(records, true);
//选择全部
grid.getSelectionModel().selectAll();
//根据row index选择
grid.getSelectionModel().selectRange(startRow, endRow, true)

Extjs GridPanel获取选中行

获取选中行,仍然须要经过SelectionModel来完成:

var records = grid.getSelectionModel().getSelection();

Extjs GridPanel显示行号

默认状况下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异步加载数据

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
}

Extjs GridPanel分页

当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请求的时候会添加三个参数:

  • page:当前页
  • start:起始行的行号
  • limit:每页数据行数,默认为25;这个参数值就是咱们设置的pageSize

2. GridPanel添加PagingToolbar

bbar: { xtype: "pagingtoolbar", store: store, displayInfo: true }

在完成这两项配置之后,GridPanel就可使用分页了。

Extjs 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选中单元格内容

在默认状况下,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中可用!

相关文章
相关标签/搜索