|
1 | 1 | /* |
2 | 2 | * File: jquery.dataTables.columnFilter.js |
3 | | -* Version: 1.4.0. |
| 3 | +* Version: 1.4.1. |
4 | 4 | * Author: Jovan Popovic |
5 | 5 | * |
6 | 6 | * Copyright 2011 Jovan Popovic, all rights reserved. |
|
95 | 95 | //return oTable.fnSettings().oApi._fnColumnIndexToVisible(oTable.fnSettings(), iColumnIndex); |
96 | 96 | } |
97 | 97 |
|
98 | | - function fnCreateInput(oTable, regex, smart, bIsNumber) { |
| 98 | + function fnCreateInput(oTable, regex, smart, bIsNumber, iFilterLength) { |
99 | 99 | var sCSSClass = "text_filter"; |
100 | 100 | if (bIsNumber) |
101 | 101 | sCSSClass = "number_filter"; |
102 | 102 |
|
103 | 103 | label = label.replace(/(^\s*)|(\s*$)/g, ""); |
104 | | - |
105 | | - var input = $('<input type="text" class="search_init ' + sCSSClass + '" value="' + label + '"/>'); |
| 104 | + var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch; |
| 105 | + var search_init = 'search_init '; |
| 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 = ''; |
| 113 | + } |
| 114 | + |
| 115 | + var input = $('<input type="text" class="' + search_init + sCSSClass + '" value="' + inputvalue + '"/>'); |
106 | 116 | th.html(input); |
107 | 117 | if (bIsNumber) |
108 | 118 | th.wrapInner('<span class="filter_column filter_number" />'); |
|
119 | 129 | }); |
120 | 130 | } else { |
121 | 131 | input.keyup(function () { |
122 | | - /* Filter on the column (the index) of this element */ |
| 132 | + if(oTable.fnSettings().oFeatures.bServerSide && iFilterLength!=0) { |
| 133 | + //If filter length is set in the server-side processing mode |
| 134 | + //Check has the user entered at least iFilterLength new characters |
| 135 | + |
| 136 | + var currentFilter = oTable.fnSettings().aoPreSearchCols[index].sSearch; |
| 137 | + var iLastFilterLength = $(this).data("dt-iLastFilterLength"); |
| 138 | + if(typeof iLastFilterLength == "undefined") |
| 139 | + iLastFilterLength = 0; |
| 140 | + var iCurrentFilterLength = this.value.length; |
| 141 | + if( Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength |
| 142 | + //&& currentFilter.length == 0 //Why this? |
| 143 | + ) |
| 144 | + { |
| 145 | + //Cancel the filtering |
| 146 | + return; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + //Remember the current filter length |
| 151 | + $(this).data("dt-iLastFilterLength", iCurrentFilterLength ); |
| 152 | + } |
| 153 | + } |
| 154 | + /* Filter on the column (the index) of this element */ |
123 | 155 | oTable.fnFilter(this.value, _fnColumnIndex(index), regex, smart);//Issue 37 |
124 | 156 | fnOnFiltered(); |
125 | 157 | }); |
|
205 | 237 |
|
206 | 238 |
|
207 | 239 | function fnCreateDateRangeInput(oTable) { |
208 | | - |
209 | 240 | th.html(_fnRangeLabelPart(0)); |
210 | 241 | var sFromId = oTable.attr("id") + '_range_from_' + i; |
211 | 242 | var from = $('<input type="text" class="date_range_filter" id="' + sFromId + '" rel="' + i + '"/>'); |
|
276 | 307 | if (aData == null) |
277 | 308 | aData = _fnGetColumnValues(oTable.fnSettings(), iColumn, true, false, true); |
278 | 309 | var index = iColumn; |
| 310 | + var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch; |
| 311 | + |
279 | 312 | var r = '<select class="search_init select_filter"><option value="" class="search_init">' + sLabel + '</option>'; |
280 | 313 | var j = 0; |
281 | 314 | var iLen = aData.length; |
282 | 315 | for (j = 0; j < iLen; j++) { |
283 | 316 | if (typeof (aData[j]) != 'object') { |
284 | | - r += '<option value="' + escape(aData[j]) + '">' + aData[j] + '</option>'; |
| 317 | + var selected = ''; |
| 318 | + if(escape(aData[j]) == currentFilter) selected = 'selected ' |
| 319 | + r += '<option ' + selected + ' value="' + escape(aData[j]) + '">' + aData[j] + '</option>'; |
285 | 320 | } |
286 | 321 | else { |
287 | | - r += '<option value="' + escape(aData[j].value) + '">' + aData[j].label + '</option>'; |
| 322 | + var selected = ''; |
| 323 | + if(escape(aData[j].value) == currentFilter) selected = 'selected ' |
| 324 | + r += '<option ' + selected + 'value="' + escape(aData[j].value) + '">' + aData[j].label + '</option>'; |
288 | 325 | } |
289 | 326 | } |
290 | 327 |
|
|
522 | 559 | iFilteringDelay: 500, |
523 | 560 | aoColumns: null, |
524 | 561 | sRangeFormat: "From {from} to {to}" |
525 | | - |
526 | 562 | }; |
527 | 563 |
|
528 | 564 | properties = $.extend(defaults, options); |
|
536 | 572 | if (properties.sPlaceHolder == "head:after") { |
537 | 573 | var tr = $("thead tr:last", oTable).detach(); |
538 | 574 | tr.prependTo($("thead", oTable)); |
| 575 | + /*Fixed a bug in specifying thead:after where filters were getting intermixed with sort columns even when using datatables bSortCellsTop. Changed prependTo to appendTo*/ |
| 576 | + //tr.appendTo($("thead", oTable)); - this does not work with numberRange.html example |
539 | 577 | sFilterRow = "thead tr:last"; |
540 | 578 | } else if (properties.sPlaceHolder == "head:before") { |
541 | 579 | var tr = $("thead tr:first", oTable).detach(); |
|
548 | 586 | i = index; |
549 | 587 | var aoColumn = { type: "text", |
550 | 588 | bRegex: false, |
551 | | - bSmart: true |
| 589 | + bSmart: true, |
| 590 | + iFilterLength: 0 |
552 | 591 | }; |
553 | 592 | if (properties.aoColumns != null) { |
554 | 593 | if (properties.aoColumns.length < i || properties.aoColumns[i] == null) |
|
571 | 610 | sRangeFormat = properties.sRangeFormat |
572 | 611 | switch (aoColumn.type) { |
573 | 612 | case "number": |
574 | | - fnCreateInput(oTable, true, false, true); |
| 613 | + fnCreateInput(oTable, true, false, true, aoColumn.iFilterLength); |
575 | 614 | break; |
576 | 615 |
|
577 | 616 | case "select": |
|
590 | 629 | default: |
591 | 630 | bRegex = (aoColumn.bRegex == null ? false : aoColumn.bRegex); |
592 | 631 | bSmart = (aoColumn.bSmart == null ? false : aoColumn.bSmart); |
593 | | - fnCreateInput(oTable, bRegex, bSmart, false); |
| 632 | + fnCreateInput(oTable, bRegex, bSmart, false, aoColumn.iFilterLength); |
594 | 633 | break; |
595 | 634 |
|
596 | 635 | } |
|
0 commit comments