jQuery 如何按日期对数据表进行降序排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37814191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:08:39  来源:igfitidea点击:

How to sort datatables with date in descending order

jqueryhtmldatatables

提问by Sujitha M

I wish to show the records using datatables with default ordering based on one of my rows with date & time in descending order. Please help me in editing the jquery structure for that

我希望使用数据表显示记录,默认排序基于我的日期和时间按降序排列的行之一。请帮我编辑 jquery 结构

enter image description here

在此处输入图片说明

回答by joan16v

The simpliest way is to add a hidden timestamp before the date in every TD tag of the column, for example:

最简单的方法是在列的每个 TD 标签中的日期之前添加一个隐藏的时间戳,例如:

<td class="sorting_1">
    <span style="display:none;">1547022615</span>09/01/2019  09:30
</td>

With the default string ordering, a timestamp would order the column the way you want and it will not be shown when rendered in the browser.

使用默认的字符串排序,时间戳会按照您想要的方式对列进行排序,并且在浏览器中呈现时不会显示。

回答by ReSedano

I had same problem. I used date-eu sorting plugin to sort dates in the format DD/MM/YY and I included the following JS file :

我有同样的问题。我使用 date-eu 排序插件以 DD/MM/YY 格式对日期进行排序,并包含以下 JS 文件:

<script src="//cdn.datatables.net/plug-ins/1.10.11/sorting/date-eu.js" type="text/javascript"></script>

This worked for me.

这对我有用。

$('#exemple').DataTable({
    "order": [[ 3, "desc" ]], //or asc 
    "columnDefs" : [{"targets":3, "type":"date-eu"}],
});

Read also this post on stackoverflow: Sorting date in datatable

另请阅读有关 stackoverflow 的这篇文章:在数据表中排序日期

回答by Harish Patidar

I got the solution with the sorting of date. Just add type as 'date' and in targets, you have pass column number(count start from 0) with datatable options. And set 'order' with column number and type of format. See below code,

我得到了日期排序的解决方案。只需将类型添加为“日期”,在目标中,您就可以通过数据表选项传递列号(从 0 开始计数)。并使用列号和格式类型设置“顺序”。看下面的代码,

columnDefs: [ { type: 'date', 'targets': [4] } ],
order: [[ 4, 'desc' ]]

回答by ThivankaW

I know this is an old thread. but you can basically use "aaSorting"

我知道这是一个旧线程。但你基本上可以使用“asorting”

$('#exemple').DataTable({

    "aaSorting": [[3,'desc']],
});

回答by Plasebo

Please refer to this pen: https://codepen.io/arnulfolg/pen/MebVgx

请参考这支笔:https: //codepen.io/arnulfolg/pen/MebVgx

It uses //cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js and //cdn.datatables.net/plug-ins/1.10.12/sorting/datetime-moment.js for sorting datatable

它使用 //cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js 和 //cdn.datatables.net/plug-ins/1.10.12/sorting/datetime-moment。用于对数据表进行排序的js

To sort the table by default use:

默认情况下要对表格进行排序,请使用:

$.fn.dataTable.moment('DD/MM/YY');
$('#example').DataTable({ 
       "order": [[ 3, "desc" ]] 
    }); 

回答by Forrest

This was the answer for me:

这是我的答案:

<td data-order=<fmt:formatDate pattern = "yyyy-MM-dd" value = "${myObject.myDate}" />>${myObject.myDate}</td>

more details, here in the html5 section: https://datatables.net/manual/data/

更多细节,在 html5 部分:https: //datatables.net/manual/data/

回答by mind

<td class="sorting_1">
    <span style="display:none;">201909010930</span>09/01/2019  09:30
</td>

Format your date in yyyyMMddHHmm. This will be your sortable timestamp. Then hide the formatted date using display none. This is actually a further explanation of the answer of joan16v

以 yyyyMMddHHmm 格式格式化您的日期。这将是您的可排序时间戳。然后使用 display none 隐藏格式化的日期。这其实是对joan16v的回答的进一步解释

回答by Brijesh

//working here code

//在这里工作代码

$('#table').DataTable({
   columnDefs: [ { type: 'date', 'targets': [3] } ],
   order: [[ 3, 'desc' ]],          
});

回答by R Tiwari

            Here the code:


           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));
              }
            }); 

回答by Emre Bolat

Default sorting in Datatables:

数据表中的默认排序:

$(document).ready(function() { 
    $('#example').DataTable({ 
        "order": [[ 3, "desc" ]] 
    }); 
});

You can use ascfor ascending order. And 3means, 4thcolumn is going to be ordered default.

您可以使用asc进行升序。和3种手段,第4列将被责令默认。