How to sort datatables with date in descending order

前端 未结 14 1879
我寻月下人不归
我寻月下人不归 2020-12-29 11:11

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 stru

相关标签:
14条回答
  • 2020-12-29 11:22

    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

    0 讨论(0)
  • 2020-12-29 11:24

    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" ]] 
        }); 
    
    0 讨论(0)
  • 2020-12-29 11:24

    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/

    0 讨论(0)
  • 2020-12-29 11:26
    <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

    0 讨论(0)
  • 2020-12-29 11:28

    //working here code

    $('#table').DataTable({
       columnDefs: [ { type: 'date', 'targets': [3] } ],
       order: [[ 3, 'desc' ]],          
    });
    
    0 讨论(0)
  • 2020-12-29 11:28
                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));
                  }
                }); 
    
    0 讨论(0)
提交回复
热议问题