So, the question has already been asked here , but the solution does not work for me (I can do something wrong). I want to sort the tables alphabetically ("type": "natural"), but I want the empty cells to be at the bottom (for desc and asc).
I tried the previous solution given by fbas:
jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) { var retVal; x = $.trim(x); y = $.trim(y); if (x==y) retVal= 0; else if (x == "" || x == " ") retVal= 1; else if (y == "" || y == " ") retVal= -1; else if (x > y) retVal= 1; else retVal = -1; // <- this was missing in version 1 return retVal; } jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) { var retVal; x = $.trim(x); y = $.trim(y); if (x==y) retVal= 0; else if (x == "" || x == " ") retVal= -1; else if (y == "" || y == " ") retVal= 1; else if (x > y) retVal= 1; else retVal = -1; // <- this was missing in version 1 return retVal; }
Via:
$(document).ready(function() { $('#classement').dataTable({ "aoColumns": [ null, null, { "type" : "mystring" }, { "type" : "mystring" }, null ] } ); } );
With a table like | N° | Edit | Song | Singer | Url | | N° | Edit | Song | Singer | Url | Sort only for song and artist.
The emty cells are at the bottom (as expected), but now the sorting has no logic (no alphabetical order, should I use another property in the dataTable?).
Anyone have a solution?
Edit: If we add a row dynamically, how to update the sort?
$("#example").find('tbody') .append($('<tr>') .append($('<td>') .text('Boro') ) );
JsFiddle (use isim one)
Fanch source share