如何在PHP中的datatable插件中对datetime字段进行排序?

时间:2016-12-05 09:57:39

标签: javascript sorting datatables

我习惯于在datatable插件中对日期和时间字段进行排序。但它不按照9,8,7,... 12,11,10的顺序排序。如果我使用一些自定义代码进行排序,我会工作,但它不会对某些数据进行排序

自定义代码:

HTML和JS

<td><?=  $date ? $date : '-' ?></td>

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function(a) {
var a = a.split('m')[0];
a=a+'m';
a = a.slice(0, -2) + ' ' + a.slice(-2);
var date = Date.parse(a);
return typeof date === 'number' ? date : -1;
}

数据表:

$('#id').DataTable({
    "paging": true,
    "ordering": true,
    "aoColumns": [
       { "bSortable": false },  
       { sType: 'uk_date' },
       null
    ],
    "order": [[0, 'desc']],
});

1 个答案:

答案 0 :(得分:0)

最后,我得到了答案,感谢大家的意见

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function(a) {
var a = a.split('m')[0];
if(a == '-'){
        return -1;
    }
a=a+'m';
a = a.slice(0, -2) + ' ' + a.slice(-2);
var date = Date.parse(a);
return typeof date === 'number' ? date : -1;
}