数据日期排序dd / mm / yyyy问题 [英] Datatable date sorting dd/mm/yyyy issue

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

问题描述

我正在使用一个名为数据表的JQuery插件。

I am using a Jquery plugin called datatables

它的梦幻般的,但是我无法按照dd / mm / yyyy格式正确排序日期。

Its fantastic, however I cannot get the dates to sort correctly according to the dd/mm/yyyy format.

我已经看过他们的支持格式,但没有这些修补程序似乎有效。

I have looked at their support formats but none of these fixes seem to work.

有人可以帮助我吗?

推荐答案

jQuery解决方案



这里是工作jQuery解决方案

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function ( a ) {
    var ukDatea = a.split('/');
    return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
},

"date-uk-asc": function ( a, b ) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"date-uk-desc": function ( a, b ) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

将上述代码添加到脚本中,并将具有Date值的特定列设置为 {sType:date-uk} ,其他为null,见下文:

Add the above code to script and set the specific column with Date values with { "sType": "date-uk" } and others as null, see below:

$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [
            null,
            null,
            null,
            null,
            { "sType": "date-uk" },
            null
        ]
    });
    });



CSS解决方案



快速解决方案,您可以在每一行中以特定格式(YYYYMMDD)附加实际的DateTime值,并使用CSS隐藏它,它将允许您对该列进行排序,而不会发生任何JavaScript更改。

CSS Solution

If you want a quick solution you can just append the actual DateTime value in each row in the specific format (YYYYMMDD) and make it hidden using CSS, it will allow you to sort that column without any javascript change.

以下是工作CSS解决方案

HTML

<td><span class='hide'>YYYYMMDD</span>DD/MM/YYYY</td>

CSS

.hide {
    display:none; 
}

这篇关于数据日期排序dd / mm / yyyy问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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