How to get values of the current sort column index and direction from [aoData]?

How to get values of the current sort column index and direction from [aoData]?

deathtospamdeathtospam Posts: 10Questions: 0Answers: 0
edited August 2012 in DataTables 1.9
I'm grabbing data from the server for each datatable page and it's working properly. However, each time an AJAX request is submitted -- for a new page of data, or when a column is clicked to re-sort the data -- I need to know what the current sort column index (0..n) and direction (ascending/descending) are. I know that these values are available in aoData, which I have access to, but I don't know what the syntax is to retrieve them.

Help?

Replies

  • cserfosscserfoss Posts: 11Questions: 0Answers: 0
    Hi --

    Take a look here:

    http://datatables.net/usage/server-side

    I think you'll find what your looking for under:

    bool bSortable_(int) Indicator for if a column is flagged as sortable or not on the client-side
    int iSortingCols Number of columns to sort on
    int iSortCol_(int) Column being sorted on (you will need to decode this number for your database)
    string sSortDir_(int) Direction to be sorted - "desc" or "asc".
  • deathtospamdeathtospam Posts: 10Questions: 0Answers: 0
    edited August 2012
    I know that information is packaged up and sent to the server; I'm trying to extract the iSortCol_0 and sSortDir_0 values via JavaScript/jQuery on the calling page, before the AJAX request is submitted, so I can use those values in other places on the page that rely on that information.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    You can get the current sorting state by using:

    [code]
    table.fnSettings().aaSorting
    [/code]

    That is DataTable's internal store. It should be treated as read only. If you want to set it, use fnSort .

    In 1.10 I'm going to look at making fnSort a getter/setter.

    Allan
  • deathtospamdeathtospam Posts: 10Questions: 0Answers: 0
    Great, tyvm
  • deathtospamdeathtospam Posts: 10Questions: 0Answers: 0
    edited September 2012
    Minor addendum: is there a way to do the same thing -- get the index and direction of the current sort column(s) -- when doing client-side sorting? Here's what I was trying to do, but it's not working.

    [code]
    $("#MyTable thead tr:eq(0)").on("click", "th", function(event){
    fGetSortInfo();
    });

    function fGetSortInfo() {
    // Returns a value of [5, "desc", 0] every time.
    var sortInfo = $("#MyTable").dataTable().fnSettings().aaSorting;
    }
    [/code]

    I'm wondering if I'm binding fGetSortInfo() in the wrong way or place. Is there an event I can bind to after the table is done updating?
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Its probably always returning that value because you are perhaps clicking on the 6th column in the table? It might be that the sort listener is running first (since you are attaching it tot he row with the sort listener cells) and then your function. Failing that, please give us a link :-)

    Allan
This discussion has been closed.