|
23 | 23 |
|
24 | 24 | $.fn.columnFilter = function(options) { |
25 | 25 |
|
| 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 | + |
26 | 37 | var asInitVals, i, label, th; |
27 | 38 |
|
28 | 39 | //var sTableId = "table"; |
|
125 | 136 |
|
126 | 137 | asInitVals[i] = label; |
127 | 138 | var index = i; |
128 | | - |
| 139 | + var delay = new delayer(); |
129 | 140 | if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) { |
130 | 141 | 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); |
134 | 148 | }); |
135 | 149 | } else { |
136 | 150 | 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 | + } |
154 | 170 | } |
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); |
159 | 175 | }); |
160 | 176 | } |
161 | 177 |
|
|
187 | 203 | //var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch; |
188 | 204 | th.html(_fnRangeLabelPart(0)); |
189 | 205 | 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 + '"/>'); |
192 | 207 | th.append(from); |
193 | 208 | th.append(_fnRangeLabelPart(1)); |
194 | 209 | var sToId = oTable.attr("id") + '_range_to_' + i; |
|
230 | 245 | } |
231 | 246 | ); |
232 | 247 | //------------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; |
233 | 256 |
|
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); |
244 | 260 | }); |
245 | | - |
246 | | - |
247 | 261 | } |
248 | 262 |
|
249 | 263 |
|
|
332 | 346 | } |
333 | 347 | ); |
334 | 348 | //------------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); |
339 | 355 | }); |
340 | | - |
341 | | - |
342 | 356 | } |
343 | 357 |
|
344 | 358 | function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel, bRegex, oSelected, bMultiselect) { |
|
0 commit comments