Filter: add processOptions function & code cleanup. See #1010

This commit is contained in:
Mottie 2015-09-07 15:47:16 -05:00
parent fca2c9d27e
commit 3328f02579
12 changed files with 238 additions and 94 deletions

View File

@ -1,4 +1,4 @@
/*! tablesorter (FORK) - updated 09-05-2015 (v2.23.3)*/
/*! tablesorter (FORK) - updated 09-07-2015 (v2.23.3)*/
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
if (typeof define === 'function' && define.amd) {
@ -1600,7 +1600,7 @@
// *** sort functions ***
// regex used in natural sort
ts.regex.chunk = /(^([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunk = /(^([+\-]?(?:\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunks = /(^\\0|\\0$)/; // replace chunks @ ends
ts.regex.hex = /^0x[0-9a-f]+$/i; // hex
@ -3202,7 +3202,7 @@
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.append( options );
txt = wo.filter_selectSource;
fxn = $.isFunction( txt ) ? true : ts.getColumnData( table, txt, column );
fxn = typeof txt === 'function' ? true : ts.getColumnData( table, txt, column );
if ( fxn ) {
// updating so the extra options are appended
tsf.buildSelect( c.table, column, '', true, $header.hasClass( wo.filter_onlyAvail ) );
@ -3221,7 +3221,7 @@
}
if ( wo.filter_hideFilters ) {
tsf.hideFilters( table, c );
tsf.hideFilters( c );
}
// show processing icon
@ -3348,7 +3348,7 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
},
buildRow: function( table, c, wo ) {
var col, column, $header, buildSelect, disabled, name, ffxn, tmp,
var col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter,
columns = c.columns,
@ -3372,14 +3372,14 @@
// assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column );
buildSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
$header.hasClass( 'filter-select' );
// get data from jQuery data, metadata, headers option or header class name
col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( buildSelect ) {
if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
@ -3542,7 +3542,7 @@
return false;
}
},
hideFilters: function( table, c ) {
hideFilters: function( c ) {
var timer;
c.$table
.find( '.' + tscss.filterRow )
@ -4066,14 +4066,12 @@
},
getOptionSource: function( table, column, onlyAvail ) {
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
var c = table.config,
wo = c.widgetOptions,
parsed = [],
arry = false,
source = wo.filter_selectSource,
last = c.$table.data( 'lastSearch' ) || [],
fxn = $.isFunction( source ) ? true : ts.getColumnData( table, source, column );
fxn = typeof source === 'function' ? true : ts.getColumnData( table, source, column );
if ( onlyAvail && last[column] !== '' ) {
onlyAvail = false;
@ -4092,11 +4090,25 @@
// custom select source function for a SPECIFIC COLUMN
arry = fxn( table, column, onlyAvail );
}
if ( arry === false ) {
// fall back to original method
arry = tsf.getOptions( table, column, onlyAvail );
}
return tsf.processOptions( table, column, arry );
},
processOptions: function( table, column, arry ) {
if ( !$.isArray( arry ) ) {
return false;
}
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
validColumn = typeof column !== 'undefined' && column !== null && column >= 0 && column < c.columns,
parsed = [];
// get unique elements and sort the list
// if $.tablesorter.sortText exists ( not in the original tablesorter ),
// then natural sort the list otherwise use a basic sort
@ -4104,7 +4116,7 @@
return $.inArray( value, arry ) === indx;
});
if ( c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
if ( validColumn && c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
// unsorted select options
return arry;
} else {
@ -4117,7 +4129,8 @@
parsed.push({
t : txt,
// check parser length - fixes #934
p : c.parsers && c.parsers.length && c.parsers[ column ].format( txt, table, [], column ) || txt
p : validColumn && c.parsers && c.parsers.length &&
c.parsers[ column ].format( txt, table, [], column ) || txt
});
}
@ -4127,10 +4140,10 @@
// sortNatural breaks if you don't pass it strings
var x = a.p.toString(),
y = b.p.toString();
if ( $.isFunction( cts ) ) {
if ( validColumn && typeof cts === 'function' ) {
// custom OVERALL text sorter
return cts( x, y, true, column, table );
} else if ( typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
} else if ( validColumn && typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
// custom text sorter for a SPECIFIC COLUMN
return cts[column]( x, y, true, column, table );
} else if ( ts.sortNatural ) {
@ -4188,6 +4201,7 @@
if ( !table.config.cache || $.isEmptyObject( table.config.cache ) ) {
return;
}
var indx, val, txt, t, $filters, $filter,
c = table.config,
wo = c.widgetOptions,
@ -4203,6 +4217,7 @@
.find( 'thead' )
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.val();
// nothing included in arry ( external source ), so get the options from
// filter_selectSource or column data
if ( typeof arry === 'undefined' || arry === '' ) {

File diff suppressed because one or more lines are too long

View File

@ -1598,7 +1598,7 @@
// *** sort functions ***
// regex used in natural sort
ts.regex.chunk = /(^([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunk = /(^([+\-]?(?:\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunks = /(^\\0|\\0$)/; // replace chunks @ ends
ts.regex.hex = /^0x[0-9a-f]+$/i; // hex

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
/*! tablesorter (FORK) - updated 09-05-2015 (v2.23.3)*/
/*! tablesorter (FORK) - updated 09-07-2015 (v2.23.3)*/
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
if (typeof define === 'function' && define.amd) {
@ -858,7 +858,7 @@
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.append( options );
txt = wo.filter_selectSource;
fxn = $.isFunction( txt ) ? true : ts.getColumnData( table, txt, column );
fxn = typeof txt === 'function' ? true : ts.getColumnData( table, txt, column );
if ( fxn ) {
// updating so the extra options are appended
tsf.buildSelect( c.table, column, '', true, $header.hasClass( wo.filter_onlyAvail ) );
@ -877,7 +877,7 @@
}
if ( wo.filter_hideFilters ) {
tsf.hideFilters( table, c );
tsf.hideFilters( c );
}
// show processing icon
@ -1004,7 +1004,7 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
},
buildRow: function( table, c, wo ) {
var col, column, $header, buildSelect, disabled, name, ffxn, tmp,
var col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter,
columns = c.columns,
@ -1028,14 +1028,14 @@
// assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column );
buildSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
$header.hasClass( 'filter-select' );
// get data from jQuery data, metadata, headers option or header class name
col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( buildSelect ) {
if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
@ -1198,7 +1198,7 @@
return false;
}
},
hideFilters: function( table, c ) {
hideFilters: function( c ) {
var timer;
c.$table
.find( '.' + tscss.filterRow )
@ -1722,14 +1722,12 @@
},
getOptionSource: function( table, column, onlyAvail ) {
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
var c = table.config,
wo = c.widgetOptions,
parsed = [],
arry = false,
source = wo.filter_selectSource,
last = c.$table.data( 'lastSearch' ) || [],
fxn = $.isFunction( source ) ? true : ts.getColumnData( table, source, column );
fxn = typeof source === 'function' ? true : ts.getColumnData( table, source, column );
if ( onlyAvail && last[column] !== '' ) {
onlyAvail = false;
@ -1748,11 +1746,25 @@
// custom select source function for a SPECIFIC COLUMN
arry = fxn( table, column, onlyAvail );
}
if ( arry === false ) {
// fall back to original method
arry = tsf.getOptions( table, column, onlyAvail );
}
return tsf.processOptions( table, column, arry );
},
processOptions: function( table, column, arry ) {
if ( !$.isArray( arry ) ) {
return false;
}
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
validColumn = typeof column !== 'undefined' && column !== null && column >= 0 && column < c.columns,
parsed = [];
// get unique elements and sort the list
// if $.tablesorter.sortText exists ( not in the original tablesorter ),
// then natural sort the list otherwise use a basic sort
@ -1760,7 +1772,7 @@
return $.inArray( value, arry ) === indx;
});
if ( c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
if ( validColumn && c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
// unsorted select options
return arry;
} else {
@ -1773,7 +1785,8 @@
parsed.push({
t : txt,
// check parser length - fixes #934
p : c.parsers && c.parsers.length && c.parsers[ column ].format( txt, table, [], column ) || txt
p : validColumn && c.parsers && c.parsers.length &&
c.parsers[ column ].format( txt, table, [], column ) || txt
});
}
@ -1783,10 +1796,10 @@
// sortNatural breaks if you don't pass it strings
var x = a.p.toString(),
y = b.p.toString();
if ( $.isFunction( cts ) ) {
if ( validColumn && typeof cts === 'function' ) {
// custom OVERALL text sorter
return cts( x, y, true, column, table );
} else if ( typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
} else if ( validColumn && typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
// custom text sorter for a SPECIFIC COLUMN
return cts[column]( x, y, true, column, table );
} else if ( ts.sortNatural ) {
@ -1844,6 +1857,7 @@
if ( !table.config.cache || $.isEmptyObject( table.config.cache ) ) {
return;
}
var indx, val, txt, t, $filters, $filter,
c = table.config,
wo = c.widgetOptions,
@ -1859,6 +1873,7 @@
.find( 'thead' )
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.val();
// nothing included in arry ( external source ), so get the options from
// filter_selectSource or column data
if ( typeof arry === 'undefined' || arry === '' ) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -2965,7 +2965,7 @@ $('table').trigger('search', false);</pre></div>
<td>Function</td>
<td>null</td>
<td>
Filter widget: Include a function to return an array of values to be added to the column filter select (<span class="version">v2.16.0</span>; <span class="version updated">v2.21.5</span>).
Filter widget: Include a function to return an array of values to be added to the column filter select (<span class="version">v2.16.0</span>; <span class="version updated">v2.23.4</span>).
<div class="collapsible">
<br>
In <span class="version updated">v2.21.5</span>, this option will now override the <code>filter_function</code> options (<em>so you need to add them back!</em>), allowing the addition of custom select options and still maintain basic filtering action - see <a href="http://jsfiddle.net/Mottie/856bzzeL/117/">this demo</a> (<a href="http://stackoverflow.com/a/29506523/145346">ref</a>).<br>
@ -2996,17 +2996,29 @@ $('table').trigger('search', false);</pre></div>
});
});</pre>
</li>
<li>An object containing column keys set with a function - when the option is set in this manner, a function can be applied to a specific column
<li>An object containing column keys set with a function - when the option is set in this manner, a function can be applied to a specific column.
<p>This example was updated in <span class="version">v2.23.4</span> to use the <code>buildSelect</code> function directly (<a href="https://github.com/Mottie/tablesorter/issues/1010">ref</a>)</p>
<pre class="prettyprint lang-js">$(function(){
$("table").tablesorter({
widgets: ["filter"],
widgetOptions : {
filter_selectSource : {
0 : function(table, column, onlyAvail){
return $.getJSON('ajax/options.json', function(data) {
// return false if there is a problem and the select
// will display the original defaults
return data.hasOwnProperty('options') ? data.options : false;
// call ajax after tablesorter has initialized; this prevents
// multiple ajax calls during initialization
if (table.hasInitialized) {
$.getJSON('ajax/options.json', function(data) {
// return false if there is a problem and the select
// will display the original defaults
var result = data.hasOwnProperty('options') ? data.options : false;
// if not already done on the server-side & you want to sort & remove duplicates
// from the results, use the processOptions function (added 2.23.4)
result = $.tablesorter.filter.processOptions( table, column, result );
// call the buildSelect function; pass `true` to replace the contents of the select
$.tablesorter.filter.buildSelect( table, column, result, true, onlyAvail );
});
}
return false;
}
}
}
@ -7050,6 +7062,63 @@ $.tablesorter.filter.buildSelect( $('table'), 1, $('&lt;option&gt;Aaron&lt;/opti
</td>
</tr>
<tr id="function-getoptions">
<td><a href="#" class="permalink">getOptions</a></td>
<td>This filter widget function returns all the cached values of the set table column (<span class="version">v2.16.0</span>):
<div class="collapsible"><br>
This function respects the parsed data setting for the column, so it uses the <code>filter_useParsedData</code> option, <code>"filter-parsed"</code> class on the header, or the parser <code>parsed</code> flag settinng (<a href="example-parsers.html">ref</a>).
<p>Use it as follows:</p>
<pre class="prettyprint lang-js">$.tablesorter.filter.getOptions( table, column, onlyAvail );</pre>
<ul>
<li><code>table</code> - table DOM element (or jQuery object) of table.</li>
<li><code>column</code> - zero-based column index.</li>
<li><code>onlyAvail</code> - an optional boolean flag, which will be ignored if the <code>options</code> parameter is defined or is an empty string; otherwise it is the value passed to the function which finds all table column values; if <code>true</code>, only the available options will be diplayed.</li>
</ul>
Use this function as follows:<br>
<pre class="prettyprint lang-js">// external filter select
var index, len,
opts = '',
$table = $('table'),
column = 0, // first column
onlyAvail = false, // if true, available rows (visible rows after filtering) are returned
array = $.tablesorter.filter.getOptions( $table, column, onlyAvail );
// process array; sort & remove duplicates (added v2.23.4)
arry = $.tablesorter.filter.processOptions( $table, column, array );
len = arry.length;
// build options
for ( index = 0; index < len; index++ ) {
opts += '<option value="' + array[ index ] + '">' + array[ index ] + '</option>';
}
$('select.external').html( opts );</pre>
<p>Please be aware that this function does not process the array, so the array may contain duplicates and will not be sorted.</p>
The filter <a href="#function-processoptions"><code>processOptions</code></a> function was added in <span class="version">v2.23.4</span> which performs those actions.
</div>
</td>
</tr>
<tr id="function-processoptions">
<td><a href="#" class="permalink">processOptions</a></td>
<td>This filter widget function returns an array of sorted column data will duplicates removed (<span class="version">v2.23.4</span>).
<div class="collapsible"><br>
Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.filter.processOptions( table, column, array );</pre>
<ul>
<li><code>table</code> - table DOM element (or jQuery object) of table.</li>
<li><code>column</code> - zero-based column index (optional).</li>
<li><code>array</code> - An array of items to process.</li>
</ul>
The <code>column</code> parameter is optional, but if included it is used:
<ul>
<li>To check the column header for a <code>filter-select-nosort</code> class name to prevent the sorting of options.</li>
<li>To check tablesorter's <code>textSorter</code> option in case there are column specific sorting algorhithms set.</li>
</ul>
See the <a href="#function-getoptions"><code>getOptions</code></a> function description or the <a href="#widget-filter-selectsource"><code>filter_selectSource</code></a> option (under "An object containing column keys"; getJSON) for examples which use this function.
</div>
</td>
</tr>
<tr id="function-getfilters">
<td><a href="#" class="permalink">getFilters</a></td>
<td>This filter widget function allows getting an array of the currently applied filters (<span class="version">v2.9</span>; <span class="version updated">v2.15</span>)

View File

@ -4,7 +4,7 @@
*/
/*! tablesorter (FORK) - updated 09-05-2015 (v2.23.3)*/
/*! tablesorter (FORK) - updated 09-07-2015 (v2.23.3)*/
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
if (typeof define === 'function' && define.amd) {
@ -1606,7 +1606,7 @@
// *** sort functions ***
// regex used in natural sort
ts.regex.chunk = /(^([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunk = /(^([+\-]?(?:\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)?$|^0x[0-9a-f]+$|\d+)/gi; // chunk/tokenize numbers & letters
ts.regex.chunks = /(^\\0|\\0$)/; // replace chunks @ ends
ts.regex.hex = /^0x[0-9a-f]+$/i; // hex
@ -3208,7 +3208,7 @@
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.append( options );
txt = wo.filter_selectSource;
fxn = $.isFunction( txt ) ? true : ts.getColumnData( table, txt, column );
fxn = typeof txt === 'function' ? true : ts.getColumnData( table, txt, column );
if ( fxn ) {
// updating so the extra options are appended
tsf.buildSelect( c.table, column, '', true, $header.hasClass( wo.filter_onlyAvail ) );
@ -3227,7 +3227,7 @@
}
if ( wo.filter_hideFilters ) {
tsf.hideFilters( table, c );
tsf.hideFilters( c );
}
// show processing icon
@ -3354,7 +3354,7 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
},
buildRow: function( table, c, wo ) {
var col, column, $header, buildSelect, disabled, name, ffxn, tmp,
var col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter,
columns = c.columns,
@ -3378,14 +3378,14 @@
// assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column );
buildSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
$header.hasClass( 'filter-select' );
// get data from jQuery data, metadata, headers option or header class name
col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( buildSelect ) {
if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
@ -3548,7 +3548,7 @@
return false;
}
},
hideFilters: function( table, c ) {
hideFilters: function( c ) {
var timer;
c.$table
.find( '.' + tscss.filterRow )
@ -4072,14 +4072,12 @@
},
getOptionSource: function( table, column, onlyAvail ) {
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
var c = table.config,
wo = c.widgetOptions,
parsed = [],
arry = false,
source = wo.filter_selectSource,
last = c.$table.data( 'lastSearch' ) || [],
fxn = $.isFunction( source ) ? true : ts.getColumnData( table, source, column );
fxn = typeof source === 'function' ? true : ts.getColumnData( table, source, column );
if ( onlyAvail && last[column] !== '' ) {
onlyAvail = false;
@ -4098,11 +4096,25 @@
// custom select source function for a SPECIFIC COLUMN
arry = fxn( table, column, onlyAvail );
}
if ( arry === false ) {
// fall back to original method
arry = tsf.getOptions( table, column, onlyAvail );
}
return tsf.processOptions( table, column, arry );
},
processOptions: function( table, column, arry ) {
if ( !$.isArray( arry ) ) {
return false;
}
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
validColumn = typeof column !== 'undefined' && column !== null && column >= 0 && column < c.columns,
parsed = [];
// get unique elements and sort the list
// if $.tablesorter.sortText exists ( not in the original tablesorter ),
// then natural sort the list otherwise use a basic sort
@ -4110,7 +4122,7 @@
return $.inArray( value, arry ) === indx;
});
if ( c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
if ( validColumn && c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
// unsorted select options
return arry;
} else {
@ -4123,7 +4135,8 @@
parsed.push({
t : txt,
// check parser length - fixes #934
p : c.parsers && c.parsers.length && c.parsers[ column ].format( txt, table, [], column ) || txt
p : validColumn && c.parsers && c.parsers.length &&
c.parsers[ column ].format( txt, table, [], column ) || txt
});
}
@ -4133,10 +4146,10 @@
// sortNatural breaks if you don't pass it strings
var x = a.p.toString(),
y = b.p.toString();
if ( $.isFunction( cts ) ) {
if ( validColumn && typeof cts === 'function' ) {
// custom OVERALL text sorter
return cts( x, y, true, column, table );
} else if ( typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
} else if ( validColumn && typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
// custom text sorter for a SPECIFIC COLUMN
return cts[column]( x, y, true, column, table );
} else if ( ts.sortNatural ) {
@ -4194,6 +4207,7 @@
if ( !table.config.cache || $.isEmptyObject( table.config.cache ) ) {
return;
}
var indx, val, txt, t, $filters, $filter,
c = table.config,
wo = c.widgetOptions,
@ -4209,6 +4223,7 @@
.find( 'thead' )
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.val();
// nothing included in arry ( external source ), so get the options from
// filter_selectSource or column data
if ( typeof arry === 'undefined' || arry === '' ) {

View File

@ -4,7 +4,7 @@
*/
/*! tablesorter (FORK) - updated 09-05-2015 (v2.23.3)*/
/*! tablesorter (FORK) - updated 09-07-2015 (v2.23.3)*/
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
if (typeof define === 'function' && define.amd) {
@ -864,7 +864,7 @@
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.append( options );
txt = wo.filter_selectSource;
fxn = $.isFunction( txt ) ? true : ts.getColumnData( table, txt, column );
fxn = typeof txt === 'function' ? true : ts.getColumnData( table, txt, column );
if ( fxn ) {
// updating so the extra options are appended
tsf.buildSelect( c.table, column, '', true, $header.hasClass( wo.filter_onlyAvail ) );
@ -883,7 +883,7 @@
}
if ( wo.filter_hideFilters ) {
tsf.hideFilters( table, c );
tsf.hideFilters( c );
}
// show processing icon
@ -1010,7 +1010,7 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
},
buildRow: function( table, c, wo ) {
var col, column, $header, buildSelect, disabled, name, ffxn, tmp,
var col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter,
columns = c.columns,
@ -1034,14 +1034,14 @@
// assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column );
buildSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
$header.hasClass( 'filter-select' );
// get data from jQuery data, metadata, headers option or header class name
col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( buildSelect ) {
if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
@ -1204,7 +1204,7 @@
return false;
}
},
hideFilters: function( table, c ) {
hideFilters: function( c ) {
var timer;
c.$table
.find( '.' + tscss.filterRow )
@ -1728,14 +1728,12 @@
},
getOptionSource: function( table, column, onlyAvail ) {
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
var c = table.config,
wo = c.widgetOptions,
parsed = [],
arry = false,
source = wo.filter_selectSource,
last = c.$table.data( 'lastSearch' ) || [],
fxn = $.isFunction( source ) ? true : ts.getColumnData( table, source, column );
fxn = typeof source === 'function' ? true : ts.getColumnData( table, source, column );
if ( onlyAvail && last[column] !== '' ) {
onlyAvail = false;
@ -1754,11 +1752,25 @@
// custom select source function for a SPECIFIC COLUMN
arry = fxn( table, column, onlyAvail );
}
if ( arry === false ) {
// fall back to original method
arry = tsf.getOptions( table, column, onlyAvail );
}
return tsf.processOptions( table, column, arry );
},
processOptions: function( table, column, arry ) {
if ( !$.isArray( arry ) ) {
return false;
}
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
validColumn = typeof column !== 'undefined' && column !== null && column >= 0 && column < c.columns,
parsed = [];
// get unique elements and sort the list
// if $.tablesorter.sortText exists ( not in the original tablesorter ),
// then natural sort the list otherwise use a basic sort
@ -1766,7 +1778,7 @@
return $.inArray( value, arry ) === indx;
});
if ( c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
if ( validColumn && c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
// unsorted select options
return arry;
} else {
@ -1779,7 +1791,8 @@
parsed.push({
t : txt,
// check parser length - fixes #934
p : c.parsers && c.parsers.length && c.parsers[ column ].format( txt, table, [], column ) || txt
p : validColumn && c.parsers && c.parsers.length &&
c.parsers[ column ].format( txt, table, [], column ) || txt
});
}
@ -1789,10 +1802,10 @@
// sortNatural breaks if you don't pass it strings
var x = a.p.toString(),
y = b.p.toString();
if ( $.isFunction( cts ) ) {
if ( validColumn && typeof cts === 'function' ) {
// custom OVERALL text sorter
return cts( x, y, true, column, table );
} else if ( typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
} else if ( validColumn && typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
// custom text sorter for a SPECIFIC COLUMN
return cts[column]( x, y, true, column, table );
} else if ( ts.sortNatural ) {
@ -1850,6 +1863,7 @@
if ( !table.config.cache || $.isEmptyObject( table.config.cache ) ) {
return;
}
var indx, val, txt, t, $filters, $filter,
c = table.config,
wo = c.widgetOptions,
@ -1865,6 +1879,7 @@
.find( 'thead' )
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.val();
// nothing included in arry ( external source ), so get the options from
// filter_selectSource or column data
if ( typeof arry === 'undefined' || arry === '' ) {

View File

@ -490,7 +490,7 @@
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.append( options );
txt = wo.filter_selectSource;
fxn = $.isFunction( txt ) ? true : ts.getColumnData( table, txt, column );
fxn = typeof txt === 'function' ? true : ts.getColumnData( table, txt, column );
if ( fxn ) {
// updating so the extra options are appended
tsf.buildSelect( c.table, column, '', true, $header.hasClass( wo.filter_onlyAvail ) );
@ -509,7 +509,7 @@
}
if ( wo.filter_hideFilters ) {
tsf.hideFilters( table, c );
tsf.hideFilters( c );
}
// show processing icon
@ -636,7 +636,7 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
},
buildRow: function( table, c, wo ) {
var col, column, $header, buildSelect, disabled, name, ffxn, tmp,
var col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter,
columns = c.columns,
@ -660,14 +660,14 @@
// assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column );
buildSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
$header.hasClass( 'filter-select' );
// get data from jQuery data, metadata, headers option or header class name
col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( buildSelect ) {
if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
@ -830,7 +830,7 @@
return false;
}
},
hideFilters: function( table, c ) {
hideFilters: function( c ) {
var timer;
c.$table
.find( '.' + tscss.filterRow )
@ -1354,14 +1354,12 @@
},
getOptionSource: function( table, column, onlyAvail ) {
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
var c = table.config,
wo = c.widgetOptions,
parsed = [],
arry = false,
source = wo.filter_selectSource,
last = c.$table.data( 'lastSearch' ) || [],
fxn = $.isFunction( source ) ? true : ts.getColumnData( table, source, column );
fxn = typeof source === 'function' ? true : ts.getColumnData( table, source, column );
if ( onlyAvail && last[column] !== '' ) {
onlyAvail = false;
@ -1380,11 +1378,25 @@
// custom select source function for a SPECIFIC COLUMN
arry = fxn( table, column, onlyAvail );
}
if ( arry === false ) {
// fall back to original method
arry = tsf.getOptions( table, column, onlyAvail );
}
return tsf.processOptions( table, column, arry );
},
processOptions: function( table, column, arry ) {
if ( !$.isArray( arry ) ) {
return false;
}
table = $( table )[0];
var cts, txt, indx, len,
c = table.config,
validColumn = typeof column !== 'undefined' && column !== null && column >= 0 && column < c.columns,
parsed = [];
// get unique elements and sort the list
// if $.tablesorter.sortText exists ( not in the original tablesorter ),
// then natural sort the list otherwise use a basic sort
@ -1392,7 +1404,7 @@
return $.inArray( value, arry ) === indx;
});
if ( c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
if ( validColumn && c.$headerIndexed[ column ].hasClass( 'filter-select-nosort' ) ) {
// unsorted select options
return arry;
} else {
@ -1405,7 +1417,8 @@
parsed.push({
t : txt,
// check parser length - fixes #934
p : c.parsers && c.parsers.length && c.parsers[ column ].format( txt, table, [], column ) || txt
p : validColumn && c.parsers && c.parsers.length &&
c.parsers[ column ].format( txt, table, [], column ) || txt
});
}
@ -1415,10 +1428,10 @@
// sortNatural breaks if you don't pass it strings
var x = a.p.toString(),
y = b.p.toString();
if ( $.isFunction( cts ) ) {
if ( validColumn && typeof cts === 'function' ) {
// custom OVERALL text sorter
return cts( x, y, true, column, table );
} else if ( typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
} else if ( validColumn && typeof cts === 'object' && cts.hasOwnProperty( column ) ) {
// custom text sorter for a SPECIFIC COLUMN
return cts[column]( x, y, true, column, table );
} else if ( ts.sortNatural ) {
@ -1476,6 +1489,7 @@
if ( !table.config.cache || $.isEmptyObject( table.config.cache ) ) {
return;
}
var indx, val, txt, t, $filters, $filter,
c = table.config,
wo = c.widgetOptions,
@ -1491,6 +1505,7 @@
.find( 'thead' )
.find( 'select.' + tscss.filter + '[data-column="' + column + '"]' )
.val();
// nothing included in arry ( external source ), so get the options from
// filter_selectSource or column data
if ( typeof arry === 'undefined' || arry === '' ) {

View File

@ -285,13 +285,13 @@ jQuery(function($){
this.c.$table.find('.tablesorter-filter-row select:first option').each(function(){
opts.push( $.trim( $(this).text() ) );
});
assert.equal ( opts.length === 3 && opts.join('') === '< 10> 10', true, 'filter_functions set' );
assert.equal ( 'len=' + opts.length + ',' + opts.join(''), 'len=3,< 10> 10', 'filter_functions set' );
opts = [];
$t.find('option').each(function(){
opts.push( $.trim( $(this).text() ) );
});
assert.equal ( opts.length === 4 && opts.join('') === 'abcdefzyx', true, 'filter_selectSource set' );
assert.equal ( 'len=' + opts.length + ',' + opts.join(''), 'len=4,abcdefzyx', 'filter_selectSource set' );
});