转载自 http://blog.sina.com.cn/s/blog_62276c5a01015ah8.html
要实现中文排序的功能,需要重写Ext.data.Store的applySort函数,可以把它放在ext-all.js文件的最后,或者是放到 HTML页面的最上面,要在ExtJS初始化之后,实际代码调用之前执行,这样重写的方法才能起作用,中文排序也就实现了。完整的实现代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>解决中文排序问题</title> <link rel="stylesheet" type="text/css" href="../ext-3.4.0/resources/css/ext-all.css"></link> <script type="text/javascript" src="../ext-3.4.0/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="../ext-3.4.0/ext-all.js"></script> <script type="text/javascript"> Ext.data.Store.prototype.applySort=function(){//重载applySort 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]); //添加:修复汉字排序异常的Bug if(typeof(v1)=="string"){//若为字符串 return v1.localeCompare(v2);//则localeCompare比较汉字字符串,Firefox与IE均支持 }//添加结束 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); } } }; </script> <script type="text/javascript"> Ext.onReady(function(){ var cum = new Ext.grid.ColumnModel([ {header:"项目ID",dataIndex:'project_id'}, {header:"项目名称",dataIndex:'project_name'}, {header:"部门",dataIndex:'project_dept_no'}, {header:"项目经理",dataIndex:'project_manager'}, {header:"开发经理",dataIndex:'project_leader'} ]); var cumdata=[ ['1','教学管理系统','001','李润','田成功'], ['2','留言系统','002','光明','周爱卿'], ['3','图书管理系统','008','毛小强','黄岩'], ['4','校信通','003','王磊','郭强'] ]; var store=new Ext.data.Store({ proxy:new Ext.data.MemoryProxy(cumdata), reader:new Ext.data.ArrayReader({},[ {name:'project_id'}, {name:'project_name'}, {name:'project_dept_no'}, {name:'project_manager'}, {name:'project_leader'} ]), sortInfo:{field:'project_name',direction:'ASC'}//设置project_name字段,按升序排列 }); store.load(); var cumgrid =new Ext.grid.GridPanel({ renderTo:'cumGrid', store:store, height:200, width:500, colModel:cum }); }); </script> </head> <body> <div id="cumGrid"></div> </body> </html>