API #fnSortNeutral - Get Trigger if sorting?

API #fnSortNeutral - Get Trigger if sorting?

svrsvr Posts: 23Questions: 0Answers: 0
edited November 2012 in General
hi, how may i do this:

the user clicks onto a row and starts the sorting. i want to display a Remove Order button AFTER he sorted the table away from standard sorting. i managed to hide the span #sorted after http://datatables.net/plug-ins/api#fnSortNeutral was called. the function works fine and returns to original sorting ... but how do i trigger the initial klick by the user on any row of the table to SHOW the hidden span #sorted again? so the "remove order" span only appears when something is sorted by the user???

hopefully u understand ... :)

thanks for any help ... stefan

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    The display of the button is a but ugly in this example, but it is fully functional: http://live.datatables.net/agaruc/edit#javascript,html :-).

    Basically you can list for the 'sort' event which is emitted from the table by DataTables ( http://datatables.net/docs/DataTables/1.9.4/#sort ). Wen that event is found you know the table is being sorted, so we can show the button. When the button is click it is hidden again (enable / disable might be another option).

    Regards,
    Allan
  • svrsvr Posts: 23Questions: 0Answers: 0
    that worked... thank you for the quick response!
  • svrsvr Posts: 23Questions: 0Answers: 0
    i guess there is a problem by expanding the functionality:


    $("#FTABLEreset").click(function () {
    /* Remove all filtering */
    $('#<? echo $tableName; ?>').dataTable().fnFilterClear();
    $("#FTABLEreset").css('display', 'none');
    });

    $("#FSORTreset").click(function () {
    /* Remove all sorting */
    $('#<? echo $tableName; ?>').dataTable().fnSortNeutral();
    $("#FSORTreset").css('display', 'none');
    });

    /* Hide SORTbutton */
    $('#<? echo $tableName; ?>').on('sort', function () {
    $('#FSORTreset').css('display', 'block');
    } );

    /* Hide Filterbutton */
    $('#<? echo $tableName; ?>').on('filter', function () {
    $("#FTABLEreset").css('display', 'block');
    } );

    if i call .on('filter', function () it seams that on('sort', function () is also fired... this means, klicking on the "remove filters" button adds the display:block to the befor hidden "remove sort orders" button. is it possible, that on'sort' is the same call as on'filter' ??? am i wrong?
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    You are quick correct - this is an unfortunate side effect of how DataTables works internally.

    Here is a solution - rather than listening for the 'sort' event from the table, listen for the click event on the headers (which is when sorting occurs): http://live.datatables.net/agaruc/2/edit

    Allan
  • svrsvr Posts: 23Questions: 0Answers: 0
    hm, ... i cant get it done usung FILTER buton hide/show AND SORT button hide/show in the same table. only using one of each will do the job. would be nice to search
    1. is something sorted away of initial sorting? yes -> show "remove sort" button
    2. is something filtered ? yes -> show "reset filter" button ...

    only listening to the click events seams not to do the job... :(

    maybe it's because of the complex structure of my tables as you might remember... using quick filters and so on...

    i guess i'm only able to use one of these buttons.
  • svrsvr Posts: 23Questions: 0Answers: 0
    got it! i had to move the click event handler to the top of document ready function ... because a clean up procedure inside there called sort again ... THANKS!
This discussion has been closed.