Skip to content

Commit 6bf57b6

Browse files
committed
Added delay before firing off ajax requests
1 parent c5716c9 commit 6bf57b6

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

media/js/jquery.dataTables.columnFilter.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
*/
2121
(function($) {
2222

23+
var Delayer = (function () {
24+
var timer = 0;
25+
return function (callback, ms) {
26+
clearTimeout(timer);
27+
timer = setTimeout(callback, ms);
28+
};
29+
});
30+
2331

2432
$.fn.columnFilter = function(options) {
2533
var columnBuilders = $.merge({}, options.columnBuilders || {});
@@ -126,35 +134,43 @@
126134
var index = i;
127135

128136
if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) {
137+
var delayer = new Delayer();
129138
input.keyup(function() {
130-
/* Filter on the column all numbers that starts with the entered value */
131-
oTable.fnFilter('^' + this.value, _fnColumnIndex(index), true, false); //Issue 37
132-
fnOnFiltered();
139+
var that = this;
140+
delayer.delay(function() {
141+
/* Filter on the column all numbers that starts with the entered value */
142+
oTable.fnFilter('^' + that.value, _fnColumnIndex(index), true, false); //Issue 37
143+
fnOnFiltered();
144+
}, properties.iFilteringDelay);
133145
});
134146
} else {
147+
var delayer = new Delayer();
135148
input.keyup(function() {
136-
if (oTable.fnSettings().oFeatures.bServerSide && iFilterLength != 0) {
137-
//If filter length is set in the server-side processing mode
138-
//Check has the user entered at least iFilterLength new characters
139-
140-
var currentFilter = oTable.fnSettings().aoPreSearchCols[index].sSearch;
141-
var iLastFilterLength = $(this).data("dt-iLastFilterLength");
142-
if (typeof iLastFilterLength == "undefined")
143-
iLastFilterLength = 0;
144-
var iCurrentFilterLength = this.value.length;
145-
if (Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength
146-
//&& currentFilter.length == 0 //Why this?
147-
) {
148-
//Cancel the filtering
149-
return;
150-
} else {
151-
//Remember the current filter length
152-
$(this).data("dt-iLastFilterLength", iCurrentFilterLength);
149+
var that = this;
150+
delayer(function() {
151+
if (oTable.fnSettings().oFeatures.bServerSide && iFilterLength != 0) {
152+
//If filter length is set in the server-side processing mode
153+
//Check has the user entered at least iFilterLength new characters
154+
155+
var currentFilter = oTable.fnSettings().aoPreSearchCols[index].sSearch;
156+
var iLastFilterLength = $(that).data("dt-iLastFilterLength");
157+
if (typeof iLastFilterLength == "undefined")
158+
iLastFilterLength = 0;
159+
var iCurrentFilterLength = that.value.length;
160+
if (Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength
161+
//&& currentFilter.length == 0 //Why this?
162+
) {
163+
//Cancel the filtering
164+
return;
165+
} else {
166+
//Remember the current filter length
167+
$(that).data("dt-iLastFilterLength", iCurrentFilterLength);
168+
}
153169
}
154-
}
155-
/* Filter on the column (the index) of this element */
156-
oTable.fnFilter(this.value, _fnColumnIndex(index), regex, smart); //Issue 37
157-
fnOnFiltered();
170+
/* Filter on the column (the index) of this element */
171+
oTable.fnFilter(that.value, _fnColumnIndex(index), regex, smart); //Issue 37
172+
fnOnFiltered();
173+
}, properties.iFilteringDelay);
158174
});
159175
}
160176

@@ -229,19 +245,18 @@
229245
);
230246
//------------end range filtering function
231247

232-
248+
var delay = new Delayer();
233249
$('#' + sFromId + ',#' + sToId, th).keyup(function() {
250+
delay(function() {
251+
var iMin = document.getElementById(sFromId).value * 1;
252+
var iMax = document.getElementById(sToId).value * 1;
253+
if (iMin != 0 && iMax != 0 && iMin > iMax)
254+
return;
234255

235-
var iMin = document.getElementById(sFromId).value * 1;
236-
var iMax = document.getElementById(sToId).value * 1;
237-
if (iMin != 0 && iMax != 0 && iMin > iMax)
238-
return;
239-
240-
oTable.fnDraw();
241-
fnOnFiltered();
256+
oTable.fnDraw();
257+
fnOnFiltered();
258+
}, properties.iFilteringDelay);
242259
});
243-
244-
245260
}
246261

247262

@@ -332,10 +347,12 @@
332347
}
333348
);
334349
//------------end date range filtering function
335-
350+
var delayer = new Delayer()
336351
$('#' + sFromId + ',#' + sToId, th).change(function() {
337-
oTable.fnDraw();
338-
fnOnFiltered();
352+
delayer(function() {
353+
oTable.fnDraw();
354+
fnOnFiltered();
355+
}, properties.iFilteringDelay);
339356
});
340357

341358

0 commit comments

Comments
 (0)