jquery - datatable, how to display table sorted by a column in this date format? -
i display table sorted column called createdat
has format :
2015-03-09t14:46:57.678-04:00
the above date format can parsed date.parse()
. so, if have columns :
<tbody> <tr><td>2015-03-09t14:46:54.678-04:00</td></tr> <tr><td>2015-03-09t14:46:55.678-04:00</td></tr> <tr><td>2015-03-09t14:46:56.678-04:00</td></tr> <tr><td>2015-02-09t14:46:57.678-04:00</td></tr> <tr><td>2015-02-09t14:46:56.678-04:00</td></tr> <tr><td>2015-03-10t02:46:57.678-04:00</td></tr> <tr><td>2015-03-11t14:41:57.678-04:00</td></tr> <tr><td>2015-03-09t12:46:57.678-04:00</td></tr> <tr><td>2015-03-09t01:46:57.678-04:00</td></tr> </tbody>
you can make simple custom sorting plugin , use particular column :
$.fn.datatableext.osort['time-date-sort-pre'] = function(value) { return date.parse(value); }; $.fn.datatableext.osort['time-date-sort-asc'] = function(a,b) { return a-b; }; $.fn.datatableext.osort['time-date-sort-desc'] = function(a,b) { return b-a; }; var table = $('#example').datatable({ columndefs : [ { type: 'time-date-sort', targets: [0] } ] });
demo -> http://jsfiddle.net/kkrqzvx4/
Comments
Post a Comment