Skip to content

Commit e2b8b99

Browse files
committed
Delay
1 parent db652d6 commit e2b8b99

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

Mvc.JQuery.Datatables.Templates/Content/jquery.dataTables.columnFilter.js

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323

2424
$.fn.columnFilter = function(options) {
2525

26+
27+
var delayer = (function () {
28+
var timer = 0;
29+
return function (callback, ms) {
30+
clearTimeout(timer);
31+
timer = setTimeout(callback, ms);
32+
};
33+
});
34+
35+
options.filterDelayInMs = options.filterDelayInMs || 500;
36+
2637
var asInitVals, i, label, th;
2738

2839
//var sTableId = "table";
@@ -125,37 +136,42 @@
125136

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

@@ -187,8 +203,7 @@
187203
//var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
188204
th.html(_fnRangeLabelPart(0));
189205
var sFromId = oTable.attr("id") + '_range_from_' + i;
190-
var from = $('<input type="text" class="number_range_filter form-control" id="' + sFromId + '" value = "' + fromDatePre + '" rel="' + i + '"/>')
191-
;
206+
var from = $('<input type="text" class="number_range_filter form-control" id="' + sFromId + '" value = "' + fromDatePre + '" rel="' + i + '"/>');
192207
th.append(from);
193208
th.append(_fnRangeLabelPart(1));
194209
var sToId = oTable.attr("id") + '_range_to_' + i;
@@ -230,20 +245,19 @@
230245
}
231246
);
232247
//------------end range filtering function
248+
var delay = new delayer();
249+
$('#' + sFromId + ',#' + sToId, th).keyup(function () {
250+
var that = this;
251+
delay(function() {
252+
var iMin = document.getElementById(sFromId).value * 1;
253+
var iMax = document.getElementById(sToId).value * 1;
254+
if (iMin != 0 && iMax != 0 && iMin > iMax)
255+
return;
233256

234-
235-
$('#' + sFromId + ',#' + sToId, th).keyup(function() {
236-
237-
var iMin = document.getElementById(sFromId).value * 1;
238-
var iMax = document.getElementById(sToId).value * 1;
239-
if (iMin != 0 && iMax != 0 && iMin > iMax)
240-
return;
241-
242-
oTable.fnDraw();
243-
fnOnFiltered();
257+
oTable.fnDraw();
258+
fnOnFiltered();
259+
}, options.filterDelayInMs);
244260
});
245-
246-
247261
}
248262

249263

@@ -332,13 +346,13 @@
332346
}
333347
);
334348
//------------end date range filtering function
335-
336-
$('#' + sFromId + ',#' + sToId, th).change(function() {
337-
oTable.fnDraw();
338-
fnOnFiltered();
349+
var delay = new delayer();
350+
$('#' + sFromId + ',#' + sToId, th).change(function () {
351+
delay(function() {
352+
oTable.fnDraw();
353+
fnOnFiltered();
354+
}, 100);
339355
});
340-
341-
342356
}
343357

344358
function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel, bRegex, oSelected, bMultiselect) {

0 commit comments

Comments
 (0)