今天学习extjs中的grid,它可以说是功能强大,无出其右,只有你想不到的,没有它做不到的,呵呵,好像是有点夸张了。好,不说废话了,我们就从最简单的grid开始,一步步来看看extjs给我们提供的grid究竟给我们提供了哪些功能。
一个grid包括一些行和列,在extjs里边,列由Ext.grid.ColumnModel来管理,我们来看看如何创建一个ColumnModel对象:
1
var
cm
=
new
Ext.grid.ColumnModel([
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'}
,
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{header: "Price", width: 75, sortable: true, dataIndex: 'price'}
,
4![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{header: "Change", width: 75, sortable: true, dataIndex: 'change'}
,
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange'}
,
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{header: "Last Updated", width: 85, sortable: true, dataIndex: 'lastChange'}
7
]);
这里定义了五个列,列可以通过参数进行配置:id用来标识列,在css中使用该id可以对整列所有的单元格设置样式,可自动扩充的列也根据这个id来标识;header是列名字;width是列的宽度;sortable用来指明列是否可排序,dataIndex,先不管它。比较常用的参数还有renderer,指示列如何来呈现,后边会再详细介绍它。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
有了列,我们还需要一些数据来填充行,构造一个数组吧:
1
var
myData
=
[
2
[
'
3m Co
'
,
71.72
,
0.02
,
0.03
,
'
9/1 12:00am
'
],
3
[
'
Alcoa Inc
'
,
29.01
,
0.42
,
1.47
,
'
9/1 12:00am
'
],
4
[
'
Altria Group Inc
'
,
83.81
,
0.28
,
0.34
,
'
9/1 12:00am
'
],
5
[
'
American Express Company
'
,
52.55
,
0.01
,
0.02
,
'
9/1 12:00am
'
],
6
[
'
American International Group, Inc.
'
,
64.13
,
0.31
,
0.49
,
'
9/1 12:00am
'
],
7
[
'
AT&T Inc.
'
,
31.61
,
-
0.48
,
-
1.54
,
'
9/1 12:00am
'
],
8
[
'
Boeing Co.
'
,
75.43
,
0.53
,
0.71
,
'
9/1 12:00am
'
],
9
[
'
Caterpillar Inc.
'
,
67.27
,
0.92
,
1.39
,
'
9/1 12:00am
'
],
10
[
'
Citigroup, Inc.
'
,
49.37
,
0.02
,
0.04
,
'
9/1 12:00am
'
],
11
[
'
E.I. du Pont de Nemours and Company
'
,
40.48
,
0.51
,
1.28
,
'
9/1 12:00am
'
],
12
[
'
Exxon Mobil Corp
'
,
68.1
,
-
0.43
,
-
0.64
,
'
9/1 12:00am
'
],
13
[
'
General Electric Company
'
,
34.14
,
-
0.08
,
-
0.23
,
'
9/1 12:00am
'
],
14
[
'
General Motors Corporation
'
,
30.27
,
1.09
,
3.74
,
'
9/1 12:00am
'
],
15
[
'
Hewlett-Packard Co.
'
,
36.53
,
-
0.03
,
-
0.08
,
'
9/1 12:00am
'
],
16
[
'
Honeywell Intl Inc
'
,
38.77
,
0.05
,
0.13
,
'
9/1 12:00am
'
],
17
[
'
Intel Corporation
'
,
19.88
,
0.31
,
1.58
,
'
9/1 12:00am
'
],
18
[
'
International Business Machines
'
,
81.41
,
0.44
,
0.54
,
'
9/1 12:00am
'
],
19
[
'
Johnson & Johnson
'
,
64.72
,
0.06
,
0.09
,
'
9/1 12:00am
'
],
20
[
'
JP Morgan & Chase & Co
'
,
45.73
,
0.07
,
0.15
,
'
9/1 12:00am
'
],
21
[
'
McDonald\
'
s Corporation
'
,36.76,0.86,2.40,
'
9
/
1 12:00am'],
22
[
'
Merck & Co., Inc.
'
,
40.96
,
0.41
,
1.01
,
'
9/1 12:00am
'
],
23
[
'
Microsoft Corporation
'
,
25.84
,
0.14
,
0.54
,
'
9/1 12:00am
'
],
24
[
'
Pfizer Inc
'
,
27.96
,
0.4
,
1.45
,
'
9/1 12:00am
'
],
25
[
'
The Coca-Cola Company
'
,
45.07
,
0.26
,
0.58
,
'
9/1 12:00am
'
],
26
[
'
The Home Depot, Inc.
'
,
34.64
,
0.35
,
1.02
,
'
9/1 12:00am
'
],
27
[
'
The Procter & Gamble Company
'
,
61.91
,
0.01
,
0.02
,
'
9/1 12:00am
'
],
28
[
'
United Technologies Corporation
'
,
63.26
,
0.55
,
0.88
,
'
9/1 12:00am
'
],
29
[
'
Verizon Communications
'
,
35.57
,
0.39
,
1.11
,
'
9/1 12:00am
'
],
30
[
'
Wal-Mart Stores, Inc.
'
,
45.45
,
0.73
,
1.63
,
'
9/1 12:00am
'
]
31
];
现在万事俱备只欠东风了,列定义好了,数据也有了,接下来就是把它们组装成一个grid。看一下完整的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1
//
/<reference path="vswd-ext_2.0.2.js" />
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*
3
*作者:大笨
4
*日期:2009-10-13
5
*版本:1.0
6
*博客地址:http://yage.cnblogs.com
7
*/
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Ext.onReady(
function
()
{
10
//构造列
11
var cm = new Ext.grid.ColumnModel([
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' },
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ header: "Price", width: 75, sortable: true, dataIndex: 'price' },
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ header: "Change", width: 75, sortable: true, dataIndex: 'change' },
15![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange' },
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ header: "Last Updated", width: 85, sortable: true, dataIndex: 'lastChange' }
17
]);
18
//构造数据
19
var myData = [
20
['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
21
['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
22
['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
23
['American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am'],
24
['American International Group, Inc.', 64.13, 0.31, 0.49, '9/1 12:00am'],
25
['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
26
['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
27
['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
28
['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
29
['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28, '9/1 12:00am'],
30
['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
31
['General Electric Company', 34.14, -0.08, -0.23, '9/1 12:00am'],
32
['General Motors Corporation', 30.27, 1.09, 3.74, '9/1 12:00am'],
33
['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
34
['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
35
['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
36
['International Business Machines', 81.41, 0.44, 0.54, '9/1 12:00am'],
37
['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
38
['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
39
['McDonald\'s Corporation', 36.76, 0.86, 2.40, '9/1 12:00am'],
40
['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
41
['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
42
['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
43
['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
44
['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
45
['The Procter & Gamble Company', 61.91, 0.01, 0.02, '9/1 12:00am'],
46
['United Technologies Corporation', 63.26, 0.55, 0.88, '9/1 12:00am'],
47
['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
48
['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']
49
];
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
//构造grid
52![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var grid = new Ext.grid.GridPanel(
{
53
renderTo: "grid",
54![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
store: new Ext.data.ArrayStore(
{
55
fields: [
56![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: 'company' },
57![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: 'price', type: 'float' },
58![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: 'change', type: 'float' },
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: 'pctChange', type: 'float' },
60![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia' }
61
],
62
data:myData
63
}),
64
cm: cm,
65
stripeRows: true,
66
autoExpandColumn: 'company',
67
height: 350,
68
width: 600,
69
title: 'Array Grid'
70
});
71
}
)
在extjs中,Ext.grid.GridPanel表示一个grid,它需要一个json对象作为参数来进行配置,我们看看用到的配置:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
15
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
16
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
57
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
58
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
59
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
60
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
renderTo:指出grid构造出来之后要在哪里呈现,可以是一个元素的id,一个Dom结点或者一个Element对象,如果没有这个参数,就必须调用Ext.grid.GridPanel的render方法来呈现出grid。
stroe:可以简写为ds,以一个统一的接口提供数据给grid,,我们知道数据可能有很多种格式,除了我们用到的数组,还可以是json,xml等等,如果让grid负责来识别每一种数据格式显然不是一个好的设计思想,所以在extjs中有一个专门的类Ext.data.Store来负责数据的格式转换,这里我们用到它的子类ArrayStore,顾名思义,是专门针对数组来进行转换的。我们会专门有一个系列来讨论Ext.data命名空间下边的一些类,那时会对Store类进行深入的了解。现在我们只看看我们用到的fields字段,它是Ext.data.Field类的集合,该类有一个name属性,我们前边在ColumnModel里边置之不理的dataIndex就是引用了这个属性的值来对应列和Field之间的关系,type用来指出类型,默认是字符串类型,dateFormat则指出日期的格式。
cm:colModel的简写,这里放上我们前边构造好的ColumnModel对象就可以了。
stripeRows:是否显示条纹。
autoExpandColumn:自动扩充的列,该列会自动填充grid的空白空间。
height和width:grid的高度和宽度。
title:grid的标题。
现在我们运行一下看看效果,应该说这个grid还算漂亮,在列的名字上点击还能够进行排序,列的宽度可以自由拖动,位置也可以改变。按下ctrl键,我们可以选择多个行。当我们把鼠标移动到列名上,还可以看到出现一个倒立的小三角,点击出现个菜单,可以看到里边能对列进行排序,还可以隐藏列。不过我们也看到,日期的显示并不尽如人意,百分比依然是浮点数,而且我们通常用赤字表示亏损或者现在更为流行的说法负增长,如果在我们的grid里边,负增长也可以用赤字表现出来,效果应该会更好一些。extjs给我们提供了一个很方便的东西来实现我们的想法:renderer。在ColumnModel里边,我们可以给需要的列里边增加renderer来实现我们想要的效果。下边是改动后的ColumnModel:
1
//
构造列
2
var
cm
=
new
Ext.grid.ColumnModel([
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' }
,
4![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Price", width: 75, sortable: true, dataIndex: 'price' }
,
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Change", width: 75, sortable: true, dataIndex: 'change', renderer: change }
,
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange', renderer: pctChange }
,
7![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Last Updated", width: 120, sortable: true, dataIndex: 'lastChange',renderer:Ext.util.Format.dateRenderer("Y-m-d h:i") }
8
]);
9
10
//
Change列的renderer函数
11![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
change(val)
{
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (val > 0)
{
13
return '<span style="color:green;">' + val + '</span>';
14![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (val < 0)
{
15
return '<span style="color:red;">' + val + '</span>';
16
}
17
return val;
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
//
% Change列的renderer函数
21![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
pctChange(val)
{
22![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (val > 0)
{
23
return '<span style="color:green;">' + val + '%</span>';
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (val < 0)
{
25
return '<span style="color:red;">' + val + '%</span>';
26
}
27
return val;
28
}
renderer可以理解为一个“解释器”方法,它用来在显示数据之前转化数据。可以有三种方式来实现renderer:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
12
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
22
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
- 使用一个返回HTML标记的renderer函数
- Ext.util.Format类的一个属性,该属性提供一个renderer函数
- 一个包括renderer函数和作用域的对象
现在运行程序,可以看到我们希望的效果了:负增长用了赤字来表示,作为对比,正的增长用了绿颜色,时间也按照我们希望的格式显示了出来。
有的时候,我们还希望给每一行编号,这个实现起来也很容易,我们只要在ColumnModel的构造函数里边加上new Ext.grid.RowNumberer()就可以了:
1
var
cm
=
new
Ext.grid.ColumnModel([
2
new
Ext.grid.RowNumberer(),
//
实现自动编号
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ id: 'company', header: "Company", width: 160, sortable: true, dataIndex: 'company' }
,
4![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Price", width: 75, sortable: true, dataIndex: 'price' }
,
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Change", width: 75, sortable: true, dataIndex: 'change', renderer: change }
,
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "% Change", width: 75, sortable: true, dataIndex: 'pctChange', renderer: pctChange }
,
7![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ header: "Last Updated", width: 120, sortable: true, dataIndex: 'lastChange',renderer:Ext.util.Format.dateRenderer("Y-m-d h:i") }
8
]);
Ext.grid.GridPanel的配置里边还有两个常用的参数:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
viewConfig:我们可以用这个参数来对grid的界面进行一些设置,详细内容可以查看帮助文档。
selModel:可以简写为sm,选择模型,比如是选择单元格还是选择整个行,默认是选择行。