How to reset/clear sorting

How to reset/clear sorting

TomdeeTomdee Posts: 1Questions: 0Answers: 0
edited February 2010 in General
My application gets table data from a query and displays it with no initial sorting by DataTables ("aaSorting":[ ]). Table sorting is enabled and works fine.

But I would like to be able to reset to the initial state: rows in query order, no sort column(s) set, and no sort classes applied. Is there a way I can do that?

Replies

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

    There isn't really a way to do this at the moment. I think the thing to do would be to create an API function ( http://datatables.net/development/api ) called fnSortingReset() which would empty the aiDisplay and aiDisplayMaster arrays (and aaSorting) and then recalculate them based on aoData...

    Sorry - I know this sounds a little complicated, but with the number of support requests in the forum at the moment and paid support, I don't really have time to write the plug-in myself :-(. I've got it in my to-do list now - but if you take a stab at it, please to post back!

    Regards,
    Allan
  • dgtaylor22dgtaylor22 Posts: 16Questions: 0Answers: 0
    I added a button on my page to clear the sorting, but it doesn't reset the datatable to the original data order. Here is the code I used.

    [code]
    $('#ClearSort').click( function() { oTable.fnSort( [ ] ); });
    [/code]

    You may be able to add a redraw or some other function that would put the data back in the original order in which it was loaded.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I've just created a plug-in called fnSortNeutral which will do this - http://datatables.net/plug-ins/api#fnSortNeutral

    Regards,
    Allan
  • manifestinteractivemanifestinteractive Posts: 1Questions: 0Answers: 0
    edited September 2010
    Here is one I use that I think feels more intuitive:

    [code]
    var oTable = $('#data_table').dataTable();

    $('.sorting, .sorting_asc, .sorting_desc').dblclick(function(){
    oTable.fnSort([]);
    return false;
    });
    [/code]
    This will make all column headers that are sortable clear all sorting when the user double clicks any sortable column header.

    - Peter
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    That's quite a nice idea that - like it. One thing it won't do however is to restore the original sort order (i.e. the order read in from the HTML), which the plug-in above will do.

    Allan
This discussion has been closed.