I somehow missed this breezing through the available documentation on the DataTables website, but basically the question arose on how to force a table that has just been loaded (with Datatables applied naturally) to be sorted in a particular manner by default?
Well it turns out that the answer to this question is pretty simple indeed.
When constructing your dataTable object you can pass it an initializer parameter named aaSorting which essentially allows you to set which columns to sort on and also which direction to sort in.
Take a look at this sample dataTables initialization below:
$('#example').dataTable( { "bProcessing": true, "bServerSide": true, "iDisplayLength": 150, "bLengthChange": false, "sAjaxSource": "reservations_list.php", "aaSorting": [[ 5, "desc" ]] } );
If you look at the last parameter being fed to the constructor you’ll spot our aforementioned aaSorting being sneaked in there with an array value that is setting column six to be ordered in a descending fashion.
(You’ll note I say column six because obviously this is array based manipulation and as we all know, javascript utilizes zero-indexed arrays.)
And that is pretty much it. aaSorting allows us to set the default sorting pattern for any dataTables object.
Nice.
You might also enjoy:
-
I’ve mentioned the wonderful jQuery plugin DataTables a number of times before, the awesome little trick that instantly transforms any HTML table fed to it ...
-
As it has quickly become apparent on this site, my two current favourites in the land of web development is the fantastic jQuery javascript library and the ...
-
I've mentioned the wonderful jQuery plugin Datatables a number of times before, the awesome little trick that instantly transforms any HTML table fed to it ...
-
Since my discovery of the awesome jQuery DataTables plug-in I haven't stopped using it in my projects at it is by far the simplest and cleanest way of deliv ...
-
DataTables is a brilliant jQuery plugin that instantly adds a full set of advanced interaction controls over whatever HTML table it gets attached to. It's f ...