fnSetFilteringDelay custom column filter delay with ajax support

fnSetFilteringDelay custom column filter delay with ajax support

DidzisDidzis Posts: 2Questions: 0Answers: 0

While browsing this board for answers on the topic I could not find one, so I decided to share my luck with thisone.

I have adapted the fnSetFilteringDelay plugin so it would fit the need for ajax delayed calls.

jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function ( oSettings, iDelay ) {

    var _that = this;

    if ( iDelay === undefined ) {
        iDelay = 250;
    }

    this.each( function ( i ) {
        $.fn.dataTableExt.iApiIndex = i;


        var
            $this = this,
            //anControl = $( 'input', _that.fnSettings().aanFeatures.f );
            anControl = $('input', $('th', _that) ); // use custom inputs instead of default Search field

        var oTimerId = new Array,
            sPreviousSearch = new Array;


            anControl.each( function(ind, el) {

                $(this).unbind( 'keyup search input' ).bind( 'keyup search input', function() {

                    var $$this = $this;


                    if (sPreviousSearch[ind] === null || sPreviousSearch[ind] != $(this).val()) {

                        window.clearTimeout(oTimerId[ind]);
                        sPreviousSearch[ind] = $(this).val();

                        oTimerId[ind] = window.setTimeout(function() {
                            $.fn.dataTableExt.iApiIndex = i;

// this line tells the ajax to do a column search instead of global search
                            _that.DataTable().column(ind)).search( $(el).val() ).draw();



                        }, iDelay);

                    }
                });


            });


        return this;
    } );
    return this;
};

Hope anyone finds this useful.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    That uses the old API, such as fnFilter etc. I would suggest that you might want to consider using the built in filter delay in 1.10 - searchDelay.

    Allan

  • DidzisDidzis Posts: 2Questions: 0Answers: 0

    Thanks, missed thisone! ;)

This discussion has been closed.