Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jameshandsoame/014af0a61d007df92b018a6353a9fac2 to your computer and use it in GitHub Desktop.

Select an option

Save jameshandsoame/014af0a61d007df92b018a6353a9fac2 to your computer and use it in GitHub Desktop.

Revisions

  1. Sam X created this gist Jun 23, 2011.
    42 changes: 42 additions & 0 deletions datatables.set_filter_debounce.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@

    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: [email protected]
    */
    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;
    }