294

I'm using the jquery DataTables plugin. From their documentation:

If sorting is enabled, then DataTables will perform a first pass sort on initialisation. You can define which column(s) the sort is performed upon, and the sorting direction, with this variable. The aaSorting array should contain an array for each column to be sorted initially containing the column's index and a direction string ('asc' or 'desc').

Is it possible to have sorting enabled but disable this first pass sort on initialization? I am currently doing the initial sort server side and need sorting functionality but don't need this initial sort functionality.

smoak
  • 14,554
  • 6
  • 33
  • 33

3 Answers3

679

Well I found the answer set "aaSorting" to an empty array:

$(document).ready( function() {
    $('#example').dataTable({
        /* Disable initial sort */
        "aaSorting": []
    });
})

For newer versions of Datatables (>= 1.10) use order option:

$(document).ready( function() {
    $('#example').dataTable({
        /* No ordering applied by DataTables during initialisation */
        "order": []
    });
})
BitOfUniverse
  • 5,903
  • 1
  • 34
  • 38
smoak
  • 14,554
  • 6
  • 33
  • 33
  • 7
    This answer was a great help but it's worth noting the potential confusion caused. If col 0 is pre-sorted asc and this initial sort is disabled then when a user first clicks col 0's header it will sort in asc order. To the user this looks like nothing happens as they will expect desc order. A second click will sort desc. To get around this you can set `asSorting: ['desc', 'asc']` in `aoColumnDefs` so that the first click is a desc sort. – tomfumb Nov 05 '14 at 19:37
  • 8
    For newer versions its order: [] – Darren Apr 28 '16 at 22:14
  • @tomfumb Actually, there is a GUI showing it is not sorted. The first click will show the Sort being activated, even though the items do not change. That's enough for me. – Nelson Apr 11 '18 at 06:05
  • 3
    You can set it on a table level too -> ``
    – Howdy_McGee Jul 09 '18 at 15:36
  • 1
    This solution wont work for me but `` works for me.
    – Alper Dec 25 '18 at 17:57
  • TIP: Use `if(aoData[0].value == 1) { console.log("first"); }` inside `"fnServerParams": function(aoData) {}` to detect if it is the datatable initialization. – Eduardo Lucio Apr 10 '19 at 17:46
113

As per latest api docs:

$(document).ready(function() {
    $('#example').dataTable({
        "order": []
    });
});

More Info

Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42
-1

In datatable options put this:

$(document).ready( function() {
  $('#example').dataTable({
    "aaSorting": [[ 2, 'asc' ]], 
    //More options ...

   });
})

Here is the solution: "aaSorting": [[ 2, 'asc' ]],

2 means table will be sorted by third column,
asc in ascending order.

luchopintado
  • 899
  • 11
  • 15
  • 17
    The question was how to "disable initial sorting", not how to specify another sort order. This answer is better suited to a different question. – iCollect.it Ltd Mar 03 '15 at 15:33