Skip to content

Instantly share code, notes, and snippets.

@wardpeet
Created October 25, 2013 14:05
Show Gist options
  • Save wardpeet/7155197 to your computer and use it in GitHub Desktop.
Save wardpeet/7155197 to your computer and use it in GitHub Desktop.
Trigger datatable search when user stops typing By default datatables filters by keypress (to be specific keyup event) Based on http://stackoverflow.com/questions/14042193/how-to-trigger-an-event-in-input-text-after-i-stop-typing-writting
// INITIATE DATATABLE
$(this).dataTable();
// Change the behaviour of datatables filter (wait until user stops typing)
var $filterBox = $(this).parents('.dataTables_wrapper').find('.dataTables_filter input')
, handlers = $.extend({}, $._data($filterBox[0], 'events')['keyup']); // Clone the object
// Remove all keyup methods, we will run them later
$filterBox.off('keyup')
var stoppedTyping; // we want to capture our timer to remove it
$filterBox.on('keyup', function() {
// remove existing timer
if (stoppedTyping) clearTimeout(stoppedTyping);
// Set a timer to check if we stopped typing
stoppedTyping = setTimeout($.proxy(function(e) {
for (var i in handlers) {
if (handlers[i].guid) {
handlers[i].handler.apply(this, [e]);
}
}
}, this), 500);
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment