Sort column of datatable via query

5

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?

    
asked by anonymous 03.04.2014 / 19:19

1 answer

9

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

During the construction of the table

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

After construction

You can use the fnSort function:

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

Default sort order

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:

03.04.2014 / 19:34