forked from community/device-mgt-core
DataTables library instantiates an ajax request for each key release when searching/filtering. in a system with a larger dataset, many requests are made to the back end for a longer keyword search which is a huge overload. Also, sometimes since the request initialized for the first character returns the largest subset of data (if available) this request may take a longer time to process. Since DataTables update the table in the order that the response comes, the final result shown would be the last response received which can be incorrect. This commit resolves the above issue by enabling a delay before the requests made using DataTables fnSetFilteringDelay plugin.revert-70aa11f8
parent
7620d7ea5d
commit
6b8d555314
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Enables filtration delay for keeping the browser more responsive while
|
||||
* searching for a longer keyword.
|
||||
*
|
||||
* This can be particularly useful when working with server-side processing,
|
||||
* where you wouldn't typically want an Ajax request to be made with every key
|
||||
* press the user makes when searching the table.
|
||||
*
|
||||
* Please note that this plug-in has been deprecated and the `dt-init
|
||||
* searchDelay` option in DataTables 1.10 should now be used. This plug-in will
|
||||
* not operate with v1.10+.
|
||||
*
|
||||
* @name fnSetFilteringDelay
|
||||
* @summary Add a key debouce delay to the global filtering input of a table
|
||||
* @author [Zygimantas Berziunas](http://www.zygimantas.com/),
|
||||
* [Allan Jardine](http://www.sprymedia.co.uk/) and _vex_
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
* $('.dataTable').dataTable().fnSetFilteringDelay();
|
||||
* } );
|
||||
*/
|
||||
|
||||
jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function (oSettings, iDelay) {
|
||||
var _that = this;
|
||||
|
||||
if (iDelay === undefined) {
|
||||
iDelay = 250;
|
||||
}
|
||||
|
||||
this.each(function (i) {
|
||||
if (typeof _that.fnSettings().aanFeatures.f !== 'undefined') {
|
||||
$.fn.dataTableExt.iApiIndex = i;
|
||||
var
|
||||
oTimerId = null,
|
||||
sPreviousSearch = null,
|
||||
anControl = $('input', _that.fnSettings().aanFeatures.f);
|
||||
|
||||
anControl.unbind('keyup search input').bind('keyup search input', function () {
|
||||
|
||||
if (sPreviousSearch === null || sPreviousSearch != anControl.val()) {
|
||||
window.clearTimeout(oTimerId);
|
||||
sPreviousSearch = anControl.val();
|
||||
oTimerId = window.setTimeout(function () {
|
||||
$.fn.dataTableExt.iApiIndex = i;
|
||||
_that.fnFilter(anControl.val());
|
||||
}, iDelay);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
});
|
||||
return this;
|
||||
};
|
Loading…
Reference in new issue