Sort table column datatable through query

Asked

Viewed 7,812 times

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 ?

1 answer

9


Main options (assuming for example that the id be the first column of the table and the nome be the second):

During the construction of the table

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).

After construction

You can use the function fnSort:

  var tabela = $('#exemplo').dataTable();
  // Ordena por nome e "desempata" com o id
  tabela.fnSort([[1,'asc'], [0,'asc']]);

Standard ordering

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:

  • 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

  • 1

    Tries "aaSorting": [] (the default value is [[0,'asc']]).

  • good guy, the "aaSorting": [] worked and worked as I wanted, brigadeo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.