Reset Sort Order

Reset Sort Order

redpaintredpaint Posts: 5Questions: 0Answers: 0
edited November 2009 in General
Hi,

I must be missing something but how do you reset the sort order of a column? I've got a hidden column that dictates the inital sort order on load and I want to allow sorting by column but I'm not sure how to reset that? I see there is a function in the API (fnSort) but I don't really want to have to re-specify the original sort colums. Is there any way to achieve this?

Many Thanks

Replies

  • izzy6150izzy6150 Posts: 49Questions: 0Answers: 0
    Hi redpaint,

    I think what you are looking for is the following:
    [code]
    $(document).ready( function () {
    var oTable = $('#example').dataTable( {
    "bSort": false,
    "aoColumns": [
    {"bSortable": false}, // first column unsortable (dont need to set to true as by default all columns are sortable when bSort is true.
    null, // first sortable column
    null, // second sortable column
    {"bSortable": false} // second unsortable column
    ]
    } );

    function restSort(oDataTable)
    {
    var oSettings = oDataTable.fnSettings(); // get the table settings
    oDataTable.fnSort( oSettings.aaSorting ); // call the sort function with initial sort parameters
    }

    function objectEvent() // this would be the definition of the event to fire the sort resetting
    {
    resetSort(oTable);
    }
    } );
    [/code]

    Hope this helps.

    Regards,
    Izzy
  • redpaintredpaint Posts: 5Questions: 0Answers: 0
    Hi Izzy,

    Thanks for the reply. I'm getting "Error: objectEvent is not defined" when firing objectEvent. I'm not all that familiar with object based javascript programming so I'm not sure where I've missed something. I'm trying to call objectEvent on a reset form button with the onclick handler.

    Many Thanks.
  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin
    Hi redpaint,

    I'm not sure I understand what you mean by "reset the sort order". You can _change_ the order that columns are sorted (i.e. asc -> desc, rather than desc -> asc) using aoColumns.asSorting - but I'm not sure that this is what you mean. From your description of using a hidden column for the initial sorting (aaSorting I presume), and then allow sorting on the visible columns, DataTables will do this by default. Could you perhaps elaborate a little?

    Also, regarding Izzy's code, I think there might be one or two little issues there, there is a typo "restSort" and possibly more importantly (and where the error you are seeing is occurring) is that oTable is not scoped for access outside $.ready(). Try making oTable global (i.e. define it outside $.ready()).

    Regards,
    Allan
This discussion has been closed.