Can fnSort use the default ordering (asc vs desc) or does it have to be passed as a parameter?

Can fnSort use the default ordering (asc vs desc) or does it have to be passed as a parameter?

jeffwjeffw Posts: 38Questions: 5Answers: 0
edited July 2013 in General
I'm using a syntax like this (where thisCol is the that user clicked on) and it's working fine:

oSearchResultsTable.fnSort( [ [thisCol, 'asc'], [1,'asc'], [2,'asc'] ] );

But I'd like the sort order for thisCol be be asc/desc depending on how many times user clicked, i.e. i'd like to just inherit the default sort order behavior. Is this possible? I'm not sure how to trap what the current sort order is, and pass it as the second parameter. I've tried looking for attributes, like $(this).attr('aria-sort'), but that seems wrong (and won't work). Is there a way to do this strictly through the datatables object without reference to the DOM?

many many thanks.

Jeff

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    It has to be passed in.

    Interestingly I was wondering about exactly this yesterday while working on an update in the sorting methods. I decided not to put it in since no one had ever expressed an interest in it before...

    I'll take another look and see if it can be done with very little code.

    Allan
  • jeffwjeffw Posts: 38Questions: 5Answers: 0
    Thanks Allan. How could I toggle the table headers to use fnSort but switch direction (asc/desc) with each click, as with default behavior? As I mentioned, the aria-sort approach (below) didn't work.

    Jeff

    $('#tblRequiresAction th').click(function () {

    if ($(this).attr('aria-sort')=='ascending') {

    oRequiresActionTable.fnSort( [ [$(this).index(), 'asc'], [1,'asc'], [2,'asc'] ] );

    } else {

    oRequiresActionTable.fnSort( [ [$(this).index(), 'desc'], [1,'asc'], [2,'asc'] ] );

    }


    });
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    You either need to use some logic which retains current state - or you could use the fnSortListener function which has that logic built in.

    Allan
  • jeffwjeffw Posts: 38Questions: 5Answers: 0
    Many thanks for the immediate response. This seems to be working, i'll have to really test it out:

    $('#tblSearchResults th, #tblRequiresAction th').click(function () {

    var thisCol = $(this).index() ;
    var thisColOrder = $(this).attr('aria-sort')==='ascending' ? "asc": "desc" ;

    oSearchResultsTable.fnSort( [ [thisCol, thisColOrder], [1, 'asc'], [2, 'asc'] ] );

    });
  • jeffwjeffw Posts: 38Questions: 5Answers: 0
    No luck. I think it's because the aria-sort doesn't seem to be changing with each click, the way it does when fnSort isn't involved. I can try attaching it to jquery's toggle function instead of click and passing in asc/desc that way instead.

    I looked at the fnSortListener page, but I'm not clear on how the listener would toggle ascending/descending. Is there an example of that somewhere?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    A little example here: http://live.datatables.net/inequx/edit#javascript,html

    I haven't been able to reproduce the problem with the aria attribute not updating when using fnSort - it appears to work as it should for me.

    I should say that 1.10 is going to have an API to get the current sort state, rather than needing to resort to methods like that :-)

    Allan
This discussion has been closed.