Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting your jquery datatable to sort DESC by first column. Or setting another column as its default sortby

I've been trying to figure this out now for a few hours and its not happening. When I load my table I need for it to sort desc on the first col. Here's what I have but no luck.

/**
*   Makes the table in the tabs for reports
*
* @param array Holds the data for datatable.
**/
var makeTabTable = function(tableData){
    $("#tab_active_reg").dataTable({
        bJQueryUI : true,
        bDestroy : true,
        aoColumns : [
            {"sTitle" : "Seen", "sWidth" : "50px", "asSorting": [ "desc"]},
            {"sTitle" : "Sold", "sWidth" : "50px"},
            {"sTitle" : "Name", "sWidth" : "240px"}
        ],
        aaData : tableData  
        });
    $('.tabsComponent').find('.fg-toolbar').empty();
};
like image 734
Tim Lieberman Avatar asked Jul 04 '12 17:07

Tim Lieberman


People also ask

How do I change the default sort in DataTable?

Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending ordering) as required.

How do you make a sorting false in DataTable?

Use columnDefs option to remove sorting from a column. Pass column index in targets within [] (Indexing starting from 0) and set orderable to false .

What are columnDefs targets?

targets option provides the information required by DataTables for which columns in the table the column definition object should be applied. It can be: 0 or a positive integer - column index counting from the left. A negative integer - column index counting from the right.


1 Answers

just add this option to the dataTable() call:

aaSorting : [[0, 'desc']]

so in your case:

   $("#tab_active_reg").dataTable({
        bJQueryUI : true,
        bDestroy : true,
        aaSorting : [[0, 'desc']],
        aoColumns : [
            {"sTitle" : "Seen", "sWidth" : "50px"},
            {"sTitle" : "Sold", "sWidth" : "50px"},
            {"sTitle" : "Name", "sWidth" : "240px"}
        ],
        aaData : tableData  
    });

as you can see, it is an array of [colnumber, sortdirection], so you can specify multiple.

like image 172
mindandmedia Avatar answered Sep 20 '22 22:09

mindandmedia