简体   繁体   English

日期的数据表排序不适用于FF

[英]Datatable sorting for date not working on FF

I'm trying to use datatable.js and moment.js for sorting dates on a table. 我正在尝试使用datatable.js和moment.js对表上的日期进行排序。 It's working well on Chrome browser but not working on Firefox. 它在Chrome浏览器上运行良好,但在Firefox上却无法运行。

Here's the code for review. 这是审查代码。

$.fn.dataTable.moment('MM, DD, YY');
$('#dataTable').DataTable( {
    info: false,
    paging: false,
    searching: false,
});

Also here's the link for review the result https://jsfiddle.net/8phz4rn2/24/ 另外这里是查看结果的链接https://jsfiddle.net/8phz4rn2/24/

Any ideas how this can be resolved? 有什么想法可以解决吗?

Your direction is correct, in regards that you are trying to parse the date from a given string and display it in a table. 您正在尝试从给定的字符串解析日期并将其显示在表格中,因此您的指示是正确的。 The problem here is that DataTable is not aware of any moment operations you did before, so it is displaying the row values as original strings. 这里的问题是DataTable不知道您之前做过的任何moment操作,因此它将行值显示为原始字符串。 So the column is sorted by strings, rather than dates. 因此,该列按字符串而不是日期排序。

You must convert the strings to dates inside the DataTable render function, just before it displays it to the screen. 您必须在将字符串显示到屏幕之前,将字符串转换为数据表render函数中的日期。

$('#dataTable').DataTable({
  info: false,
  paging: false,
  searching: false,

  "columnDefs": [{
    "targets": 0,    // column index, 0 means the first column
    "render": function(data) {
      return moment(data, 'MM/DD/YY').format('MM/DD/YY');
    }
  }]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM