305

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.

0

4 Answers 4

710

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": []
    });
})
Sign up to request clarification or add additional context in comments.

6 Comments

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.
For newer versions its order: []
@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.
You can set it on a table level too -> <table data-order="[]">
This solution wont work for me but <table data-order="[]"> works for me.
|
118

As per latest api docs:

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

More Info

Comments

1

This question is old, but this might help if someone else is struggling with this and doesn't have enough time to find a better way to resolve it. I had a problem using ajax where if I use aaSorting": [], "order": [], "bSort": false or "ordering": false. In all cases it gives an error. And I also have:

 "columnDefs": [
        { sortable: false, orderable: false, targets: '_all' }         
 ],

to disable ordering on all columns. That actually worked on all columns except the first one that ignores the orderable and keeps using the default ordering. So I found a rustic way to make that default ordering arrow to disappear.

I added to the datatable:

 initComplete: function() {
         $('th').removeClass('sorting_asc');
 }

And now it is gone:

enter image description here

1 Comment

Every answer show how to you disable sorting when instantiating DataTables, but no one is explain how to turn it on and off after it's instantiated.
0

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.

1 Comment

The question was how to "disable initial sorting", not how to specify another sort order. This answer is better suited to a different question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.