JQuery数据表中带有时间戳排序问题的日期

时间:2017-03-02 18:45:05

标签: javascript jquery datatables

我正在使用jquery datatable插件列出我的java spring MVC Web应用程序中的数据。表中的一个字段包含带时间戳的日期。在那,我一直在尝试对数据元素进行排序。当我尝试在包含带时间戳的日期的字段中对元素进行排序时,排序不起作用。

我正在使用以下jquery进行数据表初始化

 $('.swcm-dt-basic').dataTable( {
        "responsive": true,
        "order": \[\],
        "language": {
            "paginate": {
              "previous": '<i class="swcm-psi-arrow-left"></i>',
              "next": '<i class="swcm-psi-arrow-right"></i>'
            }
        }
    } );

下图显示了该问题 Error in Sorting

3 个答案:

答案 0 :(得分:0)

如果您正在寻找一个简单的解决方案来排序该列,我不确定您的问题,请使用yyyymmdd hh:mm:ss格式。 但如果您具体针对mm / dd / yyyy hh:mm:ss格式,请尝试以下建议: https://stackoverflow.com/a/25359251/3483409 https://stackoverflow.com/a/33568433/3483409

答案 1 :(得分:0)

解决方案1 ​​

对包含可排序时间戳的data-order元素使用td属性。例如:

<td data-order="2016-12-02 21:28:41">12/02/2016 21:28:41</td>

请参阅this example以获取代码和演示。

解决方案2

sorting plugins使用datetime-moment表示任何日期格式,如果您的日期采用DD/MM/YYYY HH:MM:SS格式,请date-euro

如果您的日期位于MM/DD/YYYY HH:MM:SS,则可以使用以下代码:

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "date-us-pre": function ( a ) {
        var x;

        if ( $.trim(a) !== '' ) {
            var frDatea = $.trim(a).split(' ');
            var frTimea = (undefined != frDatea[1]) ? frDatea[1].split(':') : [00,00,00];
            var frDatea2 = frDatea[0].split('/');
            x = (frDatea2[2] + frDatea2[0] + frDatea2[1] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
        }
        else {
            x = Infinity;
        }

        return x;
    },

    "date-us-asc": function ( a, b ) {
        return a - b;
    },

    "date-us-desc": function ( a, b ) {
        return b - a;
    }
} );

$(document).ready(function(){
   $('#example').dataTable( {
      columnDefs: [
         { type: 'date-us', targets: 0 }
      ]
   } );
}

答案 2 :(得分:0)

你可以使用这样的东西。

$('#example').dataTable( {
     columnDefs: [
       { type: 'de_datetime', targets: 0 },
       { type: 'de_date', targets: 1 }
     ]
  } );

有关详细信息,请访问https://datatables.net/plug-ins/sorting/date-de