制作一个简单的Grid

 1EXTJS----GRID笔记1_gridvar cm = new Ext.grid.ColumnModel([
 2EXTJS----GRID笔记1_grid_02    {header:'编号',dataIndex:'id'},
 3EXTJS----GRID笔记1_grid_03    {header:'名称',dataIndex:'name'},
 4EXTJS----GRID笔记1_Extjs_04    {header:'描述',dataIndex:'desc'}
 5EXTJS----GRID笔记1_grid]);
 6EXTJS----GRID笔记1_grid
 7EXTJS----GRID笔记1_gridvar data = [
 8EXTJS----GRID笔记1_grid    [1,'name1','desc1'],
 9EXTJS----GRID笔记1_grid    [2,'name2','desc2'],
10EXTJS----GRID笔记1_grid    [3,'name3','desc3'],
11EXTJS----GRID笔记1_grid    [4,'name4','desc4'],
12EXTJS----GRID笔记1_grid    [5,'name5','desc5']
13EXTJS----GRID笔记1_grid];
14EXTJS----GRID笔记1_grid
15EXTJS----GRID笔记1_Extjs_15var store = new Ext.data.Store({
16EXTJS----GRID笔记1_grid_16    proxy:new Ext.data.MemoryProxy(data),
17EXTJS----GRID笔记1_grid_17    reader:new Ext.data.ArrayReader({},[
18EXTJS----GRID笔记1_Extjs_18        {name:'id'},
19EXTJS----GRID笔记1_grid_19        {name:'name'},
20EXTJS----GRID笔记1_Extjs_20        {name:'desc'}
21EXTJS----GRID笔记1_grid_16    ])
22EXTJS----GRID笔记1_grid_22}
);
23EXTJS----GRID笔记1_grid
24EXTJS----GRID笔记1_gridstore.load();
25EXTJS----GRID笔记1_grid
26EXTJS----GRID笔记1_Extjs_26var grid = new Ext.grid.GridPanel({
27EXTJS----GRID笔记1_grid_16    height:600,
28EXTJS----GRID笔记1_grid_16    renderTo:'grid',
29EXTJS----GRID笔记1_grid_16    store:store,
30EXTJS----GRID笔记1_grid_16    cm:cm
31EXTJS----GRID笔记1_grid_22}
);


Grid常用功能详解
    
enableColumnMove : true/false  //是否可拖放列
    enableColumnResize : true/false  //是否可以改变列的宽度
    stripeRows : true/false  //斑马线效果
    如果需要每列自动填满Grid,可以使用viewConfig配置中的foreceFit。即:

1EXTJS----GRID笔记1_grid_32var grid = new Ext.grid.GridPanel({
2EXTJS----GRID笔记1_grid_16    height:600,
3EXTJS----GRID笔记1_grid_16    renderTo:'grid',
4EXTJS----GRID笔记1_grid_16    store:store,
5EXTJS----GRID笔记1_grid_16    cm:cm,
6EXTJS----GRID笔记1_grid_37    viewConfig:{
7EXTJS----GRID笔记1_grid_16        forceFit:true
8EXTJS----GRID笔记1_Extjs_39    }

9EXTJS----GRID笔记1_grid_22}
);


    Gird支持中文排序需要重写Ext.data.Store的applySort函数:

 1EXTJS----GRID笔记1_grid_41Ext.data.Store.prototype.applySort = function() {
 2EXTJS----GRID笔记1_Extjs_42    if (this.sortInfo && !this.remoteSort) {
 3EXTJS----GRID笔记1_grid_16        var s = this.sortInfo, f = s.field;
 4EXTJS----GRID笔记1_grid_16        var st = this.fields.get(f).sortType;
 5EXTJS----GRID笔记1_grid_45        var fn = function(r1,r2){
 6EXTJS----GRID笔记1_grid_16            var v1 = st(r1.data[f]), v2=st(r2.data[f]);
 7EXTJS----GRID笔记1_Extjs_47            if (typeof(v1) == "string"){
 8EXTJS----GRID笔记1_grid_16                return v1.localeCompare(v2);
 9EXTJS----GRID笔记1_Extjs_39            }

10EXTJS----GRID笔记1_grid_16            return v1 > v2 ? 1: (v1 < v2 ? -1 : 0)
11EXTJS----GRID笔记1_Extjs_39        }
;
12EXTJS----GRID笔记1_grid_16        this.data.sort(s.direction,fn);
13EXTJS----GRID笔记1_grid_53        if (this.snapshot && this.snapshot != this.data) {
14EXTJS----GRID笔记1_grid_16            this.snapshot.sort(s.direction, fn);
15EXTJS----GRID笔记1_Extjs_39        }

16EXTJS----GRID笔记1_Extjs_39    }

17EXTJS----GRID笔记1_grid_22}
;


