I'm trying to sort my table by the field name of my database table, but the jquery (datatable) plugin insists on sorting by the id (first field of the table)
How to force sorting through the query result, in the case sort by name?
I'm trying to sort my table by the field name of my database table, but the jquery (datatable) plugin insists on sorting by the id (first field of the table)
How to force sorting through the query result, in the case sort by name?
Main options (assuming for the purposes of the example that id
is the first column of the table and nome
is the second):
Use the aaSorting
configuration parameter.
$('#exemplo').dataTable( {
"aaSorting": [[1, "asc"]]
)};
Where the first value ( 1
) of the array is the index of the column in the table and the second value ( asc
) indicates whether the column should be sorted ascending or descending ( asc
or desc
). Multiple columns can be used in sorting by including multiple arrays of two positions in the matrix (as in a order by
clause).
You can use the fnSort
function:
var tabela = $('#exemplo').dataTable();
// Ordena por nome e "desempata" com o id
tabela.fnSort([[1,'asc'], [0,'asc']]);
The default reference value of aaSorting
is [[0,'asc']]
. To keep the ordering in the original order of the dataset do:
$('#exemplo').dataTable( {
"aaSorting": []
)};
Sources: