preventing events
preventing events
Short story:
I want to prevent the search/ajax event, I tried returning false in the event functions:
$(table_obj).on('preXhr.dt', function (event, settings, json)
{
return false;
});
but it doesn't work, is there a way to do this?
Long story, I had an issue with the SearchDelay option:
This following paragraph just describes the current functionality of the SearchDelay option, I explain it because I was expecting something different.
When I write something in the search field, the first key I type triggers the first "search event" and launches an ajax request with just the first character in the search field, then the throttling starts but it will last the duration I've set, even if I keep typing in shorter periods than this duration, if I'm not done with the typing it will keep sending requests after each "throttle period".
When I first saw this option I thought it's a x ms delay before assuming the typing has ended and the request can be send. But then again maybe this option is alright as it is, a delay between the first triggered search and the following one, and what I'm describing should be a new option.
Also this might work better with a minimum character length option, SearchMinLength?, before starting the search. Attempting to manually do this functionality I tried using the search event function but I wasn't able to find a way to stop the search, same with preXhr.
This question has an accepted answers - jump to answer
Answers
That I think is a bug that I need to fix. I'll try to do so next week before 1.10.6 is released.
Correct - it is a throttle, not a debounce.
If you wanted to create a debounce you would need to unbind the default listener for the input box and replace it with your own that does use a debounce function.
Allan
Thank you for the advice.
I managed the functionality rebinding the events "keyup.DT search.DT input.DT paste.DT cut.DT" of the search input to a new function. The new function is a copy of the "var searchFn = function() {..." of the source plus the debounce code, I used $.fn.dataTable.ext.oApi._fnFilterComplete to replace the call/s, I can't use "_fnFilterComplete" directly, and it works, is this alright? or is there a "better" way to call these inner functions?
I would suggest calling
search()
and avoiding the private API entirely. SetsearchDelay
to 0 to stop DataTables doing any throttling if you are using server-side processing.Allan
For anyone looking into this in the future like I was, if you want to prevent the ajax event before it happens, you can use
preDrawCallback
. If youreturn false
inside it, it will cancel the AJAX request.Later on I discovered that you can overwrite the ajax function, i.e.:
This way I have full control.