mirror of
https://github.com/DataTables/DataTables.git
synced 2025-03-15 16:29:16 +01:00
New: Built in filtering will throttle calls to the server when using
server-side processing - A popular plug-in for DataTables is the `fnSetFilteringDelay` plug-in, which provides a bit of a buffer when typing into the filter input so you don't make an Ajax request for every key stroke - i.e. don't DDoS your own server! - Since we have _fnThrottle built in now for the scrolling, we can also use it to provide this buffering functionality for server-side processing filtering in the core. A couple of small changes for _fnThrottle were required to get the callback context correct and ot call it not immediately, but only after the delay
This commit is contained in:
parent
83f30cec8f
commit
2c4cc4fd1b
@ -1 +1 @@
|
|||||||
38e760a00f96112e374d286ba7302065abc07733
|
a931f2b4aeb3f699cbda26245bdc590079044eed
|
||||||
|
68
media/js/jquery.dataTables.js
vendored
68
media/js/jquery.dataTables.js
vendored
@ -2552,27 +2552,33 @@
|
|||||||
} )
|
} )
|
||||||
.append( $('<label/>' ).append( str ) );
|
.append( $('<label/>' ).append( str ) );
|
||||||
|
|
||||||
|
var searchFn = function() {
|
||||||
|
/* Update all other filter input elements for the new display */
|
||||||
|
var n = features.f;
|
||||||
|
var val = !this.value ? "" : this.value; // mental IE8 fix :-(
|
||||||
|
|
||||||
|
/* Now do the filter */
|
||||||
|
if ( val != previousSearch.sSearch ) {
|
||||||
|
_fnFilterComplete( settings, {
|
||||||
|
"sSearch": val,
|
||||||
|
"bRegex": previousSearch.bRegex,
|
||||||
|
"bSmart": previousSearch.bSmart ,
|
||||||
|
"bCaseInsensitive": previousSearch.bCaseInsensitive
|
||||||
|
} );
|
||||||
|
|
||||||
|
// Need to redraw, without resorting
|
||||||
|
settings._iDisplayStart = 0;
|
||||||
|
_fnDraw( settings );
|
||||||
|
}
|
||||||
|
};
|
||||||
var jqFilter = $('input[type="search"]', filter)
|
var jqFilter = $('input[type="search"]', filter)
|
||||||
.val( previousSearch.sSearch.replace('"','"') )
|
.val( previousSearch.sSearch.replace('"','"') )
|
||||||
.bind( 'keyup.DT search.DT input.DT paste.DT cut.DT', function(e) {
|
.bind(
|
||||||
/* Update all other filter input elements for the new display */
|
'keyup.DT search.DT input.DT paste.DT cut.DT',
|
||||||
var n = features.f;
|
_fnDataSource( settings ) === 'ssp' ?
|
||||||
var val = !this.value ? "" : this.value; // mental IE8 fix :-(
|
_fnThrottle( searchFn, 400 ):
|
||||||
|
searchFn
|
||||||
/* Now do the filter */
|
)
|
||||||
if ( val != previousSearch.sSearch ) {
|
|
||||||
_fnFilterComplete( settings, {
|
|
||||||
"sSearch": val,
|
|
||||||
"bRegex": previousSearch.bRegex,
|
|
||||||
"bSmart": previousSearch.bSmart ,
|
|
||||||
"bCaseInsensitive": previousSearch.bCaseInsensitive
|
|
||||||
} );
|
|
||||||
|
|
||||||
// Need to redraw, without resorting
|
|
||||||
settings._iDisplayStart = 0;
|
|
||||||
_fnDraw( settings );
|
|
||||||
}
|
|
||||||
} )
|
|
||||||
.bind( 'keypress.DT', function(e) {
|
.bind( 'keypress.DT', function(e) {
|
||||||
/* Prevent form submission */
|
/* Prevent form submission */
|
||||||
if ( e.keyCode == 13 ) {
|
if ( e.keyCode == 13 ) {
|
||||||
@ -3954,28 +3960,40 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function _fnThrottle( fn ) {
|
/**
|
||||||
|
* Throttle the calls to a function. Arguments and context are maintained for
|
||||||
|
* the throttled function
|
||||||
|
* @param {function} fn Function to be called
|
||||||
|
* @param {int} [freq=200] call frequency in mS
|
||||||
|
* @returns {function} wrapped function
|
||||||
|
* @memberof DataTable#oApi
|
||||||
|
*/
|
||||||
|
function _fnThrottle( fn, freq ) {
|
||||||
var
|
var
|
||||||
frequency = 200,
|
frequency = freq || 200,
|
||||||
last,
|
last,
|
||||||
timer;
|
timer;
|
||||||
|
|
||||||
return function () {
|
return function () {
|
||||||
var
|
var
|
||||||
now = +new Date(),
|
that = this,
|
||||||
|
now = +new Date(),
|
||||||
args = arguments;
|
args = arguments;
|
||||||
|
|
||||||
if ( last && now < last + frequency ) {
|
if ( last && now < last + frequency ) {
|
||||||
clearTimeout( timer );
|
clearTimeout( timer );
|
||||||
|
|
||||||
timer = setTimeout( function () {
|
timer = setTimeout( function () {
|
||||||
last = now;
|
last = undefined;
|
||||||
fn();
|
fn.apply( that, args );
|
||||||
}, frequency );
|
}, frequency );
|
||||||
}
|
}
|
||||||
|
else if ( last ) {
|
||||||
|
last = now;
|
||||||
|
fn.apply( that, args );
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
last = now;
|
last = now;
|
||||||
fn();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user