12

I have a 4 column table, I want the first 3 columsn to be sortable by the user, but not the 4th, this is working fine. I also want the 3rd column to sort in ASC order by default. This part isn't working, I cant get any of the columns to sort by default and can't figure out what's wrong with my syntax:

$(document).ready(function() {
$(".table-sortable").dataTable({
    aaSorting: [],
    bPaginate: false,
    bFilter: false,
    bInfo: false,
    bSortable: true,
    bRetrieve: true,
    aoColumnDefs: [
        { "aTargets": [ 0 ], "bSortable": true },
        { "aTargets": [ 1 ], "bSortable": true },
        { "aTargets": [ 2 ], "asSorting": [ "asc" ], "bSortable": true },
        { "aTargets": [ 3 ], "bSortable": false }
    ]
}); 
});

Here's what I've been working from: http://datatables.net/usage/columns

1
  • I did figure out what IS happening, is now its not allowing that type of sort on column 3. I tried adding "desc", "asc", "asc" but no success. Commented Sep 13, 2012 at 16:16

2 Answers 2

36

This should get you what you need

$(document).ready(function() {
    $(".table-sortable").dataTable({
        aaSorting: [[2, 'asc']],
        bPaginate: false,
        bFilter: false,
        bInfo: false,
        bSortable: true,
        bRetrieve: true,
        aoColumnDefs: [
            { "aTargets": [ 0 ], "bSortable": true },
            { "aTargets": [ 1 ], "bSortable": true },
            { "aTargets": [ 2 ], "bSortable": true },
            { "aTargets": [ 3 ], "bSortable": false }
        ]
    }); 
});

The key is the aaSorting option. For some reason it's not in his 'main' Usage pages... you can find it here though http://datatables.net/ref

Sign up to request clarification or add additional context in comments.

5 Comments

@turbo2oh: Glad you've got it working. DataTables is a great plugin
Hi, anyone got aaSorting with if aTargets use classnames instead of column indices?
The only way I've gotten it to work with class names instead of column indices if with the following: 'aaSorting': [[$('.desc').index(), 'desc']] where '.desc' of the class on the column th I'd like to sort.
not working for me, only "bSortable": true -- showing sorting icon, "bSortable": false -- not showing icon. But functionality not working.
Thank you! I couldn't figure out why "order" wasn't working for me like it showed here: datatables.net/examples/basic_init/table_sorting.html. aaSorting worked perfectly as expected. Thanks!
3

It worked for me. Thanks.. Initially I was using 'order':[2,'desc'] which was not working..Correct option is aaSorting

eg;

$(document).ready(function() {
    $('#example1').DataTable({
        aaSorting: [[0, 'desc']]
    });
});

3 Comments

This is not an answer, please add this text as it should be, as a comment.
Thanks for pointing this out. This indeed works. The documentation I could find that seemed "official" showed "order" as the proper parameter, but aaSorting worked for me. Anyone know why "order" doesn't work? Bad docs?
I haven't used data tables in a while now but as I remember aaSorting was pre-1.10 and order is >=1.10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.