So the question has already been asked here, but the solution doesn\'t work for me (I might do something wrong). I want to sort my tables by alphabetical order (\"type\" : \
UPDATE: Embedded Stack Snippet.
I think the aoColumns
is a legacy option for DataTables v 1.9. That being said, you might also need to use $.extend to include your custom sort functions.
Please take a look at the Stack Snippet below, or this live demo on jsfiddle. In a nutshell, I define the name
column as the type non-empty-string
during the table initalization. Then I extended the jQuery.fn.dataTableExt.oSort
API with a non-empty-string-asc
and a non-empty-string-desc
sorting functions. See if this is what you are looking for.
Stack Snippet:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"non-empty-string-asc": function (str1, str2) {
if(str1 == "")
return 1;
if(str2 == "")
return -1;
return ((str1 < str2) ? -1 : ((str1 > str2) ? 1 : 0));
},
"non-empty-string-desc": function (str1, str2) {
if(str1 == "")
return 1;
if(str2 == "")
return -1;
return ((str1 < str2) ? 1 : ((str1 > str2) ? -1 : 0));
}
} );
var dataTable = $('#example').dataTable({
columnDefs: [
{type: 'non-empty-string', targets: 0} // define 'name' column as non-empty-string type
]
});
dataTable.api().row.add(['John Smith', 'Intern', 'San Francisco', 19, 2011/05/25, 62000]).draw();
Name
Position
Office
Age
Start date
Salary
Tiger Nixon
System Architect
Edinburgh
61
2011/04/25
$320,800
Garrett Winters
Accountant
Tokyo
63
2011/07/25
$170,750
Ashton Cox
Junior Technical Author
San Francisco
66
2009/01/12
$86,000
Cedric Kelly
Senior Javascript Developer
Edinburgh
22
2012/03/29
$433,060
Junior Technical Author
San Francisco
66
2009/01/12
$86,000
Senior Javascript Developer
Edinburgh
22
2012/03/29
$433,060