Multi-column sort programmatically

Multi-column sort programmatically

chancharleschancharles Posts: 3Questions: 0Answers: 0
edited April 2010 in General
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

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Charles ,

    http://datatables.net/api#fnSort should do the trick.

    Allan
  • chancharleschancharles Posts: 3Questions: 0Answers: 0
    Thanks, Allan, how exactly can I invoke fnSort when someone clicks on a sortable column header?

    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!
  • icatsicats Posts: 1Questions: 0Answers: 0
    I would love to know the answer to chancharles' question as well! When I sort by one column I always want the secondary sort on another column.

    Thanks :)
  • turnkadelicturnkadelic Posts: 1Questions: 0Answers: 0
    I would also like to know this. It's my only issue (but an important one) with an otherwise great plug-in.

    Thanks!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    To call fnSort, it's just like any other API function - from the documentation:

    [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
This discussion has been closed.