DataTable自定义排序

使用JQ DataTable 的时候,希望某列数据可以进行自定义排序,操作如下:(以中文排序和百分比排序为例)

1:定义排序类型:

 
  1. //百分率排序  
  2. jQuery.fn.dataTableExt.oSort['number-fate-asc']  = function(s1,s2) {  
  3.     s1 = s1.replace('%','');  
  4.     s2 = s2.replace('%','');  
  5.     return s1-s2;  
  6. };  
  7.   
  8. jQuery.fn.dataTableExt.oSort['number-fate-desc'] = function(s1,s2) {  
  9.     s1 = s1.replace('%','');  
  10.     s2 = s2.replace('%','');  
  11.     return s2-s1;  
  12. };  
  13. //中文排序  
  14. jQuery.fn.dataTableExt.oSort['chinese-string-asc']  = function(s1,s2) {  
  15.     return s1.localeCompare(s2);  
  16. };  
  17. jQuery.fn.dataTableExt.oSort['chinese-string-desc'] = function(s1,s2) {  
  18.     return s2.localeCompare(s1);  
  19. };   



2:指定排序的列:

 
    1. $('#flexme1').dataTable({  
    2.     "aoColumns": [  
    3.         null,  
    4.         { data: 'area', "sType": "chinese-string" },//中文排序列  
    5.         null,  
    6.         { data: 'percent', "sType": "number-fate" },//百分率排序  
    7.         null,  
    8.         null  
    9.     ]  
    10. });