How to sort datatables with date in descending order

做~自己de王妃 提交于 2020-05-24 20:55:30

问题


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


回答1:


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

<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.




回答2:


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 :

<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




回答3:


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,

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



回答4:


Please refer to this pen: 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

To sort the table by default use:

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



回答5:


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

$('#exemple').DataTable({

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



回答6:


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/




回答7:


//working here code

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



回答8:


<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




回答9:


Default sorting in Datatables:

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

You can use asc for ascending order. And 3 means, 4th column is going to be ordered default.




回答10:


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



回答11:


Just add "type":"date" directly in the columns like { "data": "MyDateField", "type":"date" }.



来源:https://stackoverflow.com/questions/37814191/how-to-sort-datatables-with-date-in-descending-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!