javascript - DataTables - Ignore empty moment date values when sorting -
i'm using datatables , trying ignore empty values on date column based momentjs when sorting.
e.g: 08 nov 2016 09:47 pm
http://codepen.io/anon/pen/qqbwva?editors=1010
currently have , sorts values expected contains empty cells.
$.fn.datatable.moment('dd mmm yyyy hh:mm a'); $('.table-data').datatable();
i've tried following custom sorting method posted here https://datatables.net/forums/discussion/1178/sorting-dates-handling-null-values
jquery.extend(jquery.fn.datatableext.osort, { 'date-asc': function (a, b) { var x = date.parse(a); var y = date.parse(b); if (x == y) { return 0; } if (isnan(x) || x < y) { return 1; } if (isnan(y) || x > y) { return -1; } }, 'date-desc': function (a, b) { var x = date.parse(a); var y = date.parse(b); if (x == y) { return 0; } if (isnan(y) || x < y) { return -1; } if (isnan(x) || x > y) { return 1; } } }); $('.table-data').datatable({ columndefs: [ { type: 'date', targets: -1 } ] });
but seems no difference. appreciate if sort issue.
update want move empty valued row last won't visible @ first place. http://jsfiddle.net/dnsl2oc4/2/
try instead:
$(document).ready(function() { //$.fn.datatable.moment('dd mmm yyyy hh:mm a'); jquery.extend(jquery.fn.datatableext.osort, { 'datenonstandard-asc': function (a, b) { var x = date.parse(a); var y = date.parse(b); if (x == y) { return 0; } if (isnan(x) || x < y) { return 1; } if (isnan(y) || x > y) { return -1; } }, 'datenonstandard-desc': function (a, b) { var x = date.parse(a); var y = date.parse(b); if (x == y) { return 0; } if (isnan(y) || x < y) { return -1; } if (isnan(x) || x > y) { return 1; } } }); $('.table-data').datatable({ columndefs: [ { type: 'datenonstandard', targets: -1 } ] }); });
if you're doing non-standard sorting it'd better clear ;-)
it seems want , think logic sound using moment overkill. might need test more, though...
Comments
Post a Comment