Default collation for jquery datatables

I am trying to set the default collation to the second column in my jquery datatable. It defaults to sorting by index 0. I use the syntax "aaSorting": [[ 1, "asc" ]] , but it highlights the column that I don’t want at boot time. How can I set the default sorting for a specific column without highlighting the column as if sorting were not involved, and index column 0 was used.

+54
javascript jquery sorting datatables
Dec 26 2018-11-21T00:
source share
9 answers

There are several options:

  • Immediately after initializing the DataTables, delete the collation classes in the TD element in TBODY.

  • Disable collation classes using http://datatables.net/ref#bSortClasses . The problem is that it will disable the sort classes for user sort requests - which may or may not be what you want.

  • Let your server display the table in the required sort order and not use the default sort in the table ( aaSorting:[] ).

+54
Dec 27 '11 at 9:40
source share

Here is the real code that does this ...

 $(document).ready(function() { var oTable = $('#myTable').dataTable(); // Sort immediately with column 2 (at position 1 in the array (base 0). More could be sorted with additional array elements oTable.fnSort( [ [1,'asc'] ] ); // And to sort another column descending (at position 2 in the array (base 0). oTable.fnSort( [ [2,'desc'] ] ); } ); 

To avoid highlighting the column, change the CSS like this:

 table.dataTable tr.odd td.sorting_1 { background-color: transparent; } table.dataTable tr.even td.sorting_1 { background-color: transparent; } 
+47
Jan 14 '13 at 23:30
source share

You can use the fnSort function, see details here:

http://datatables.net/api#fnSort

+15
Jul 03 2018-12-12T00:
source share

The best option is to disable sorting and simply submit the data with the desired sort order (from a database or other source). Try adding this to your "datatable": "bSort": false

+2
Jan 29 '14 at 11:08
source share

Datatables supports HTML5 data- * attributes for this function.

It supports multiple columns in sort order (based on 0)

 <table data-order="[[ 1, 'desc' ], [2, 'asc' ]]"> <thead> <tr> <td>First</td> <td>Another column</td> <td>A third</td> </tr> </thead> <tbody> <tr> <td>z</td> <td>1</td> <td>$%^&*</td> </tr> <tr> <td>y</td> <td>2</td> <td>*$%^&</td> </tr> </tbody> </table> 

Now my jQuery is just $('table').DataTables(); , and I get my second and third columns sorted in desc / asc order.

Here are some other nice attributes for <table> that I reuse:

data-page-length="-1" sets the page length for All (pass 25 for page length 25) ...

data-fixed-header="true" ... Make an assumption

0
Sep 28 '16 at 0:25
source share

I also had this problem. I used the stateSave parameter and this made this problem.
Remove this option and the problem is resolved.

0
Nov 01 '16 at 6:01
source share

use this, it works for me: "order": [[1, "ASC"]],

0
Dec 14 '16 at 9:02
source share

This worked for me:

  jQuery('#tblPaging').dataTable({ "sort": true, "pageLength": 20 }); 
0
Jan 03 '17 at 14:26
source share

Just include the following code:

  $(document).ready(function() { $('#tableID').DataTable( { "order": [[ 3, "desc" ]] } ); } ); 

Full reference article with an example:

https://datatables.net/examples/basic_init/table_sorting.html

0
Aug 14 '17 at 12:33 on
source share



All Articles