在单元格中显示红色的字、图片和按钮

 1EXTJS----GRID笔记1_gridvar cm = new Ext.grid.ColumnModel([
 2EXTJS----GRID笔记1_grid_02    {header:'编号',dataIndex:'id'},
 3EXTJS----GRID笔记1_grid_03    {header:'名称',dataIndex:'name'},
 4EXTJS----GRID笔记1_Extjs_61    {header:'性别',dataIndex:'sex',renderer:function(value){
 5EXTJS----GRID笔记1_Extjs_62        if (value == 'male') {
 6EXTJS----GRID笔记1_grid_16            return "<span style='color:red;font-weight:bold;'>红男</span><img src='user_male.png'/>"
 7EXTJS----GRID笔记1_grid_64        }
 else {
 8EXTJS----GRID笔记1_grid_16            return "<span style='color:green;font-weight:bold;'>绿女</span><img src='user_female.png'/>"
 9EXTJS----GRID笔记1_Extjs_39        }

10EXTJS----GRID笔记1_grid_22    }
}
,
11EXTJS----GRID笔记1_grid_68    {header:'描述',dataIndex:'desc',renderer:fn}
12EXTJS----GRID笔记1_grid]);
13EXTJS----GRID笔记1_grid
14EXTJS----GRID笔记1_Extjs_71var fn = function(value, cellmeta, record, rowIndex, columnIndex, store){
15EXTJS----GRID笔记1_grid_16    var str = "<input type='button' value='查看详细信息' onclick='alert(\""+
16EXTJS----GRID笔记1_grid_16        "这个单元格的值是: " + value + "\\n" +
17EXTJS----GRID笔记1_grid_74        "这个单元格的配置是: {cellId" + cellmeta.cellId + ",Id:" + cellmeta.id + ",css:" + cellmeta.css + "}\\n" +
18EXTJS----GRID笔记1_grid_16        "这个单元格对应行的record是:" + record + ", 一行的数据都在里面\\n" + 
19EXTJS----GRID笔记1_grid_16        "这是第" + rowIndex + "行\\n" +
20EXTJS----GRID笔记1_grid_16        "这是第" + columnIndex + "列\\n" +
21EXTJS----GRID笔记1_grid_16        "这个表格对应的Ext.data.Store在这里:" + store + ", 随便用吧。" +
22EXTJS----GRID笔记1_grid_16        "\")'>";
23EXTJS----GRID笔记1_grid_16    return str;
24EXTJS----GRID笔记1_grid_22}

给Grid的行和列设置颜色
EXTJS----GRID笔记1_grid//设置行的颜色
EXTJS----GRID笔记1_Extjs_83
viewConfig:{
EXTJS----GRID笔记1_grid_16    forceFit:
true,
EXTJS----GRID笔记1_grid_16    enableRowBody:
true,
EXTJS----GRID笔记1_grid_86    getRowClass:
function(record,rowIndex,p,ds){
EXTJS----GRID笔记1_grid_16        
var cls = 'white-row';
EXTJS----GRID笔记1_grid_16        
if (record.data.id % 2 == 0)
EXTJS----GRID笔记1_grid_89        
{
EXTJS----GRID笔记1_grid_16            cls 
= 'red-row'
EXTJS----GRID笔记1_Extjs_91        }
 else {
EXTJS----GRID笔记1_grid_16            cls 
= 'yellow-row'
EXTJS----GRID笔记1_Extjs_39        }

EXTJS----GRID笔记1_grid_16        
return cls;
EXTJS----GRID笔记1_Extjs_39    }

EXTJS----GRID笔记1_grid_22}

1EXTJS----GRID笔记1_grid//修改列的颜色,使用renderer属性配置如下方法
2EXTJS----GRID笔记1_Extjs_98var fn = function(value, cellmeta, record, rowIndex, columnIndex, store){
3EXTJS----GRID笔记1_grid_16    var value = record.get('color');
4EXTJS----GRID笔记1_grid_16    cell.attr = "style=background-color:" + value;
5EXTJS----GRID笔记1_grid_16    return data;
6EXTJS----GRID笔记1_grid_22}


    当然,如果只想修改Grid某一行的样式,还可以用grid.getView().addRowClass(r,css);修改Grid某一单元格的样式,可以用Ext.get(grid.getView().getCell(r,c)).addClass(css)或grid.getVies().getRow(r).style.backgroundColor = "red"。


 

原文地址:http://www.blogjava.net/zhengzhili/archive/2009/10/21/299161.html