简体   繁体   English

jQuery datatable日期排序问题

[英]jquery datatable date sorting issue

I am using datatable plugin version 1.8.2 for displaying table on my webpage. 我正在使用数据表插件版本1.8.2在我的网页上显示表。

It is working fine except. 一切正常。 It is not sorting dates properly, it is showing "invalid date" in Date object . 它没有正确排序日期,而是在Date对象中显示了“无效日期” below is my code snippet. 以下是我的代码段。

     $(document).ready(function() {

                jQuery.fn.dataTableExt.oSort['usdate-asc']  = function(a,b) {
    /*
    a and b are <div> tag with date
    */
                    var texta = ($(a).text()).toString(); // Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format
                    var textb = ($(b).text()).toString();// Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format

                    var usDatea = new Date(Date.parse(texta)); // Here it is showing "invalid date"
                    var usDateb = new Date(Date.parse(textb)); 

                    return ((usDatea < usDateb) ? -1 : ((usDatea > usDateb) ?  1 : 0));
                };

                jQuery.fn.dataTableExt.oSort['usdate-desc'] = function(a,b) {
 /*
    a and b are <div> tag with date
    */
                    var texta = ($(a).text()).toString(); //same as above
                    var textb = ($(b).text()).toString(); //same as above

                    var usDatea = new Date(Date.parse(texta));  //same as above
                    var usDateb = new Date(Date.parse(textb));  //same as above

                    return ((usDatea < usDateb) ? 1 : ((usDatea > usDateb) ?  -1 : 0));
                };

                $('#tablegridname').dataTable( {
                    "sPaginationType": 'full_numbers',
                    "bJQueryUI": true,
                    "iDisplayLength": 50,
                    "aLengthMenu":[50,100,500,1000],
                    "aaSorting": [[ 4, 'desc' ]],
                    "aoColumns": [null, null, null, null, {"sType": "usdate"}]
                } );

            });
            });

Try this fiddle: 试试这个小提琴:

http://jsfiddle.net/82vh6mp2/ http://jsfiddle.net/82vh6mp2/

It uses this simple function: 它使用以下简单功能:

    function parseDateForSort(d)
    {
       return d.substring(6,10) + d.substring(0,2) + 
          d.substring(3,5) + d.substring(20) + 
          d.substring(11,19);
    }

The function takes advantage of the fact that, luckily, PM comes after AM alphabetically; 该功能利用了以下事实:幸运的是,PM按字母顺序排在AM之后; hence the "d.substring(20)" in the middle of the string. 因此,在字符串中间的“ d.substring(20)”。 So we have YYYYMMDD[AM or PM]HH:MM:SS. 因此,我们有YYYYMMDD [AM或PM] HH:MM:SS。

In your code you can get rid of Date.parse, and replace your return with: 在您的代码中,您可以删除Date.parse,然后将返回值替换为:

  usDatea = parseDateForSort(texta);
  usDateb = parseDateForSort(textb);

  return ((usDatea < usDateb) ? -1 : ((usDatea > usDateb) ?  1 : 0));

Good luck. 祝好运。

Addendum: 附录:

You can create your own sort type, and then specify the column thusly: 您可以创建自己的排序类型,然后指定列:

$.extend($.fn.dataTableExt.oSort, {
    "date-us-pre": function (v) {
        return parseDateForSort(v);
    },

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

And then in your .dataTable call include "aoColumnDefs": [ { "sType":"date-us", "aTargets":[6] } ] 然后在您的.dataTable调用中包括"aoColumnDefs": [ { "sType":"date-us", "aTargets":[6] } ]

or whatever column number it is instead of 6. 或任何列号,而不是6。

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

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