ExtJs中解決中文排序問題

來源:互聯網
上載者:User
ExtJs中解決中文排序問題

(2012-07-29 19:48:50)

轉載▼
標籤:

雜談
分類:

技術專區

要實現中文排序的功能,需要重寫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>

 

原文地址 :http://blog.sina.com.cn/s/blog_62276c5a01015ah8.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.