mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
Filter: add defaultFilter option. Fixes #704
This commit is contained in:
parent
dbecda238c
commit
b497cfb1a4
@ -64,7 +64,8 @@ p.tip em, div.tip em,.label-info {padding: 2px; background-color: #5bc0de; color
|
||||
span.tip em, .label-success { background-color: #5cb85c; }
|
||||
.updated .label-success { background-color: #8eCe8e; }
|
||||
span.tip.old em, .label-default { background-color: #999; }
|
||||
span.warn em, .label.alert { background-color: #d9534f; }
|
||||
span.warning { background-color: #f0ad4e; } /* orange */
|
||||
span.warn em, .label.alert { background-color: #d9534f; } /* red */
|
||||
.label.warning { background-color: #f0ad4e; }
|
||||
tr td.lookhere, span.lookhere { background-color: rgba(230,191,153,0.5); }
|
||||
div.ui-slider .ui-slider-handle { width: 0.8em; height: 0.8em; }
|
||||
|
@ -30,6 +30,8 @@
|
||||
// Set to use a jQuery selector (or jQuery object) pointing to the
|
||||
// external filter (column specific or any match)
|
||||
filter_external : '.search',
|
||||
// add a default type search to the first name column
|
||||
filter_defaultFilter: { 1 : '~{query}' },
|
||||
// include column filters
|
||||
filter_columnFilters: true,
|
||||
filter_placeholder: { search : 'Search...' },
|
||||
@ -57,6 +59,7 @@
|
||||
<h3><a href="#">Notes</a></h3>
|
||||
<div>
|
||||
<ul>
|
||||
<li>In <span class="version">v2.17.8</span>, added a default fuzzy search to the "First Name" column.</li>
|
||||
<li>This is a demo of the <a href="index.html#widget-filter-external"><code>filter_external</code></a> option (added <span class="version">v2.15</span>).</li>
|
||||
<li>In <span class="version">v2.15</span>
|
||||
<ul>
|
||||
@ -108,7 +111,7 @@ $('table').trigger('search', [ columns ]);</pre>
|
||||
<h1>Demo</h1>
|
||||
|
||||
<div id="demo"><input class="search" type="search" data-column="all"> (Match any column)<br>
|
||||
<input class="search" type="search" data-column="1"> (First Name)<br>
|
||||
<input class="search" type="search" data-column="1"> (First Name; fuzzy search... try "be")<br>
|
||||
|
||||
<!-- targeted by the "filter_reset" option -->
|
||||
<button type="button" class="reset">Reset Search</button>
|
||||
@ -117,7 +120,7 @@ $('table').trigger('search', [ columns ]);</pre>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Rank</th>
|
||||
<th>First Name</th>
|
||||
<th data-placeholder="Fuzzy search">First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Age</th>
|
||||
<th>Total</th>
|
||||
|
@ -30,27 +30,28 @@
|
||||
</script>
|
||||
|
||||
<script id="js">// *** Filter search type function arguments ***
|
||||
// filter = filter input value for a column; iFilter = same as filter, except lowercase
|
||||
// exact = table cell text (or parsed data, if column parser enabled)
|
||||
// iExact = same as exact, except lowercase
|
||||
// data.filter = filter input value for a column; iFilter = same as filter, except lowercase
|
||||
// data.exact = table cell text (or parsed data, if column parser enabled)
|
||||
// data.iExact = same as exact, except lowercase
|
||||
|
||||
// search for a match from the beginning of a string
|
||||
// "^l" matches "lion" but not "koala"
|
||||
$.tablesorter.filter.types.start = function( filter, iFilter, exact, iExact ) {
|
||||
if ( /^\^/.test( iFilter ) ) {
|
||||
return iExact.indexOf( iFilter.substring(1) ) === 0;
|
||||
$.tablesorter.filter.types.start = function( config, data ) {
|
||||
if ( /^\^/.test( data.iFilter ) ) {
|
||||
return data.iExact.indexOf( data.iFilter.substring(1) ) === 0;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
// search for a match at the end of a string
|
||||
// "a$" matches "Llama" but not "aardvark"
|
||||
$.tablesorter.filter.types.end = function( filter, iFilter, exact, iExact ) {
|
||||
if ( /\$$/.test( iFilter ) ) {
|
||||
var fl = iFilter.length - 1,
|
||||
f = iFilter.substring(0, fl),
|
||||
el = iExact.length;
|
||||
return iExact.lastIndexOf(f) + fl === el;
|
||||
$.tablesorter.filter.types.end = function( config, data ) {
|
||||
if ( /\$$/.test( data.iFilter ) ) {
|
||||
var filter = data.iFilter,
|
||||
filterLength = filter.length - 1,
|
||||
removedSymbol = filter.substring(0, filterLength),
|
||||
exactLength = data.iExact.length;
|
||||
return data.iExact.lastIndexOf(removedSymbol) + filterLength === exactLength;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@ -98,6 +99,7 @@ $(function() {
|
||||
<h3><a href="#">Notes</a></h3>
|
||||
<div>
|
||||
<ul>
|
||||
<li><span class="label warning">Notice!</span> In version <span class="version">v2.17.8</span>, some drastic changes were made to the way variables are passed to the filters. Please check out the "How to add Custom Filter types" section below.</li>
|
||||
<li>This demo was added in <span class="version">v2.17.5</span>, but modification using these instructions works for v2.13.3+; when the filter widget was restructured to allow the adding of custom filter search types.</li>
|
||||
<li>In this demo, two additional search types have been added as an example
|
||||
<ul>
|
||||
@ -137,16 +139,35 @@ $(function() {
|
||||
<li>Should it look for a symbol at the beginning, middle or end of the filter?</li>
|
||||
<li>Does the designator need spaces around it (e.g. " and " or " or ") to prevent problems? You wouldn't be able to find the country of "Andorra" if the "and" or "or" designators didn't require spaces.</li>
|
||||
<li>Make sure to name your filter so that it doesn't interfere with already existing ones, unless your filter is meant to replace an existing one. See the "Built-in Filters" section for a full list of filter function names.</li>
|
||||
<li>Changed in <span class="version updated">v2.17.8</span>, only two parameters are passed to the filter type function:
|
||||
<pre class="prettyprint lang-js">$.tablesorter.filter.types.myNewFilter = function( config, data ) { /* ... */ }</pre>
|
||||
<ul>
|
||||
<li><code>config</code> - tablesorter config containing all variables (<code>table.config</code>)</li>
|
||||
<li><code>data</code> - filter data containing all filter variables. All variables are listed below.</li>
|
||||
</ul>
|
||||
<br>
|
||||
</li>
|
||||
<li>Within your filter, first test for your designator symbol.
|
||||
<ul>
|
||||
<li>If it exists within the filter, then do your comparison and return a boolean of <code>true</code> or <code>false</code>.</li>
|
||||
<li>Four arguments are passed to the filter function:
|
||||
<li>The following arguments are passed to the filter function within the <code>data</code> parameter (changed in <span class="version updated">v2.17.8</span>):
|
||||
<ul>
|
||||
<li><code>filter</code> - The exact text from the filter input (e.g. <code>^h</code>).</li>
|
||||
<li><code>iFilter</code> - The text from the filter in all lower case for case insensitive searches.</li>
|
||||
<li><code>exact</code> - The exact (or parsed) text from the current table cell; the parsed text is passed when the column has a <code>"filter-parsed"</code> class name set.</li>
|
||||
<li><code>iExact</code> - The exact (or parsed) text in all lower case for case insensitive searches.</li>
|
||||
<li><code>data.filter</code> - The exact text from the filter input (e.g. <code>^h</code>).</li>
|
||||
<li><code>data.iFilter</code> - The text from the filter in all lower case for case insensitive searches.</li>
|
||||
<li><code>data.exact</code> - The exact (or parsed) text from the current table cell, or the entire row if <code>data.anyMatch</code> is <code>true</code>; the parsed text is passed when the column has a <code>"filter-parsed"</code> class name set.</li>
|
||||
<li><code>data.iExact</code> - The exact (or parsed) text in all lower case for case insensitive searches.<br><br></li>
|
||||
<li><code>data.index</code> - The current column index (zero-based) being filtered. When performing an "any match", this index is equal to <code>config.columns</code> which is the last column of the table plus one.</li>
|
||||
<li><code>data.cache</code> - The parsed text from the current table cell, or the entire row if <code>data.anyMatch</code> is <code>true</code>. This value will be in all lower case if <code>config.ignoreCase</code> is <code>true</code>.<br><br></li>
|
||||
|
||||
<li><code>data.anyMatch</code> - This is a boolean value indicating when the <code>data.exact</code> and <code>data.iExact</code> parameters contain data from the entire row instead of one cell. This value will always be <code>false</code> if the table does not include an associated any match filter.<br><br></li>
|
||||
|
||||
<li><code>data.rowArray</code> - An array of the exact text from each table cell.</li>
|
||||
<li><code>data.cacheArray</code> - An array of parsed text from each table cell.</li>
|
||||
<li><code>data.childRowText</code> - contains all text from any associated child rows.</li>
|
||||
|
||||
<li><code>data.parsed</code> - An array of boolean flags that indicate if the column data should obtained from parsed values, or not; obtained from <code>filter_useParsedData</code> setting or <code>filter-parsed</code> classes on the header cells.</li>
|
||||
</ul>
|
||||
<br>
|
||||
</li>
|
||||
<li>If your designator doesn't exist, you *must* return <code>null</code> to allow comparisons with other filter types.</li>
|
||||
</ul>
|
||||
@ -154,17 +175,17 @@ $(function() {
|
||||
<li>Here is a basic example with extensive comments:
|
||||
<pre class="prettyprint lang-js">// search for a match from the beginning of a string
|
||||
// "^l" matches "lion" but not "koala"
|
||||
$.tablesorter.filter.types.start = function( filter, iFilter, exact, iExact ) {
|
||||
$.tablesorter.filter.types.start = function( config, data ) {
|
||||
// test for filter type designator. In this example, "^" must be at the beginning of the filter
|
||||
// for this test, the use of the case insensitive "iFilter" variable
|
||||
// doesn't matter since we are only looking at the symbol
|
||||
if ( /^\^/.test( iFilter ) ) {
|
||||
if ( /^\^/.test( data.iFilter ) ) {
|
||||
// test the table content (exact or parsed) against the filter
|
||||
// * Don't forget to remove the designator first ( the substring(1) part ), so "^h".substring(1) becomes "h"
|
||||
// * In this case, we do care about using "iFilter" since we're comparing it with "iExact"
|
||||
// * We use "indexOf" so that we know the match starts from the beginning of the string.
|
||||
// * Your function should return a boolean indicating a match, or not.
|
||||
return iExact.indexOf( iFilter.substring(1) ) === 0;
|
||||
return data.iExact.indexOf( data.iFilter.substring(1) ) === 0;
|
||||
}
|
||||
// ALWAYS return null if your custom filter type doesn't match
|
||||
return null;
|
||||
|
@ -479,7 +479,7 @@
|
||||
<li><a href="example-widget-filter-any-match.html">external option (match any column)</a> (<span class="version">v2.13.3</span>; <span class="version updated">v2.15</span>)</li>
|
||||
<li><a href="example-widget-filter-external-inputs.html">external inputs</a> (<span class="version">v2.14</span>; <span class="version updated">v2.15</span>)</li>
|
||||
<li><a href="example-widget-filter-custom.html">custom</a> (v2.3.6; <span class="version updated">v2.10.1</span>)</li>
|
||||
<li><a href="example-widget-filter-custom-search.html">custom searches</a> (<span class="version">v2.17.5</span>)</li>
|
||||
<li><a href="example-widget-filter-custom-search.html">custom searches</a> (<span class="version">v2.17.5</span>; <span class="version updated">v2.17.8</span>)</li>
|
||||
<li>formatter: <a href="example-widget-filter-formatter-1.html">jQuery UI widgets</a> and <a href="example-widget-filter-formatter-2.html">HTML5 Elements</a> (v2.7.7; <span class="version updated">v2.17.5</span>).</li>
|
||||
<li>formatter: <a href="example-widget-filter-formatter-select2.html">select2</a> (<span class="version">v2.16.0</span>; <span class="version updated">v2.17.5</span>)</li>
|
||||
</ul>
|
||||
@ -1623,6 +1623,8 @@ $(function(){
|
||||
filter_cssFilter : '',
|
||||
// data attribute in the header cell that contains the default filter value
|
||||
filter_defaultAttrib : 'data-value',
|
||||
// add a default column filter type "~{query}" to make fuzzy searches default; "{q1} AND {q2}" to make all searches use a logical AND.
|
||||
filter_defaultFilter : [],
|
||||
// jQuery selector string (or jQuery object) of external search inputs
|
||||
filter_external : '',
|
||||
// class added to filtered rows; needed by pager plugin
|
||||
@ -2043,6 +2045,55 @@ $(function(){
|
||||
<td><a href="example-widget-filter.html">Example</a></td>
|
||||
</tr>
|
||||
|
||||
<tr id="widget-defaultfilter">
|
||||
<td><a href="#" class="permalink">filter_defaultFilter</a></td>
|
||||
<td>Object</td>
|
||||
<td>{ }</td>
|
||||
<td>
|
||||
Filter widget: Add a default filter type to a column (<span class="version">v2.17.8</span>).
|
||||
<div class="collapsible">
|
||||
<br>
|
||||
<span class="label warning">Warning</span> If a column has a default filter set, the user <strong>will not</strong> be able to use other filters.<br>
|
||||
<br>
|
||||
Use the <a href="#widget-defaultfilter"><code>filter_defaultFilter</code></a> option as follows:
|
||||
<pre class="prettyprint lang-js">$(function(){
|
||||
$("table").tablesorter({
|
||||
widgets: ["filter"],
|
||||
widgetOptions : {
|
||||
|
||||
filter_defaultFilter : {
|
||||
// target a column by class name or column index (zero-based)
|
||||
|
||||
'.fuzzy' : '~{q}',
|
||||
2 : '{q}=',
|
||||
|
||||
// for default "anyMatches" use the column length as the index
|
||||
// e.g. if your table has 5 columns (0,1,2,3,4), then set the anyMatch column as 5.
|
||||
5 : '{q} or {q}' // any match column
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
});</pre>
|
||||
Set up the column string as follows:
|
||||
<ul>
|
||||
<li>Add the desired filter type symbol along with <code>{query}</code> or <code>{q}</code> to maintain positioning</li>
|
||||
<li>Symbols can be added to the beginning <code>"~{query}"</code> (default fuzzy search) or end <code>"{q}="</code> (default exact search)</li>
|
||||
<li>For symbols that separate queries, like "AND", "OR" or "-" (range):
|
||||
<ul>
|
||||
<li>Add one additional <code>{q}</code> tag.</li>
|
||||
<li>For example, to add a default "OR" search, use <code>"{q} OR {q}"</code>.</li>
|
||||
<li>If the user enters <code>"a b c d"</code> the column will be filtered using <code>"a OR b OR c OR d"</code>, so there is no need to add more <code>{q}</code> tags within the string; adding more will likely mess up the results.</li>
|
||||
<li>If the user only enters <code>"a"</code>, then the column will be filtered using <code>"a OR a"</code> so as not to cause other issues.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><span class="label label-info">Note</span> It is not possible to set up a default filter search within a query. So the "wild" filter search will not work as intended (e.g. <code>{q}*</code> and <code>{q}?</code> are essentially the same as <code>{q}</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
</td>
|
||||
<td><a href="example-widget-filter-any-match.html">Example</a></td>
|
||||
</tr>
|
||||
|
||||
<tr id="widget-filter-external">
|
||||
<td><a href="#" class="permalink">filter_external</a></td>
|
||||
<td>String</td>
|
||||
|
@ -245,7 +245,7 @@ ts.addWidget({
|
||||
ts.benchmark("Applying " + theme + " theme", time);
|
||||
}
|
||||
},
|
||||
remove: function(table, c, wo) {
|
||||
remove: function(table, c) {
|
||||
var $table = c.$table,
|
||||
theme = c.theme || 'jui',
|
||||
themes = ts.themes[ theme ] || ts.themes.jui,
|
||||
@ -354,6 +354,7 @@ ts.addWidget({
|
||||
filter_childRows : false, // if true, filter includes child row content in the search
|
||||
filter_columnFilters : true, // if true, a filter will be added to the top of each table column
|
||||
filter_cssFilter : '', // css class name added to the filter row & each input in the row (tablesorter-filter is ALWAYS added)
|
||||
filter_defaultFilter : [], // add a default column filter type "~{query}" to make fuzzy searches default; "{q1} AND {q2}" to make all searches use a logical AND.
|
||||
filter_external : '', // jQuery selector string (or jQuery object) of external filters
|
||||
filter_filteredRow : 'filtered', // class added to filtered rows; needed by pager plugin
|
||||
filter_formatter : null, // add custom filter elements to the filter row
|
||||
@ -410,24 +411,25 @@ ts.filter = {
|
||||
type : /undefined|number/, // check type
|
||||
exact : /(^[\"|\'|=]+)|([\"|\'|=]+$)/g, // exact match (allow '==')
|
||||
nondigit : /[^\w,. \-()]/g, // replace non-digits (from digit & currency parser)
|
||||
operators : /[<>=]/g // replace operators
|
||||
operators : /[<>=]/g, // replace operators
|
||||
query : '(q|query)' // replace filter queries
|
||||
},
|
||||
// function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed )
|
||||
// filter = array of filter input values; iFilter = same array, except lowercase
|
||||
// exact = table cell text (or parsed data if column parser enabled)
|
||||
// iExact = same as exact, except lowercase
|
||||
// cached = table cell text from cache, so it has been parsed
|
||||
// index = column index; table = table element (DOM)
|
||||
// wo = widget options (table.config.widgetOptions)
|
||||
// parsed = array (by column) of boolean values (from filter_useParsedData or "filter-parsed" class)
|
||||
// function( c, data ) { }
|
||||
// c = table.config
|
||||
// data.filter = array of filter input values; data.iFilter = same array, except lowercase
|
||||
// data.exact = table cell text (or parsed data if column parser enabled)
|
||||
// data.iExact = same as data.exact, except lowercase
|
||||
// data.cache = table cell text from cache, so it has been parsed
|
||||
// data.index = column index; table = table element (DOM)
|
||||
// data.parsed = array (by column) of boolean values (from filter_useParsedData or "filter-parsed" class)
|
||||
types: {
|
||||
// Look for regex
|
||||
regex: function( filter, iFilter, exact, iExact ) {
|
||||
if ( ts.filter.regex.regex.test(iFilter) ) {
|
||||
regex: function( c, data ) {
|
||||
if ( ts.filter.regex.regex.test(data.iFilter) ) {
|
||||
var matches,
|
||||
regex = ts.filter.regex.regex.exec(iFilter);
|
||||
regex = ts.filter.regex.regex.exec(data.iFilter);
|
||||
try {
|
||||
matches = new RegExp(regex[1], regex[2]).test( iExact );
|
||||
matches = new RegExp(regex[1], regex[2]).test( data.iExact );
|
||||
} catch (error) {
|
||||
matches = false;
|
||||
}
|
||||
@ -436,27 +438,29 @@ ts.filter = {
|
||||
return null;
|
||||
},
|
||||
// Look for operators >, >=, < or <=
|
||||
operators: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) {
|
||||
if ( /^[<>]=?/.test(iFilter) ) {
|
||||
operators: function( c, data ) {
|
||||
if ( /^[<>]=?/.test(data.iFilter) ) {
|
||||
var cachedValue, result,
|
||||
c = table.config,
|
||||
query = ts.formatFloat( iFilter.replace(ts.filter.regex.operators, ''), table ),
|
||||
table = c.table,
|
||||
index = data.index,
|
||||
parsed = data.parsed[index],
|
||||
query = ts.formatFloat( data.iFilter.replace(ts.filter.regex.operators, ''), table ),
|
||||
parser = c.parsers[index],
|
||||
savedSearch = query;
|
||||
// parse filter value in case we're comparing numbers (dates)
|
||||
if (parsed[index] || parser.type === 'numeric') {
|
||||
result = ts.filter.parseFilter(table, $.trim('' + iFilter.replace(ts.filter.regex.operators, '')), index, parsed[index], true);
|
||||
if (parsed || parser.type === 'numeric') {
|
||||
result = ts.filter.parseFilter(c, $.trim('' + data.iFilter.replace(ts.filter.regex.operators, '')), index, parsed, true);
|
||||
query = ( typeof result === "number" && result !== '' && !isNaN(result) ) ? result : query;
|
||||
}
|
||||
|
||||
// iExact may be numeric - see issue #149;
|
||||
// check if cached is defined, because sometimes j goes out of range? (numeric columns)
|
||||
cachedValue = ( parsed[index] || parser.type === 'numeric' ) && !isNaN(query) && typeof cached !== 'undefined' ? cached :
|
||||
isNaN(iExact) ? ts.formatFloat( iExact.replace(ts.filter.regex.nondigit, ''), table) :
|
||||
ts.formatFloat( iExact, table );
|
||||
cachedValue = ( parsed || parser.type === 'numeric' ) && !isNaN(query) && typeof data.cache !== 'undefined' ? data.cache :
|
||||
isNaN(data.iExact) ? ts.formatFloat( data.iExact.replace(ts.filter.regex.nondigit, ''), table) :
|
||||
ts.formatFloat( data.iExact, table );
|
||||
|
||||
if ( />/.test(iFilter) ) { result = />=/.test(iFilter) ? cachedValue >= query : cachedValue > query; }
|
||||
if ( /</.test(iFilter) ) { result = /<=/.test(iFilter) ? cachedValue <= query : cachedValue < query; }
|
||||
if ( />/.test(data.iFilter) ) { result = />=/.test(data.iFilter) ? cachedValue >= query : cachedValue > query; }
|
||||
if ( /</.test(data.iFilter) ) { result = /<=/.test(data.iFilter) ? cachedValue <= query : cachedValue < query; }
|
||||
// keep showing all rows if nothing follows the operator
|
||||
if ( !result && savedSearch === '' ) { result = true; }
|
||||
return result;
|
||||
@ -464,37 +468,40 @@ ts.filter = {
|
||||
return null;
|
||||
},
|
||||
// Look for a not match
|
||||
notMatch: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) {
|
||||
if ( /^\!/.test(iFilter) ) {
|
||||
iFilter = ts.filter.parseFilter(table, iFilter.replace('!', ''), index, parsed[index]);
|
||||
if (ts.filter.regex.exact.test(iFilter)) {
|
||||
notMatch: function( c, data ) {
|
||||
if ( /^\!/.test(data.iFilter) ) {
|
||||
var indx,
|
||||
filter = ts.filter.parseFilter(c, data.iFilter.replace('!', ''), data.index, data.parsed[data.index]);
|
||||
if (ts.filter.regex.exact.test(filter)) {
|
||||
// look for exact not matches - see #628
|
||||
iFilter = iFilter.replace(ts.filter.regex.exact, '');
|
||||
return iFilter === '' ? true : $.trim(iFilter) !== iExact;
|
||||
filter = filter.replace(ts.filter.regex.exact, '');
|
||||
return filter === '' ? true : $.trim(filter) !== data.iExact;
|
||||
} else {
|
||||
var indx = iExact.search( $.trim(iFilter) );
|
||||
return iFilter === '' ? true : !(wo.filter_startsWith ? indx === 0 : indx >= 0);
|
||||
indx = data.iExact.search( $.trim(filter) );
|
||||
return filter === '' ? true : !(c.widgetOptions.filter_startsWith ? indx === 0 : indx >= 0);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
// Look for quotes or equals to get an exact match; ignore type since iExact could be numeric
|
||||
exact: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed, rowArray ) {
|
||||
exact: function( c, data ) {
|
||||
/*jshint eqeqeq:false */
|
||||
if (ts.filter.regex.exact.test(iFilter)) {
|
||||
var fltr = ts.filter.parseFilter(table, iFilter.replace(ts.filter.regex.exact, ''), index, parsed[index]);
|
||||
return rowArray ? $.inArray(fltr, rowArray) >= 0 : fltr == iExact;
|
||||
if (ts.filter.regex.exact.test(data.iFilter)) {
|
||||
var filter = ts.filter.parseFilter(c, data.iFilter.replace(ts.filter.regex.exact, ''), data.index, data.parsed[data.index]);
|
||||
return data.anyMatch ? $.inArray(filter, data.rowArray) >= 0 : filter == data.iExact;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
// Look for an AND or && operator (logical and)
|
||||
and : function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) {
|
||||
if ( ts.filter.regex.andTest.test(filter) ) {
|
||||
var query = iFilter.split( ts.filter.regex.andSplit ),
|
||||
result = iExact.search( $.trim(ts.filter.parseFilter(table, query[0], index, parsed[index])) ) >= 0,
|
||||
and : function( c, data ) {
|
||||
if ( ts.filter.regex.andTest.test(data.filter) ) {
|
||||
var index = data.index,
|
||||
parsed = data.parsed[index],
|
||||
query = data.iFilter.split( ts.filter.regex.andSplit ),
|
||||
result = data.iExact.search( $.trim( ts.filter.parseFilter(c, query[0], index, parsed) ) ) >= 0,
|
||||
indx = query.length - 1;
|
||||
while (result && indx) {
|
||||
result = result && iExact.search( $.trim(ts.filter.parseFilter(table, query[indx], index, parsed[index])) ) >= 0;
|
||||
result = result && data.iExact.search( $.trim( ts.filter.parseFilter(c, query[indx], index, parsed) ) ) >= 0;
|
||||
indx--;
|
||||
}
|
||||
return result;
|
||||
@ -502,52 +509,55 @@ ts.filter = {
|
||||
return null;
|
||||
},
|
||||
// Look for a range (using " to " or " - ") - see issue #166; thanks matzhu!
|
||||
range : function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) {
|
||||
if ( ts.filter.regex.toTest.test(iFilter) ) {
|
||||
range : function( c, data ) {
|
||||
if ( ts.filter.regex.toTest.test(data.iFilter) ) {
|
||||
var result, tmp,
|
||||
c = table.config,
|
||||
table = c.table,
|
||||
index = data.index,
|
||||
parsed = data.parsed[index],
|
||||
// make sure the dash is for a range and not indicating a negative number
|
||||
query = iFilter.split( ts.filter.regex.toSplit ),
|
||||
range1 = ts.formatFloat( ts.filter.parseFilter(table, query[0].replace(ts.filter.regex.nondigit, ''), index, parsed[index]), table ),
|
||||
range2 = ts.formatFloat( ts.filter.parseFilter(table, query[1].replace(ts.filter.regex.nondigit, ''), index, parsed[index]), table );
|
||||
query = data.iFilter.split( ts.filter.regex.toSplit ),
|
||||
range1 = ts.formatFloat( ts.filter.parseFilter(c, query[0].replace(ts.filter.regex.nondigit, ''), index, parsed), table ),
|
||||
range2 = ts.formatFloat( ts.filter.parseFilter(c, query[1].replace(ts.filter.regex.nondigit, ''), index, parsed), table );
|
||||
// parse filter value in case we're comparing numbers (dates)
|
||||
if (parsed[index] || c.parsers[index].type === 'numeric') {
|
||||
if (parsed || c.parsers[index].type === 'numeric') {
|
||||
result = c.parsers[index].format('' + query[0], table, c.$headers.eq(index), index);
|
||||
range1 = (result !== '' && !isNaN(result)) ? result : range1;
|
||||
result = c.parsers[index].format('' + query[1], table, c.$headers.eq(index), index);
|
||||
range2 = (result !== '' && !isNaN(result)) ? result : range2;
|
||||
}
|
||||
result = ( parsed[index] || c.parsers[index].type === 'numeric' ) && !isNaN(range1) && !isNaN(range2) ? cached :
|
||||
isNaN(iExact) ? ts.formatFloat( iExact.replace(ts.filter.regex.nondigit, ''), table) :
|
||||
ts.formatFloat( iExact, table );
|
||||
result = ( parsed || c.parsers[index].type === 'numeric' ) && !isNaN(range1) && !isNaN(range2) ? data.cache :
|
||||
isNaN(data.iExact) ? ts.formatFloat( data.iExact.replace(ts.filter.regex.nondigit, ''), table) :
|
||||
ts.formatFloat( data.iExact, table );
|
||||
if (range1 > range2) { tmp = range1; range1 = range2; range2 = tmp; } // swap
|
||||
return (result >= range1 && result <= range2) || (range1 === '' || range2 === '');
|
||||
}
|
||||
return null;
|
||||
},
|
||||
// Look for wild card: ? = single, * = multiple, or | = logical OR
|
||||
wild : function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed, rowArray ) {
|
||||
if ( /[\?|\*]/.test(iFilter) || ts.filter.regex.orReplace.test(filter) ) {
|
||||
var c = table.config,
|
||||
query = ts.filter.parseFilter(table, iFilter.replace(ts.filter.regex.orReplace, "|"), index, parsed[index]);
|
||||
wild : function( c, data ) {
|
||||
if ( /[\?|\*]/.test(data.iFilter) || ts.filter.regex.orReplace.test(data.filter) ) {
|
||||
var index = data.index,
|
||||
parsed = data.parsed[index],
|
||||
query = ts.filter.parseFilter(c, data.iFilter.replace(ts.filter.regex.orReplace, "|"), index, parsed);
|
||||
// look for an exact match with the "or" unless the "filter-match" class is found
|
||||
if (!c.$headers.filter('[data-column="' + index + '"]:last').hasClass('filter-match') && /\|/.test(query)) {
|
||||
query = $.isArray(rowArray) ? '(' + query + ')' : '^(' + query + ')$';
|
||||
query = data.anyMatch && $.isArray(data.rowArray) ? '(' + query + ')' : '^(' + query + ')$';
|
||||
}
|
||||
// parsing the filter may not work properly when using wildcards =/
|
||||
return new RegExp( query.replace(/\?/g, '\\S{1}').replace(/\*/g, '\\S*') ).test(iExact);
|
||||
return new RegExp( query.replace(/\?/g, '\\S{1}').replace(/\*/g, '\\S*') ).test(data.iExact);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
// fuzzy text search; modified from https://github.com/mattyork/fuzzy (MIT license)
|
||||
fuzzy: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) {
|
||||
if ( /^~/.test(iFilter) ) {
|
||||
fuzzy: function( c, data ) {
|
||||
if ( /^~/.test(data.iFilter) ) {
|
||||
var indx,
|
||||
patternIndx = 0,
|
||||
len = iExact.length,
|
||||
pattern = ts.filter.parseFilter(table, iFilter.slice(1), index, parsed[index]);
|
||||
len = data.iExact.length,
|
||||
pattern = ts.filter.parseFilter(c, data.iFilter.slice(1), data.index, data.parsed[data.index]);
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (iExact[indx] === pattern[patternIndx]) {
|
||||
if (data.iExact[indx] === pattern[patternIndx]) {
|
||||
patternIndx += 1;
|
||||
}
|
||||
}
|
||||
@ -580,6 +590,7 @@ ts.filter = {
|
||||
wo.filter_formatterCount = 0;
|
||||
wo.filter_formatterInit = [];
|
||||
|
||||
txt = '\\{' + ts.filter.regex.query + '\\}';
|
||||
$.extend( regex, {
|
||||
child : new RegExp(c.cssChildRow),
|
||||
filtered : new RegExp(wo.filter_filteredRow),
|
||||
@ -588,7 +599,9 @@ ts.filter = {
|
||||
toSplit : new RegExp('(?:\\s+(?:-|' + ts.language.to + ')\\s+)' ,'gi'),
|
||||
andTest : new RegExp('\\s+(' + ts.language.and + '|&&)\\s+', 'i'),
|
||||
andSplit : new RegExp('(?:\\s+(?:' + ts.language.and + '|&&)\\s+)', 'gi'),
|
||||
orReplace : new RegExp('\\s+(' + ts.language.or + ')\\s+', 'gi')
|
||||
orReplace : new RegExp('\\s+(' + ts.language.or + ')\\s+', 'gi'),
|
||||
iQuery : new RegExp(txt, 'i'),
|
||||
igQuery : new RegExp(txt, 'ig')
|
||||
});
|
||||
|
||||
// don't build filter row if columnFilters is false or all columns are set to "filter-false" - issue #156
|
||||
@ -780,10 +793,9 @@ ts.filter = {
|
||||
c.$table.data('lastSearch', filters);
|
||||
return filters;
|
||||
},
|
||||
parseFilter: function(table, filter, column, parsed, forceParse){
|
||||
var c = table.config;
|
||||
parseFilter: function(c, filter, column, parsed, forceParse){
|
||||
return forceParse || parsed ?
|
||||
c.parsers[column].format( filter, table, [], column ) :
|
||||
c.parsers[column].format( filter, c.table, [], column ) :
|
||||
filter;
|
||||
},
|
||||
buildRow: function(table, c, wo) {
|
||||
@ -991,26 +1003,51 @@ ts.filter = {
|
||||
}, 200);
|
||||
});
|
||||
},
|
||||
defaultFilter: function(filter, mask){
|
||||
if (filter === '') { return filter; }
|
||||
var regex = ts.filter.regex.iQuery,
|
||||
maskRegex = ts.filter.regex.igQuery,
|
||||
query = $.trim(filter).split(/\s/),
|
||||
len = query.length - 1,
|
||||
indx = 0,
|
||||
val = mask;
|
||||
if ( len < 1 && mask.match( maskRegex ).length > 1 ) {
|
||||
// only one "word" in query but mask has >1 slots
|
||||
query[1] = query[0];
|
||||
}
|
||||
// replace all {query} with query words...
|
||||
// if query = "Bob", then convert mask from "!{query}" to "!Bob"
|
||||
// if query = "Bob Joe Frank", then convert mask "{q} OR {q}" to "Bob OR Joe OR Frank"
|
||||
while (regex.test(val)) {
|
||||
val = val.replace(regex, query[indx++] || '');
|
||||
if (regex.test(val) && indx < len && (query[indx] || '') !== '') {
|
||||
val = mask.replace(regex, val);
|
||||
}
|
||||
}
|
||||
return val;
|
||||
},
|
||||
findRows: function(table, filters, combinedFilters) {
|
||||
if (table.config.lastCombinedFilter === combinedFilters) { return; }
|
||||
var cached, len, $rows, rowIndex, tbodyIndex, $tbody, $cells, columnIndex,
|
||||
childRow, childRowText, exact, iExact, iFilter, lastSearch, matches, result,
|
||||
notFiltered, searchFiltered, filterMatched, showRow, time, val, indx,
|
||||
anyMatch, iAnyMatch, rowArray, rowText, iRowText, rowCache, fxn, ffxn,
|
||||
var len, $rows, rowIndex, tbodyIndex, $tbody, $cells, columnIndex,
|
||||
childRow, lastSearch, matches, result, showRow, time, val, indx,
|
||||
notFiltered, searchFiltered, filterMatched, fxn, ffxn,
|
||||
regex = ts.filter.regex,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
columns = c.columns,
|
||||
$tbodies = c.$table.children('tbody'), // target all tbodies #568
|
||||
// data object passed to filters; anyMatch is a flag for the filters
|
||||
data = { anyMatch: false },
|
||||
// anyMatch really screws up with these types of filters
|
||||
anyMatchNotAllowedTypes = [ 'range', 'notMatch', 'operators' ],
|
||||
// parse columns after formatter, in case the class is added at that point
|
||||
parsed = c.$headers.map(function(columnIndex) {
|
||||
return c.parsers && c.parsers[columnIndex] && c.parsers[columnIndex].parsed ||
|
||||
// getData won't return "parsed" if other "filter-" class names exist (e.g. <th class="filter-select filter-parsed">)
|
||||
ts.getData && ts.getData(c.$headers.filter('[data-column="' + columnIndex + '"]:last'), ts.getColumnData( table, c.headers, columnIndex ), 'filter') === 'parsed' ||
|
||||
$(this).hasClass('filter-parsed');
|
||||
}).get();
|
||||
noAnyMatch = [ 'range', 'notMatch', 'operators' ];
|
||||
|
||||
// parse columns after formatter, in case the class is added at that point
|
||||
data.parsed = c.$headers.map(function(columnIndex) {
|
||||
return c.parsers && c.parsers[columnIndex] && c.parsers[columnIndex].parsed ||
|
||||
// getData won't return "parsed" if other "filter-" class names exist (e.g. <th class="filter-select filter-parsed">)
|
||||
ts.getData && ts.getData(c.$headers.filter('[data-column="' + columnIndex + '"]:last'), ts.getColumnData( table, c.headers, columnIndex ), 'filter') === 'parsed' ||
|
||||
$(this).hasClass('filter-parsed');
|
||||
}).get();
|
||||
|
||||
if (c.debug) { time = new Date(); }
|
||||
// filtered rows count
|
||||
c.filteredRows = 0;
|
||||
@ -1060,15 +1097,25 @@ ts.filter = {
|
||||
ts.log( "Searching through " + ( searchFiltered && notFiltered < len ? notFiltered : "all" ) + " rows" );
|
||||
}
|
||||
if ((wo.filter_$anyMatch && wo.filter_$anyMatch.length) || filters[c.columns]) {
|
||||
anyMatch = wo.filter_$anyMatch && wo.filter_$anyMatch.val() || filters[c.columns] || '';
|
||||
data.anyMatchFlag = true;
|
||||
data.anyMatchFilter = wo.filter_$anyMatch && wo.filter_$anyMatch.val() || filters[c.columns] || '';
|
||||
if (c.sortLocaleCompare) {
|
||||
// replace accents
|
||||
anyMatch = ts.replaceAccents(anyMatch);
|
||||
data.anyMatchFilter = ts.replaceAccents(data.anyMatchFilter);
|
||||
}
|
||||
iAnyMatch = anyMatch.toLowerCase();
|
||||
if (wo.filter_defaultFilter && regex.iQuery.test( ts.getColumnData( table, wo.filter_defaultFilter, c.columns, true ) || '')) {
|
||||
data.anyMatchFilter = ts.filter.defaultFilter(data.anyMatchFilter, wo.filter_defaultFilter[c.columns]);
|
||||
// clear search filtered flag because default filters are not saved to the last search
|
||||
searchFiltered = false;
|
||||
}
|
||||
data.iAnyMatchFilter = data.anyMatchFilter;
|
||||
}
|
||||
|
||||
// loop through the rows
|
||||
for (rowIndex = 0; rowIndex < len; rowIndex++) {
|
||||
|
||||
data.cacheArray = c.cache[tbodyIndex].normalized[rowIndex];
|
||||
|
||||
childRow = $rows[rowIndex].className;
|
||||
// skip child rows & already filtered rows
|
||||
if ( regex.child.test(childRow) || (searchFiltered && regex.filtered.test(childRow)) ) { continue; }
|
||||
@ -1078,15 +1125,16 @@ ts.filter = {
|
||||
// so, if "table.config.widgetOptions.filter_childRows" is true and there is
|
||||
// a match anywhere in the child row, then it will make the row visible
|
||||
// checked here so the option can be changed dynamically
|
||||
childRowText = (childRow.length && wo.filter_childRows) ? childRow.text() : '';
|
||||
childRowText = wo.filter_ignoreCase ? childRowText.toLocaleLowerCase() : childRowText;
|
||||
data.childRowText = (childRow.length && wo.filter_childRows) ? childRow.text() : '';
|
||||
data.childRowText = wo.filter_ignoreCase ? data.childRowText.toLocaleLowerCase() : data.childRowText;
|
||||
$cells = $rows.eq(rowIndex).children();
|
||||
|
||||
if (anyMatch) {
|
||||
rowArray = $cells.map(function(i){
|
||||
if (data.anyMatchFlag) {
|
||||
data.anyMatch = true;
|
||||
data.rowArray = $cells.map(function(i){
|
||||
var txt;
|
||||
if (parsed[i]) {
|
||||
txt = c.cache[tbodyIndex].normalized[rowIndex][i];
|
||||
if (data.parsed[i]) {
|
||||
txt = data.cacheArray[i];
|
||||
} else {
|
||||
txt = wo.filter_ignoreCase ? $(this).text().toLowerCase() : $(this).text();
|
||||
if (c.sortLocaleCompare) {
|
||||
@ -1095,13 +1143,15 @@ ts.filter = {
|
||||
}
|
||||
return txt;
|
||||
}).get();
|
||||
rowText = rowArray.join(' ');
|
||||
iRowText = rowText.toLowerCase();
|
||||
rowCache = c.cache[tbodyIndex].normalized[rowIndex].slice(0,-1).join(' ');
|
||||
data.filter = data.anyMatchFilter;
|
||||
data.iFilter = data.iAnyMatchFilter;
|
||||
data.exact = data.rowArray.join(' ');
|
||||
data.iExact = data.exact.toLowerCase();
|
||||
data.cache = data.cacheArray.slice(0,-1).join(' ');
|
||||
filterMatched = null;
|
||||
$.each(ts.filter.types, function(type, typeFunction) {
|
||||
if ($.inArray(type, anyMatchNotAllowedTypes) < 0) {
|
||||
matches = typeFunction( anyMatch, iAnyMatch, rowText, iRowText, rowCache, columns, table, wo, parsed, rowArray );
|
||||
if ($.inArray(type, noAnyMatch) < 0) {
|
||||
matches = typeFunction( c, data );
|
||||
if (matches !== null) {
|
||||
filterMatched = matches;
|
||||
return false;
|
||||
@ -1113,30 +1163,33 @@ ts.filter = {
|
||||
} else {
|
||||
if (wo.filter_startsWith) {
|
||||
showRow = false;
|
||||
columnIndex = columns;
|
||||
columnIndex = c.columns;
|
||||
while (!showRow && columnIndex > 0) {
|
||||
columnIndex--;
|
||||
showRow = showRow || rowArray[columnIndex].indexOf(iAnyMatch) === 0;
|
||||
showRow = showRow || data.rowArray[columnIndex].indexOf(data.iFilter) === 0;
|
||||
}
|
||||
} else {
|
||||
showRow = (iRowText + childRowText).indexOf(iAnyMatch) >= 0;
|
||||
showRow = (data.iExact + data.childRowText).indexOf(data.iFilter) >= 0;
|
||||
}
|
||||
}
|
||||
data.anyMatch = false;
|
||||
}
|
||||
|
||||
for (columnIndex = 0; columnIndex < columns; columnIndex++) {
|
||||
for (columnIndex = 0; columnIndex < c.columns; columnIndex++) {
|
||||
data.filter = filters[columnIndex];
|
||||
data.index = columnIndex;
|
||||
// ignore if filter is empty or disabled
|
||||
if (filters[columnIndex]) {
|
||||
cached = c.cache[tbodyIndex].normalized[rowIndex][columnIndex];
|
||||
if (data.filter) {
|
||||
data.cache = data.cacheArray[columnIndex];
|
||||
// check if column data should be from the cell or from parsed data
|
||||
if (wo.filter_useParsedData || parsed[columnIndex]) {
|
||||
exact = cached;
|
||||
if (wo.filter_useParsedData || data.parsed[columnIndex]) {
|
||||
data.exact = data.cache;
|
||||
} else {
|
||||
// using older or original tablesorter
|
||||
exact = $.trim($cells.eq(columnIndex).text());
|
||||
exact = c.sortLocaleCompare ? ts.replaceAccents(exact) : exact; // issue #405
|
||||
data.exact = $.trim( $cells.eq(columnIndex).text() );
|
||||
data.exact = c.sortLocaleCompare ? ts.replaceAccents(data.exact) : data.exact; // issue #405
|
||||
}
|
||||
iExact = !regex.type.test(typeof exact) && wo.filter_ignoreCase ? exact.toLocaleLowerCase() : exact;
|
||||
data.iExact = !regex.type.test(typeof data.exact) && wo.filter_ignoreCase ? data.exact.toLocaleLowerCase() : data.exact;
|
||||
result = showRow; // if showRow is true, show that row
|
||||
|
||||
// in case select filter option has a different value vs text "a - z|A through Z"
|
||||
@ -1144,28 +1197,32 @@ ts.filter = {
|
||||
c.$filters.add(c.$externalFilters).filter('[data-column="'+ columnIndex + '"]').find('select option:selected').attr('data-function-name') || '' : '';
|
||||
|
||||
// replace accents - see #357
|
||||
filters[columnIndex] = c.sortLocaleCompare ? ts.replaceAccents(filters[columnIndex]) : filters[columnIndex];
|
||||
// val = case insensitive, filters[columnIndex] = case sensitive
|
||||
iFilter = wo.filter_ignoreCase ? (filters[columnIndex] || '').toLocaleLowerCase() : filters[columnIndex];
|
||||
data.filter = c.sortLocaleCompare ? ts.replaceAccents(data.filter) : data.filter;
|
||||
|
||||
if (wo.filter_defaultFilter && regex.iQuery.test( ts.getColumnData( table, wo.filter_defaultFilter, columnIndex ) || '')) {
|
||||
data.filter = ts.filter.defaultFilter(data.filter, wo.filter_defaultFilter[columnIndex]);
|
||||
}
|
||||
// val = case insensitive, columnFilter = case sensitive
|
||||
data.iFilter = wo.filter_ignoreCase ? (data.filter || '').toLocaleLowerCase() : data.filter;
|
||||
fxn = ts.getColumnData( table, wo.filter_functions, columnIndex );
|
||||
if (fxn) {
|
||||
if (fxn === true) {
|
||||
// default selector; no "filter-select" class
|
||||
result = (c.$headers.filter('[data-column="' + columnIndex + '"]:last').hasClass('filter-match')) ?
|
||||
iExact.search(iFilter) >= 0 : filters[columnIndex] === exact;
|
||||
data.iExact.search(data.iFilter) >= 0 : data.filter === data.exact;
|
||||
} else if (typeof fxn === 'function') {
|
||||
// filter callback( exact cell content, parser normalized content, filter input value, column index, jQuery row object )
|
||||
result = fxn(exact, cached, filters[columnIndex], columnIndex, $rows.eq(rowIndex));
|
||||
} else if (typeof fxn[ffxn || filters[columnIndex]] === 'function') {
|
||||
result = fxn(data.exact, data.cache, data.filter, columnIndex, $rows.eq(rowIndex));
|
||||
} else if (typeof fxn[ffxn || data.filter] === 'function') {
|
||||
// selector option function
|
||||
result = fxn[ffxn || filters[columnIndex]](exact, cached, filters[columnIndex], columnIndex, $rows.eq(rowIndex));
|
||||
result = fxn[ffxn || data.filter](data.exact, data.cache, data.filter, columnIndex, $rows.eq(rowIndex));
|
||||
}
|
||||
} else {
|
||||
filterMatched = null;
|
||||
// cycle through the different filters
|
||||
// filters return a boolean or null if nothing matches
|
||||
$.each(ts.filter.types, function(type, typeFunction) {
|
||||
matches = typeFunction( filters[columnIndex], iFilter, exact, iExact, cached, columnIndex, table, wo, parsed );
|
||||
matches = typeFunction( c, data );
|
||||
if (matches !== null) {
|
||||
filterMatched = matches;
|
||||
return false;
|
||||
@ -1175,8 +1232,8 @@ ts.filter = {
|
||||
result = filterMatched;
|
||||
// Look for match, and add child row data for matching
|
||||
} else {
|
||||
exact = (iExact + childRowText).indexOf( ts.filter.parseFilter(table, iFilter, columnIndex, parsed[columnIndex]) );
|
||||
result = ( (!wo.filter_startsWith && exact >= 0) || (wo.filter_startsWith && exact === 0) );
|
||||
data.exact = (data.iExact + data.childRowText).indexOf( ts.filter.parseFilter(c, data.iFilter, columnIndex, data.parsed[columnIndex]) );
|
||||
result = ( (!wo.filter_startsWith && data.exact >= 0) || (wo.filter_startsWith && data.exact === 0) );
|
||||
}
|
||||
}
|
||||
showRow = (result) ? showRow : false;
|
||||
|
Loading…
Reference in New Issue
Block a user