Custom search fires search event twice

Custom search fires search event twice

marcusonmarcuson Posts: 3Questions: 1Answers: 0

Link to test case: https://jsfiddle.net/svarcm/vpL0rqas/
Description of problem: When a custom search is used, the "search" event is fired twice for every query change.

Answers

  • marcusonmarcuson Posts: 3Questions: 1Answers: 0

    I realized the second fire is because of necessary draw(), but is it possible to have a custom search with only one firing of search event?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    As you say, it's because the draw is showing the filter, so unfortunately it's unavoidable.

    I suspect the only way around it would be to use a global variable and break up the search() and draw() - using the variable to tell your handler to ignore the one you don't care about. It's clunky, but I can't think of another way around it, I'm afraid,

    Colin

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited November 2020

    One option might be to use the draw event to create a single instance event handler using one(). You will need to use off() first. See this example:
    https://jsfiddle.net/5sukd0bp/1/

    Note that any table draw, ie sorting, paging, etc, will fire the search event.

    Kevin

  • marcusonmarcuson Posts: 3Questions: 1Answers: 0

    Thanks for the nice solution, but search is still performed twice on the background, see https://jsfiddle.net/svarcm/gxqpu829/. I would be enough with original search box, but I need to modify query (remove accents) before search and there is aparently no way to do that. When you have table with 20k rows, performance is just twice as bad.

This discussion has been closed.