使用JQ DataTable 的时候,希望某列数据可以进行自定义排序,操作如下:(以中文排序和百分比排序为例)
1:定义排序类型:
- //百分率排序
- jQuery.fn.dataTableExt.oSort['number-fate-asc'] = function(s1,s2) {
- s1 = s1.replace('%','');
- s2 = s2.replace('%','');
- return s1-s2;
- };
- jQuery.fn.dataTableExt.oSort['number-fate-desc'] = function(s1,s2) {
- s1 = s1.replace('%','');
- s2 = s2.replace('%','');
- return s2-s1;
- };
- //中文排序
- jQuery.fn.dataTableExt.oSort['chinese-string-asc'] = function(s1,s2) {
- return s1.localeCompare(s2);
- };
- jQuery.fn.dataTableExt.oSort['chinese-string-desc'] = function(s1,s2) {
- return s2.localeCompare(s1);
- };
2:指定排序的列:
- $('#flexme1').dataTable({
- "aoColumns": [
- null,
- { data: 'area', "sType": "chinese-string" },//中文排序列
- null,
- { data: 'percent', "sType": "number-fate" },//百分率排序
- null,
- null
- ]
- });