Custom Sort with Date Plus a String
Custom Sort with Date Plus a String
jeff41
Posts: 1Questions: 0Answers: 0
Long time listener, first time caller...
I am trying to write my own custom sort function, after using the ColumnFilter Plug-in successfully a couple times. The customer sort should sort a column that has a date mixed with a other text, e.g., age at the date.
I have a table with a row that looks like so:
[code]
Jasmine Krajci
Adrian Krajcik, Aubrey Krajcik
07/09/2011
student discount
new customer
strawberry
08/31/2002
<!-- Would like to sort by the date at the beginning of this cell -->
09/26/2000 10 years old
[/code]
I have set up my custom filter and Datatables initialization like this:
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-mixed-pre": function ( a ) {
// remove leading spaces and extract date string
var dateString = a.replace(/^\s*/, '').substring(0, 10).split('/');
return (dateString[2] + dateString[0] + dateString[1]) * 1;
},
"date-mixed-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-mixed-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
$(document).ready(function(){
// set up the datatables
customerTable = $('#table-purchase').dataTable( {
"bStateSave": true,
"bPaginate": false,
"aaSorting": [ [4,'asc'], [5,'asc'] ],
"aoColumns": [
null,
null,
null,
null,
null,
null,
{ "sType": "date-mixed" } ,
],
"oLanguage": {
"sSearch": ""
}
});
[/code]
Datatables filters normally on all of the columns but this one. In other words, the sort arrows flip up and down but nothing happens. There are no errors in the browser console. Any suggestions for fixing or debugging?
Thank you to Allan and everyone for sustaining this very useful code and documentation.
I am trying to write my own custom sort function, after using the ColumnFilter Plug-in successfully a couple times. The customer sort should sort a column that has a date mixed with a other text, e.g., age at the date.
I have a table with a row that looks like so:
[code]
Jasmine Krajci
Adrian Krajcik, Aubrey Krajcik
07/09/2011
student discount
new customer
strawberry
08/31/2002
<!-- Would like to sort by the date at the beginning of this cell -->
09/26/2000 10 years old
[/code]
I have set up my custom filter and Datatables initialization like this:
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-mixed-pre": function ( a ) {
// remove leading spaces and extract date string
var dateString = a.replace(/^\s*/, '').substring(0, 10).split('/');
return (dateString[2] + dateString[0] + dateString[1]) * 1;
},
"date-mixed-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-mixed-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
$(document).ready(function(){
// set up the datatables
customerTable = $('#table-purchase').dataTable( {
"bStateSave": true,
"bPaginate": false,
"aaSorting": [ [4,'asc'], [5,'asc'] ],
"aoColumns": [
null,
null,
null,
null,
null,
null,
{ "sType": "date-mixed" } ,
],
"oLanguage": {
"sSearch": ""
}
});
[/code]
Datatables filters normally on all of the columns but this one. In other words, the sort arrows flip up and down but nothing happens. There are no errors in the browser console. Any suggestions for fixing or debugging?
Thank you to Allan and everyone for sustaining this very useful code and documentation.
This discussion has been closed.