fnSort binds to "sort" event and to "filter" event?

fnSort binds to "sort" event and to "filter" event?

jitterskajitterska Posts: 3Questions: 0Answers: 0
edited June 2012 in Bug reports
Hi Allan,

First I just want to commend you on your excellent work. Anyone who uses jQuery should use your wonderful tool as well. I'm attempting to hook a few javascript calls into some of the DataTable's events and have had a lot of success in doing so by using the "bind" functionality. However I'm running into trouble when I attempt to separate the "sort" event from the "filter" event. I need two separate functions to occur, but when I sort my table, it also always calls the "filter" event. Any light you could shed on this would be extremely helpful. Thanks!

[code]
var oTable = $("#example").dataTable({
"bDestroy": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaData": eval(response.d) }).bind("sort", function() { alert('sort called'); }).bind("filter", function() { alert('filter called'); });

oTable.fnSort([[4, 'desc']]);
[/code]

Replies

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    This is actually correct, because at the moment when you do a sort you actually does a filter as well - this the filter event is fired. It is a quirk of how DataTables is currently implemented - sorry :-(.

    The plan ( http://datatables.net/development/roadmap ) is to separate that behaviour - v1.11 and stop this from happening, but that's a good while off since 1.10 development hasn't even started yet...

    I'm actually not 100% sure how to combat this at the moment. All I can say is that if the bFiltered and bSorted flags in the settings object ( http://datatables.net/docs/DataTables/1.9.1/DataTable.models.oSettings.html#bFiltered ) are both set, then its a sort that has happened.

    A bit pants I know - but unfortunately fixing it is going to take a good deal of time.

    Allan
  • jitterskajitterska Posts: 3Questions: 0Answers: 0
    Hey, no worries. I really appreciate everything you've done with this tool. It is seriously is one of my favorites to use combining UI design with jQuery. Datatables itself has already saved me tons of time so if I have to invest a bit in a work-around to my problem I'd considered it a fair trade! Again awesome job.
  • eddedd Posts: 1Questions: 0Answers: 0
    anyone interested in a workaround, here's how I separated the two events:

    for filtering, find the input box and attach a keyup event, delaying execution of the desired function by a certain time to allow people to enter a whole phrase without triggering it at every key stroke.

    [code]
    var keyTimer;
    $("#tableDiv").find('input').keyup(function() {

    clearTimeout(keyTimer);
    keyTimer = setTimeout(function(){ console.log("filtered"); }, 500);
    });
    [/code]

    similar for sorting, just attach a click handler to thead:
    [code]
    $("#tableDiv").find('thead').click(function() {

    console.log("sorted");
    });
    [/code]

    p.s.: also want to thank you Allan for the great job and effort! much appreciated! will insist my company donates something when the project is done.
This discussion has been closed.