Skip to content

Commit 28be163

Browse files
committed
Resolved issue 41 added regex filters in select lists
1 parent ed95c62 commit 28be163

File tree

2 files changed

+87
-75
lines changed

2 files changed

+87
-75
lines changed

media/js/jquery.dataTables.columnFilter.js

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* File: jquery.dataTables.columnFilter.js
3-
* Version: 1.4.4.
3+
* Version: 1.4.5.
44
* Author: Jovan Popovic
55
*
66
* Copyright 2011-2012 Jovan Popovic, all rights reserved.
@@ -104,58 +104,57 @@
104104
var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
105105
var search_init = 'search_init ';
106106
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 = '';
113113
}
114-
114+
115115
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);
120120
if (bIsNumber)
121121
th.wrapInner('<span class="filter_column filter_number" />');
122122
else
123123
th.wrapInner('<span class="filter_column filter_text" />');
124+
124125
asInitVals[i] = label;
125126
var index = i;
126127

127128
if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) {
128129
input.keyup(function () {
129130
/* 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
131132
fnOnFiltered();
132133
});
133134
} else {
134135
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
159158
fnOnFiltered();
160159
});
161160
}
@@ -306,7 +305,7 @@
306305

307306
}
308307

309-
function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel) {
308+
function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel, bRegex) {
310309
if (aData == null)
311310
aData = _fnGetColumnValues(oTable.fnSettings(), iColumn, true, false, true);
312311
var index = iColumn;
@@ -317,14 +316,20 @@
317316
var iLen = aData.length;
318317
for (j = 0; j < iLen; j++) {
319318
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 '
322321
r += '<option ' + selected + ' value="' + escape(aData[j]) + '">' + aData[j] + '</option>';
323322
}
324323
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+
}
328333
}
329334
}
330335

@@ -338,12 +343,15 @@
338343
} else {
339344
$(this).addClass("search_init");
340345
}
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
342350
fnOnFiltered();
343351
});
344352
}
345353

346-
function fnCreateSelect(oTable, aData) {
354+
function fnCreateSelect(oTable, aData, bRegex) {
347355
var oSettings = oTable.fnSettings();
348356
if (aData == null && oSettings.sAjaxSource != "" && !oSettings.oFeatures.bServerSide) {
349357
// Add a function to the draw callback, which will check for the Ajax data having
@@ -355,22 +363,15 @@
355363
// Only rebuild the select on the second draw - i.e. when the Ajax
356364
// data has been loaded.
357365
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
359367
}
360368
};
361369
})(i, th, label),
362370
"sName": "column_filter_" + i
363371
});
364372
}
365373
// 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
374375

375376
}
376377

@@ -574,26 +575,22 @@
574575
var sFilterRow = "tfoot tr";
575576
if (properties.sPlaceHolder == "head:after") {
576577
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+
}
585584
sFilterRow = "thead tr:last";
586585
} else if (properties.sPlaceHolder == "head:before") {
587586
var tr = $("thead tr:first", oTable).detach();
588587
//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+
}
597594
sFilterRow = "thead tr:first";
598595
}
599596

@@ -625,14 +622,16 @@
625622
else
626623
sRangeFormat = properties.sRangeFormat;
627624
switch (aoColumn.type) {
628-
case "null":
629-
break;
625+
case "null":
626+
break;
630627
case "number":
631628
fnCreateInput(oTable, true, false, true, aoColumn.iFilterLength, aoColumn.iMaxLenght);
632629
break;
633630

634631
case "select":
635-
fnCreateSelect(oTable, aoColumn.values);
632+
if (aoColumn.bRegex != true)
633+
aoColumn.bRegex = false;
634+
fnCreateSelect(oTable, aoColumn.values, aoColumn.bRegex);
636635
break;
637636
case "number-range":
638637
fnCreateRangeInput(oTable);

regex.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@
2323
<script type="text/javascript" charset="utf-8">
2424
$(document).ready( function () {
2525
$('#example').dataTable().columnFilter({ aoColumns: [
26-
{ type: "text", bRegex:true },
26+
{ type: "text", bRegex:true },
27+
{ type: "select", bRegex:true,
28+
values: [
29+
{ value: '^(Internet|IE)', label: 'Internet Explorer(any)'},
30+
{ value: '^Mozilla', label: 'Mozilla(any)'},
31+
{ value: '^Firefox', label: 'FireFox(any)'}
32+
]
33+
},
2734
{ type: "text", bRegex:true },
2835
{ type: "text", bRegex:true },
2936
{ type: "text", bRegex:true },
@@ -592,7 +599,13 @@ <h1>Initialization code</h1>
592599
<pre> $(document).ready( function () {
593600
$('#example').dataTable().columnFilter({ aoColumns: [
594601
{ type: "text", bRegex:true },
595-
{ type: "text", bRegex:true },
602+
{ type: "select", bRegex:true,
603+
values: [
604+
{ value: '^(Internet|IE)', label: 'Internet Explorer(any)'},
605+
{ value: '^Mozilla', label: 'Mozilla(any)'},
606+
{ value: '^Firefox', label: 'FireFox(any)'}
607+
]
608+
},
596609
{ type: "text", bRegex:true },
597610
{ type: "text", bRegex:true },
598611
{ type: "text", bRegex: true }

0 commit comments

Comments
 (0)