1

我正在使用 jquery 数据表,并且我有一列包含这些值的组合。一个日期、一个字符串“A”和另一个字符串“B”。当我单击该列时,我希望该列首先出现“A”,然后按日期排序,然后“B”出现。有没有办法实现这个功能?

4

2 回答 2

1

客户端 :

$.extend($.fn.DataTable.ext.oSort, {
   /*
   *
   *custom_date sorting
   */
    "custom_date": function (a) {
        return a;
    },
    "custom_date-asc": function (x, y) {
        var x = getCustomEuroDateValue(x);
        var y = getCustomEuroDateValue(y);
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    },
    "custom_date-desc": function (x, y) {
        var x = getCustomEuroDateValue(x);
        var y = getCustomEuroDateValue(y);
        return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    },
});
function getCustomEuroDateValue(strDate) {
   // return a string by spiting and merging the date as you need
   // B A 02/11/2013 to A20131102B
   //
   // Example : 
   // var frDatea = $.trim(strDate);
   // var frDatea1 = frDatea.split(' ');
   // var frDatea2 = frDatea1[2].split('/');
   // var x = (frDatea1[1] + frDatea2[2] + frDatea2[1]+ frDatea2[0] + frDatea1[0]);
   // return x;


}

然后将其添加到您的 otable 属性中

"aoColumns": [ {  }, { },{ "sType": "custom_date" },{ } ], 
于 2013-11-13T08:26:16.963 回答
0

如果使用客户端,请检测 DataTables 排序侦听器,然后附加您自己的调用http://www.datatables.net/ref#fnSort

将服务器端与数据库一起使用更容易实现,因为您始终可以在 select 语句中包含静态 order by 子句作为辅助排序选项

如果需要更多帮助,请发布更多代码

于 2013-05-23T20:01:11.373 回答