5
I am trying to sort my table by the name field of my database table, but the jquery (datatable) plugin insists on sorting by id (first table field)
How to force sorting through the query result, in case sort by name ?
5
I am trying to sort my table by the name field of my database table, but the jquery (datatable) plugin insists on sorting by id (first table field)
How to force sorting through the query result, in case sort by name ?
9
Main options (assuming for example that the id
be the first column of the table and the nome
be the second):
Use the configuration parameter aaSorting
.
$('#exemplo').dataTable( {
"aaSorting": [[1, "asc"]]
)};
Where the first value(1
) array is the column index in the table and the second value (asc
) indicates whether the column should be ordered ascending or descending (asc
or desc
). Multiple columns can be used in sorting including multiple two-position arrays in Matrix (as in a clause order by
).
You can use the function fnSort
:
var tabela = $('#exemplo').dataTable();
// Ordena por nome e "desempata" com o id
tabela.fnSort([[1,'asc'], [0,'asc']]);
The default value reference of aaSorting
is [[0,'asc']]
. To keep sorting according to the original order of the dataset do:
$('#exemplo').dataTable( {
"aaSorting": []
)};
Sources:
Browser other questions tagged jquery mysql
You are not signed in. Login or sign up in order to post.
in case there would be some way for me to leave a null value set in the datatable so that it does not sort without the user clicking in the column ? so it would be ordered according to the result of the query
– tiaguinhow
Tries
"aaSorting": []
(the default value is[[0,'asc']]
).– Anthony Accioly
good guy, the "aaSorting": [] worked and worked as I wanted, brigadeo
– tiaguinhow