DataTables Set Default sorting column and set unsortable columns

datatables, javascript, jquery

Solution

As per the table sorting docs you can do that using the `order` option:

$('.table-asc0').dataTable({
  order: [[0, 'asc']]
})

The 0 indicates to sort on the first column, while `asc` to do it in ascending order. You can chose any other column and use `desc` too.

For DataTables versions prior to `1.10` you should use `aaSorting` instead

$('.table-asc0').dataTable({
  aaSorting: [[0, 'asc']]
})

To order descending on the first column:

$('.table-asc1').dataTable({
  aaSorting: [[1, 'desc']]
})

Problem

Is it possible to set the default column to sort once the page loads? I want to use the one datatable call for different tables across my site. Is it possible to add a `th` class to achieve this? I also want to disable sorting on some columns and since i'm looking for the one datatables call to do everything, is there a class i can add to the th that will make it unsortable? This is my called dataTable script ``` if (jQuery().dataTable) { $('#table-list-items').dataTable({ "fnDrawCallback" : function () { }, "aLengthMenu": [ [10, 15, 25, 50, 100, -1], [10, 15, 25, 50, 100, "All"] ], "iDisplayLength": 25, "oLanguage": { "sLengthMenu": "_MENU_ Records per page", "sInfo": "_START_ - _END_ of _TOTAL_", "sInfoEmpty": "0 - 0 of 0", "oPaginate": { "sPrevious": "Prev", "sNext": "Next" } }, "aoColumnDefs": [{ 'bSortable': true, 'aTargets': [0] }] }); } ```

Original source

Related problems