Skip to content

Instantly share code, notes, and snippets.

@dtjm
Created June 23, 2011 22:14
Show Gist options
  • Save dtjm/1043768 to your computer and use it in GitHub Desktop.
Save dtjm/1043768 to your computer and use it in GitHub Desktop.
Search filter debounce for Allan Jardine's DataTables plugin for jQuery. Requires Underscore.js
jQuery.fn.dataTableExt.oApi.fnSetFilterDebounce = function ( oSettings, iDelay ) {
/*
* Type: Plugin for DataTables (www.datatables.net) JQuery plugin.
* Name: dataTableExt.oApi.fnSetFilterDebounce
* Version: 0.01
* Description: Enables filtration debouncing which prevents filter from
* happening until keypresses have stopped for [iDelay] milliseconds.
* Uses code from Zygimantas' fnSetFilteringDelay
* Inputs: object:oSettings - dataTables settings object
* integer:iDelay - delay in miliseconds
* Returns: JQuery
* Usage: $('#example').dataTable().fnSetFilterDebounce(250);
* Requires: DataTables 1.6.0+, Underscore.js 1.1.3+
*
* Author: Sam X Nguyen, Zygimantas Berziunas (www.zygimantas.com)
* and Allan Jardine (v2)
* Created: June 23, 2011
* Language: Javascript
* License: GPL v2 or BSD 3 point style
* Contact: samxnguyen@gmail.com
*/
var _that = this;
this.each( function ( i ) {
$.fn.dataTableExt.iApiIndex = i;
var iDelay = (iDelay && (/^[0-9]+$/.test(iDelay)) ? iDelay : 250),
$this = this,
oTimerId = null,
sPreviousSearch = null,
anControl = $( 'input', _that.fnSettings().aanFeatures.f );
var fnFilter = function() {
_that.fnFilter( anControl.val() );
}
anControl.unbind( 'keyup' ).bind( 'keyup', _(fnFilter).debounce(iDelay) );
return this;
} );
return this;
}
@unluckynelson
Copy link

unluckynelson commented Feb 20, 2022

I had to add anControl.unbind( 'input' ) for DT version 1.11.3

Thanks works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment