|
1 | 1 | /* |
2 | 2 | * File: jquery.dataTables.columnFilter.js |
3 | | -* Version: 1.4.4. |
| 3 | +* Version: 1.4.5. |
4 | 4 | * Author: Jovan Popovic |
5 | 5 | * |
6 | 6 | * Copyright 2011-2012 Jovan Popovic, all rights reserved. |
|
104 | 104 | var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch; |
105 | 105 | var search_init = 'search_init '; |
106 | 106 | var inputvalue = label; |
107 | | - if(currentFilter != '' && currentFilter != '^') { |
108 | | - if(bIsNumber && currentFilter.charAt(0) == '^') |
109 | | - inputvalue = currentFilter.substr(1); //ignore trailing ^ |
110 | | - else |
111 | | - inputvalue = currentFilter; |
112 | | - search_init = ''; |
| 107 | + if (currentFilter != '' && currentFilter != '^') { |
| 108 | + if (bIsNumber && currentFilter.charAt(0) == '^') |
| 109 | + inputvalue = currentFilter.substr(1); //ignore trailing ^ |
| 110 | + else |
| 111 | + inputvalue = currentFilter; |
| 112 | + search_init = ''; |
113 | 113 | } |
114 | | - |
| 114 | + |
115 | 115 | var input = $('<input type="text" class="' + search_init + sCSSClass + '" value="' + inputvalue + '"/>'); |
116 | | - if(iMaxLenght!= undefined && iMaxLenght != -1){ |
117 | | - input.attr('maxlength', iMaxLenght); |
118 | | - } |
119 | | - th.html(input); |
| 116 | + if (iMaxLenght != undefined && iMaxLenght != -1) { |
| 117 | + input.attr('maxlength', iMaxLenght); |
| 118 | + } |
| 119 | + th.html(input); |
120 | 120 | if (bIsNumber) |
121 | 121 | th.wrapInner('<span class="filter_column filter_number" />'); |
122 | 122 | else |
123 | 123 | th.wrapInner('<span class="filter_column filter_text" />'); |
| 124 | + |
124 | 125 | asInitVals[i] = label; |
125 | 126 | var index = i; |
126 | 127 |
|
127 | 128 | if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) { |
128 | 129 | input.keyup(function () { |
129 | 130 | /* Filter on the column all numbers that starts with the entered value */ |
130 | | - oTable.fnFilter('^' + this.value, _fnColumnIndex(index), true, false);//Issue 37 |
| 131 | + oTable.fnFilter('^' + this.value, _fnColumnIndex(index), true, false); //Issue 37 |
131 | 132 | fnOnFiltered(); |
132 | 133 | }); |
133 | 134 | } else { |
134 | 135 | input.keyup(function () { |
135 | | - if(oTable.fnSettings().oFeatures.bServerSide && iFilterLength!=0) { |
136 | | - //If filter length is set in the server-side processing mode |
137 | | - //Check has the user entered at least iFilterLength new characters |
138 | | - |
139 | | - var currentFilter = oTable.fnSettings().aoPreSearchCols[index].sSearch; |
140 | | - var iLastFilterLength = $(this).data("dt-iLastFilterLength"); |
141 | | - if(typeof iLastFilterLength == "undefined") |
142 | | - iLastFilterLength = 0; |
143 | | - var iCurrentFilterLength = this.value.length; |
144 | | - if( Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength |
145 | | - //&& currentFilter.length == 0 //Why this? |
146 | | - ) |
147 | | - { |
148 | | - //Cancel the filtering |
149 | | - return; |
150 | | - } |
151 | | - else |
152 | | - { |
153 | | - //Remember the current filter length |
154 | | - $(this).data("dt-iLastFilterLength", iCurrentFilterLength ); |
155 | | - } |
156 | | - } |
157 | | - /* Filter on the column (the index) of this element */ |
158 | | - oTable.fnFilter(this.value, _fnColumnIndex(index), regex, smart);//Issue 37 |
| 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 | + } |
| 151 | + else { |
| 152 | + //Remember the current filter length |
| 153 | + $(this).data("dt-iLastFilterLength", iCurrentFilterLength); |
| 154 | + } |
| 155 | + } |
| 156 | + /* Filter on the column (the index) of this element */ |
| 157 | + oTable.fnFilter(this.value, _fnColumnIndex(index), regex, smart); //Issue 37 |
159 | 158 | fnOnFiltered(); |
160 | 159 | }); |
161 | 160 | } |
|
306 | 305 |
|
307 | 306 | } |
308 | 307 |
|
309 | | - function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel) { |
| 308 | + function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel, bRegex) { |
310 | 309 | if (aData == null) |
311 | 310 | aData = _fnGetColumnValues(oTable.fnSettings(), iColumn, true, false, true); |
312 | 311 | var index = iColumn; |
|
317 | 316 | var iLen = aData.length; |
318 | 317 | for (j = 0; j < iLen; j++) { |
319 | 318 | if (typeof (aData[j]) != 'object') { |
320 | | - var selected = ''; |
321 | | - if(escape(aData[j]) == currentFilter) selected = 'selected ' |
| 319 | + var selected = ''; |
| 320 | + if (escape(aData[j]) == currentFilter) selected = 'selected ' |
322 | 321 | r += '<option ' + selected + ' value="' + escape(aData[j]) + '">' + aData[j] + '</option>'; |
323 | 322 | } |
324 | 323 | else { |
325 | | - var selected = ''; |
326 | | - if(escape(aData[j].value) == currentFilter) selected = 'selected ' |
327 | | - r += '<option ' + selected + 'value="' + escape(aData[j].value) + '">' + aData[j].label + '</option>'; |
| 324 | + var selected = ''; |
| 325 | + if (bRegex) { |
| 326 | + //Do not escape values if they are explicitely set to avoid escaping special characters in the regexp |
| 327 | + if (aData[j].value == currentFilter) selected = 'selected '; |
| 328 | + r += '<option ' + selected + 'value="' + aData[j].value + '">' + aData[j].label + '</option>'; |
| 329 | + } else { |
| 330 | + if (escape(aData[j].value) == currentFilter) selected = 'selected '; |
| 331 | + r += '<option ' + selected + 'value="' + escape(aData[j].value) + '">' + aData[j].label + '</option>'; |
| 332 | + } |
328 | 333 | } |
329 | 334 | } |
330 | 335 |
|
|
338 | 343 | } else { |
339 | 344 | $(this).addClass("search_init"); |
340 | 345 | } |
341 | | - oTable.fnFilter(unescape($(this).val()), iColumn); //Issue 37 |
| 346 | + if (bRegex) |
| 347 | + oTable.fnFilter($(this).val(), iColumn, bRegex); //Issue 41 |
| 348 | + else |
| 349 | + oTable.fnFilter(unescape($(this).val()), iColumn); //Issue 25 |
342 | 350 | fnOnFiltered(); |
343 | 351 | }); |
344 | 352 | } |
345 | 353 |
|
346 | | - function fnCreateSelect(oTable, aData) { |
| 354 | + function fnCreateSelect(oTable, aData, bRegex) { |
347 | 355 | var oSettings = oTable.fnSettings(); |
348 | 356 | if (aData == null && oSettings.sAjaxSource != "" && !oSettings.oFeatures.bServerSide) { |
349 | 357 | // Add a function to the draw callback, which will check for the Ajax data having |
|
355 | 363 | // Only rebuild the select on the second draw - i.e. when the Ajax |
356 | 364 | // data has been loaded. |
357 | 365 | if (oSettings.iDraw == 2 && oSettings.sAjaxSource != null && oSettings.sAjaxSource != "" && !oSettings.oFeatures.bServerSide) { |
358 | | - return fnCreateColumnSelect(oTable, null, _fnColumnIndex(iColumn), nTh, sLabel); //Issue 37 |
| 366 | + return fnCreateColumnSelect(oTable, null, _fnColumnIndex(iColumn), nTh, sLabel, bRegex); //Issue 37 |
359 | 367 | } |
360 | 368 | }; |
361 | 369 | })(i, th, label), |
362 | 370 | "sName": "column_filter_" + i |
363 | 371 | }); |
364 | 372 | } |
365 | 373 | // Regardless of the Ajax state, build the select on first pass |
366 | | - fnCreateColumnSelect(oTable, aData, _fnColumnIndex(i), th, label); //Issue 37 |
367 | | - |
368 | | - /* |
369 | | - var fnOnFilteredCurrent = fnOnFiltered; |
370 | | - fnOnFiltered = function(){ |
371 | | - fnCreateColumnSelect(oTable, aData, i, th, label); |
372 | | - fnOnFilteredCurrent(); |
373 | | - };*/ |
| 374 | + fnCreateColumnSelect(oTable, aData, _fnColumnIndex(i), th, label, bRegex); //Issue 37 |
374 | 375 |
|
375 | 376 | } |
376 | 377 |
|
|
574 | 575 | var sFilterRow = "tfoot tr"; |
575 | 576 | if (properties.sPlaceHolder == "head:after") { |
576 | 577 | var tr = $("thead tr:last", oTable).detach(); |
577 | | - if(oTable.fnSettings().bSortCellsTop) |
578 | | - { |
579 | | - tr.appendTo($("thead", oTable)); |
580 | | - } |
581 | | - else |
582 | | - { |
583 | | - tr.prependTo($("thead", oTable)); |
584 | | - } |
| 578 | + if (oTable.fnSettings().bSortCellsTop) { |
| 579 | + tr.appendTo($("thead", oTable)); |
| 580 | + } |
| 581 | + else { |
| 582 | + tr.prependTo($("thead", oTable)); |
| 583 | + } |
585 | 584 | sFilterRow = "thead tr:last"; |
586 | 585 | } else if (properties.sPlaceHolder == "head:before") { |
587 | 586 | var tr = $("thead tr:first", oTable).detach(); |
588 | 587 | //tr.attr("id", 1); |
589 | | - if(oTable.fnSettings().bSortCellsTop) |
590 | | - { |
591 | | - tr.appendTo($("thead", oTable)); |
592 | | - } |
593 | | - else |
594 | | - { |
595 | | - tr.prependTo($("thead", oTable)); |
596 | | - } |
| 588 | + if (oTable.fnSettings().bSortCellsTop) { |
| 589 | + tr.appendTo($("thead", oTable)); |
| 590 | + } |
| 591 | + else { |
| 592 | + tr.prependTo($("thead", oTable)); |
| 593 | + } |
597 | 594 | sFilterRow = "thead tr:first"; |
598 | 595 | } |
599 | 596 |
|
|
625 | 622 | else |
626 | 623 | sRangeFormat = properties.sRangeFormat; |
627 | 624 | switch (aoColumn.type) { |
628 | | - case "null": |
629 | | - break; |
| 625 | + case "null": |
| 626 | + break; |
630 | 627 | case "number": |
631 | 628 | fnCreateInput(oTable, true, false, true, aoColumn.iFilterLength, aoColumn.iMaxLenght); |
632 | 629 | break; |
633 | 630 |
|
634 | 631 | case "select": |
635 | | - fnCreateSelect(oTable, aoColumn.values); |
| 632 | + if (aoColumn.bRegex != true) |
| 633 | + aoColumn.bRegex = false; |
| 634 | + fnCreateSelect(oTable, aoColumn.values, aoColumn.bRegex); |
636 | 635 | break; |
637 | 636 | case "number-range": |
638 | 637 | fnCreateRangeInput(oTable); |
|
0 commit comments