简体   繁体   English

jQuery DataTable日期顺序

[英]JQuery DataTable Date Order

I have two problems. 我有两个问题。

In my table, there is a created date column and I am soring by this date. 在我的表格中,有一个创建的日期列,我正在按此日期进行痛楚。 Unfortunately it sorts as string, instead of date. 不幸的是,它按字符串而不是日期排序。 (I'm using nodejs). (我正在使用nodejs)。

it had to be 一定是

21.05.2019 2019年5月21日

22.04.2109 2109年4月22日

and my code : 和我的代码:

"order": [[0,"desc"],[ 1, "desc" ],[ 7, "desc" ]],

I've tried a plugin as well 我也尝试过插件

"columnDefs": [                                                
                    {
                        "targets": [ 1 ],
                        "visible": true,
                        "searchable": false
                    },
                    {
                        "targets":7,
                        "type": "date-de"
                    },


<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/
jquery.dataTables.js" type="text/javascript"></script>

<script src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/date-de.js"
 type="text/javascript"></script>

$(document).ready(function() {
   jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "extract-date-pre": function(value) {
        var date = $(value, 'span')[0].innerHTML;
        date = date.split('/');
        return Date.parse(date[1] + '/' + date[0] + '/' + date[2])
    },
    "extract-date-asc": function(a, b) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "extract-date-desc": function(a, b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});

but did not work... 但是没有用...

As I can see the date is in: DD.MM.YYYY format. 如我所见,日期的格式为: DD.MM.YYYY To parse the date you need to split by . 要解析日期,您需要除以. not / . 不是/

"extract-date-pre": function(value) {
    var date = $(value, 'span')[0].innerHTML;
    date = date.split('.').reverse().join('-');
    return Date.parse(date);
}

Please, only one question at once, remove the select part and create a new question with it. 请一次只回答一个问题,删除选择的部分并用它创建一个新问题。

Your first question: In order to the datatable to order the date in the right way, you must print the date as Year-Month-Day in a hidden element. 您的第一个问题:为了使数据表以正确的方式对日期进行排序,您必须在隐藏的元素中将日期打印为Year-Month-Day。

Example, I don't know how you populate your datetable, but in plain HTML you will do something like: 例如,我不知道如何填充日期表,但是在纯HTML中,您将执行以下操作:

<tr>
 <td>
   <span style="display:none">2019-05-22</span>
    22-05-2019
 </td>
</tr>

So when the datatable orders the column, it picks the value inside the hidden span that now can be ordered as a simple string. 因此,当数据表对列进行排序时,它会选择隐藏范围内的值,该值现在可以作为简单字符串进行排序。

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

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