如何按日期对DataTables行进行排序? [英] How can I sort my DataTables row by date?

查看:123
本文介绍了如何按日期对DataTables行进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按日期对列进行排序:

I want to sort my column by date:

var table = $('#table').DataTable({
   "order": [[0, "desc"]],
});

但这是我的结果:

29.06.17
27.06.17
26.06.17
22.08.17
18.10.17
15.09.17

我期望的是:

18.10.17
15.09.17
22.08.17    
29.06.17
27.06.17
26.06.17

六月,然后是八月,然后是九月,然后是十月....

June, then August, then September and then October....

我也测试过:

"columnDefs": [
   { "type": "date-dd.mm.yy", targets: 0 }
],

但这并没有改变任何东西.

But this didn't change anything.

推荐答案

dataTables date类型使用Data.parse(),它仅支持一组有限的日期格式.欧洲风格dd.mm.yy无法解析,因此日期按字母排序.

dataTables date type uses Data.parse() which only supports a limited set of date formats. European style dd.mm.yy is not parseable thus the dates is alpha sorted.

您可以处理 数据属性 ,即在每列中添加一个data-sort="10/18/17",但我认为创建一个返回有效日期的小插件会更容易:

You can deal with data attributes, i.e adding a data-sort="10/18/17" to each column, but I think it is easier to create a small plugin that return valid dates :

$.extend( $.fn.dataTableExt.oSort, {
  "jarla-date-pre": function(a) {
     a = a.split('.');
     return new Date(a[1]+'/'+a[0]+'/'+a[2])
   }
});

像这样使用它:

columnDefs: [
  { type: 'jarla-date', targets: 0 }   
]

演示-> http://jsfiddle.net/vad94dcs/

demo -> http://jsfiddle.net/vad94dcs/

这篇关于如何按日期对DataTables行进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