Can't create a custom sorting function
Can't create a custom sorting function
I'm trying to use this example: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html
But for whatever reason, my way isn't working; I'm fairly sure it's mostly due to my own lack of knowledge about DataTables/jQuey/JS in general, so this should be an easy fix. Here's the code:
[code]function specialSort(a, b) {
x = a.indexOf('-') > -1; // true for date
y = a.indexOf('-') > -1; // true for date
if( x ) {
if( y ) {
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
} else {
return -1;
}
} else if( y ) {
return 1;
} else {
a = parseInt(a);
b = parseInt(b);
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
}
}
jQuery.fn.dataTableExt.oSort['special-asc'] = specialSort; // Is this the right syntax for assigning functions? I tried some other variants, but none of those worked...
jQuery.fn.dataTableExt.oSort['special-desc'] = function(a, b) { return -specialSort(a, b); };
/*snip...*/
"aoColumnDefs": [
{ "sType": "special", "aTargets": [8] }
]
[/code]
Whenever I add these lines to my code though, my table goes blank except for the headers. (And my invisible column headers appear.) I'm pretty sure it's just a syntax thing, but I don't know what it might be.
Thanks for the great work Allan!
But for whatever reason, my way isn't working; I'm fairly sure it's mostly due to my own lack of knowledge about DataTables/jQuey/JS in general, so this should be an easy fix. Here's the code:
[code]function specialSort(a, b) {
x = a.indexOf('-') > -1; // true for date
y = a.indexOf('-') > -1; // true for date
if( x ) {
if( y ) {
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
} else {
return -1;
}
} else if( y ) {
return 1;
} else {
a = parseInt(a);
b = parseInt(b);
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
}
}
jQuery.fn.dataTableExt.oSort['special-asc'] = specialSort; // Is this the right syntax for assigning functions? I tried some other variants, but none of those worked...
jQuery.fn.dataTableExt.oSort['special-desc'] = function(a, b) { return -specialSort(a, b); };
/*snip...*/
"aoColumnDefs": [
{ "sType": "special", "aTargets": [8] }
]
[/code]
Whenever I add these lines to my code though, my table goes blank except for the headers. (And my invisible column headers appear.) I'm pretty sure it's just a syntax thing, but I don't know what it might be.
Thanks for the great work Allan!
This discussion has been closed.
Replies
Allan