Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default sorted column in Datatable

I am working on datatable plugin in Jquery. And have to set the default column by which the data is sorted so I mean that :

I have a table with 4 columns and by default the data is sorted by column no 1, I want that the data should be sorted by column number 2 or 3.

How that can be done:

$('#tblMainTable').dataTable({
    "bJQueryUI" : true,
    "sDom" : 'R<"H"lfr>t<"F"ip<',
    "aoColumns" : [ 
        {"bSortable" : false}, 
        null, 
        null,
        null,
        {"bSortable" : false}, 
        {"bSortable" : false}
    ],
    "aaSorting": [[ 2, "desc" ]]
});

I specified that in "aaSorting" but not getting the result.

Please shed some light?

like image 965
Ankur Verma Avatar asked Oct 14 '12 07:10

Ankur Verma


People also ask

How do you arrange data in ascending order 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 I sort a specific column in a DataTable?

$('table'). dataTable({ "pageLength": -1, //display all records "order": [[ 0, "desc" ]] // Sort by first column descending }); Reference: Sorting.

How does DataTable sorting work?

Sorting in DataTables is based on the detected type of the data column (you can add your own type detection functions, or override automatic detection using sType). With this specific type given to the column, DataTables will apply the required sorting function.

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 .


3 Answers

The example in the datatable api does it like this:

$(document).ready(function() {
  var oTable = $('#example').dataTable();

  // Sort immediately with columns 0 and 1
  oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );
like image 52
kannix Avatar answered Oct 20 '22 18:10

kannix


I know you have the answer now, but here is an even more easier way from the DatatTable API

$('#tblMainTable').dataTable({
    "order": [[1, "desc"], [2, "desc"]]
});

Do note that the index is from 0 'Zero' so the example means "Column 2 and 3 is the default sorting column and its Descending (use asc for Ascending)."

like image 21
Gabriel Ojomu Avatar answered Oct 20 '22 20:10

Gabriel Ojomu


I know you got the answer, but just for record

You can also sort it from server side by using the parameter

params.iSortCol_0

it is basically an integer 0,1,2.. means first,second,third.. column. so you can write a switch before fetching the data..

 String sortOn = 'firstcolumnname'; //default
 switch(params.iSortCol_0 as int) {

   case 0:
     sortOn = 'id';
     break;
  ......

  }

and include this in order by of your query

 order by ${sortOn}

Hope this will help

like image 2
Suganthan Madhavan Pillai Avatar answered Oct 20 '22 19:10

Suganthan Madhavan Pillai