如何按日期对我的jquery数据表排序? [英] How can I sort my jquery datatable by date?

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

问题描述

这是我的数据表的内容:

This is the content of my datatable:

05.06.2020 10:46
14.08.2020 11:18
17.09.2020 01:24
17.09.2020 04:42
20.08.2020 01:47
20.08.2020 04:37
22.09.2020 02:05
23.09.2020 13:52
28.04.2020 02:46
28.04.2020 12:00

排序不正确,因为我希望列以最新日期开始.这是我的方法:

The sorting is not working correctly, as I am expecting the colums start with the newest date. This is my approach:

var table = $('.table').DataTable({
    "data":{{ data|raw }},
    "order": [[ 0, "asc" ]],
  ....

   'createdCell':  function init(cell, cellData, rowData, rowIndex, colIndex) {
       var unixTimestamp = moment(cellData, 'DD.MM.YYYY HH:MM').unix();
        $(cell).html(cellData);
        $(cell).attr('data-order', unixTimestamp);
   }

但是排序仍然是错误的.我尝试过此解决方案如何按日期对DataTables进行排序?,但它对我不起作用.

But still the sorting is wrong. I tried this solution How can I sort my DataTables row by date? but it did not work for me.

推荐答案

这是对我有用的解决方案:

This is a solution that worked for me:

"order": [[ 0, "desc" ]],
"columnDefs": [
  {
        "type": "date",
        "render": function (data, type, row, meta) {
          var DateCreated = moment(data, 'DD.MM.YYYY HH:mm').format('YYYY-MM-DD');
          return DateCreated;
        },
        "targets": 0
      },
  ]

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

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