Reputation: 4320
I'm using jQuery DataTable and the first column contain a custom date range. For ex: 22 Oct 2015 - 22 Nov 2015
where 22 Oct 2015
is the start date and 22 Nov 2015
is the end date. I want the sorting functionality to work on start date.
Let me explain a scenario: Let say I have 4 rows in DataTable and the first column date values are:
I want the sorting result in following format:
How to do this!
Upvotes: 1
Views: 921
Reputation: 85528
You must implement your own sorting plugin to do that. It is quite simple :
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-range-pre": function ( a ) {
a = a.split('-')[0].trim();
return Date.parse(a);
},
"date-range-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-range-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
The above simply extracts the first date by splitting the string and then do the sorting on that. There is no error handling, i.e care taking of empty strings and so on - you can do that yourself. Usage :
var table = $('#example').DataTable({
columnDefs: [
{ type: 'date-range', targets: 0 }
]
})
demo -> http://jsfiddle.net/gs5syg70/
Upvotes: 2