Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datatables default sort not working

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

like image 851
turbo2oh Avatar asked Sep 13 '12 16:09

turbo2oh


1 Answers

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

like image 128
BLSully Avatar answered Oct 07 '22 14:10

BLSully