<html>
<head>
<script type="text/javascript">
Ext.onReady(function(){
//处理中文排序(这段代码是重写Ext.data.Store的applySort函数,可以把这个加到ext-all.js文件的最后,或放到html页面的最上边)
Ext.data.Store.prototype.applySort=function(){
if(this.sortInfo && !this.remoteSort){
var s=this.sortInfo,f=s.field;
var st=this.fields.get(f).sortType;
var fn=function(r1,r2){
var v1=st(r1.data[f]),v2=st(r2.data[f]);
if(typeof(v1)=="string"){
return v1.localeCompare(v2);
}
return v1>v2?1:(v1<v2?-1:0);
};
this.data.sort(s.direction,fn);
if(this.snapshot && this.snapshot!=this.data){
this.snapshot.sort(s.direction,fn);
}
}
}
//定义列
var cm = new Ext.grid.ColumnModel([
{header:'编号',dataIndex:'id',sortable:true,width:20},
{header:'名称',dataIndex:'name',sortable:true,width:80},
{header:'描述',dataIndex:'descn',sortable:true,width:200}
]);
//数据定义
var data=[
['1','啊','descn1'],
['2','啵','descn2'],
['3','呲','descn3'],
['4','嘚','descn4'],
['5','咯','descn5']
];
//数据源定义(数据的转换)
var ds=new Ext.data.Store({
proxy:new Ext.data.MemoryProxy(data),
reader:new Ext.data.ArrayReader({},[
{name:'id'},{name:'name'},{name:'descn'}
]),
sortInfo:{field:"name",direction:"ASC"}
});
ds.load();//加载数据
//装配
var grid=new Ext.grid.GridPanel({
renderTo:'hello',
ds:ds,
cm:cm,
viewConfig:{ //告诉视图层重新计算所有列宽,填充grid
forceFit:true
},
autoHeight:true
});
});
</script>
</head>
<body>
<div id="hello"></div>
</body>
</html>