Multi-column sort programmatically
Multi-column sort programmatically
chancharles
Posts: 3Questions: 0Answers: 0
Hi, is there a way to programmatically perform multi column sort? For example, when the first column is sorted, I need to use the 2nd column as secondary sort. I know this can be done with holding the "Shift" key. However, I don't want to require the user to do that.
aaSorting seems to be used only for initialization.. So I need something other than that.
Any help is much appreciated!
Thanks!
Charles
aaSorting seems to be used only for initialization.. So I need something other than that.
Any help is much appreciated!
Thanks!
Charles
This discussion has been closed.
Replies
http://datatables.net/api#fnSort should do the trick.
Allan
The only callback I can see is to use the Sort plugin but that doesn't have a reference to the table itself, does it?
How else would you do it?
Thanks!
Thanks :)
Thanks!
[code]
$(document).ready(function() {
var oTable = $('#example').dataTable();
/* Sort immediately with columns 0 and 1 */
oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );
[/code]
So all you need to do is to add an event handler to whatever element you want to have the sorting controlled on, something like this:
[code]
$(document).ready(function() {
var oTable = $('#example').dataTable();
$('#click_me').click( function () {
oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );
} );
[/code]
If you want to use the sort listener that DataTables does (click once for asc, twice for desc, shift click support etc), you can use the fnSortListener API function: http://datatables.net/api#fnSortListener
Allan