简体   繁体   中英

Datatable date sorting dd-mm-yyyy issue

<table id="example">
   <thead>
      <tr>
         <th>Name</th>
         <th>Date</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>A</td>
         <td>30-08-2015</td>
      </tr>
      <tr>
         <td>B</td>
         <td>29-08-2015</td>
      </tr>
      <tr>
         <td>C</td>
         <td>04-09-2015</td>
      </tr>
      <tr>
         <td>D</td>
         <td>01-09-2015</td>
      </tr>
      <tr>
         <td>E</td>
         <td>14-09-2015</td>
      </tr>
   </tbody>
</table>

And my script:

$.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));
    }
};  
var table = $('#example');
var oTable = table.dataTable({
    "columns": [
        null,
        { "type": "date-uk" }
    ],
    "order": [[1,'asc']],
});

I want show sort by date DESC, how to fix it?

use HTML5 data-* attributes - cell data .

With the attribute data-order and as value the timestamp of date (PHP : date("U") ). Exemple :

<td data-order="1231718400">Mon 12th Jan 09</td>

SOLUTION

Use the code below:

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

$(document).ready(function (){
   var table = $('#example');
   var oTable = table.dataTable({
      "columns": [
         null,
         { "type": "date-uk" }
      ],
      "order": [[1, 'desc']]
   });
});

DEMO

See this jsFiddle for code and demonstration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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