Change default column Datatables JS

Asked

Viewed 222 times

2

I’m wondering if it is possible to change the default ordering of a datatable, made with the plugin https://datatables.net/

Currently he is "born" by ordering by the first column, and I wanted him to start ordering by the second:

inserir a descrição da imagem aqui

Follows the Code:

$('#table').DataTable({
    "scrollX": false,
    "ordering": true,
    "lengthMenu": [ [ 10, 30, 50, 100, -1], ["10","30", "50", "100","Todos"] ],
    "scrollCollapse": true,
});

Table is a common table...

<table id="table"> 
   <thead>
       ...
   </thead>
   <tbody>
      ...
   </tbody>
</table>

Thanks in advance!

1 answer

3


Looking at the documentation, it is possible to change the sorting of a Datatable using the property order which is a vector of two-position vectors, where the first indicates which column (from 0) and the second type, whether it is crescent (asc) or nonperiodic (desc), as an example below:

$(document).ready(function() {
    $('#example').DataTable( {
        "order": [[ 3, "desc" ]] //Ordena pela quarta coluna de forma descrescente
    } );
} );
  • I swear I passed beaten in that part of the doc. Really worth!

Browser other questions tagged

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