Skip to content

Commit 3b679cb

Browse files
committed
filter widget input now corresponds to column with multiple thead rows
1 parent fdda285 commit 3b679cb

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

js/jquery.tablesorter.widgets.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ $.tablesorter.addWidget({
180180
format: function(table) {
181181
if (!$(table).hasClass('hasFilters')) {
182182
var i, j, k, l, cv, v, val, r, ff, t, x, xi, cr,
183-
sel, $tb, $tr, $td, reg2,
183+
sel, $tb, $th, $tr, $td, reg2,
184184
c = table.config,
185+
$ths = $(c.headerList),
185186
wo = c.widgetOptions,
186187
css = wo.filter_cssFilter || 'tablesorter-filter',
187188
$t = $(table).addClass('hasFilters'),
@@ -245,18 +246,14 @@ $.tablesorter.addWidget({
245246
}
246247
// Look for quotes to get an exact match
247248
} else if (/[\"|\']$/.test(val) && xi === val.replace(/(\"|\')/g,'')) {
248-
r = (r) ? true : false;
249+
ff = true;
249250
// Look for wild card: ? = single, or * = multiple
250251
} else if (/[\?|\*]/.test(val)) {
251252
ff = new RegExp( val.replace(/\?/g, '\\S{1}').replace(/\*/g, '\\S*') ).test(xi);
252253
// Look for match, and add child row data for matching
253254
} else {
254255
x = (xi + t).indexOf(val);
255-
if ( (!wo.filter_startsWith && x >= 0) || (wo.filter_startsWith && x === 0) ) {
256-
r = (r) ? true : false;
257-
} else {
258-
r = false;
259-
}
256+
ff = ( (!wo.filter_startsWith && x >= 0) || (wo.filter_startsWith && x === 0) );
260257
}
261258
r = (ff) ? (r ? true : false) : false;
262259
}
@@ -275,7 +272,7 @@ $.tablesorter.addWidget({
275272
buildSelect = function(i){
276273
var o, arry = [];
277274
i = parseInt(i, 10);
278-
o = '<option value="">' + ($(c.headerList[i]).attr('data-placeholder') || '') + '</option>';
275+
o = '<option value="">' + ($ths.filter('[data-column="' + i + '"]:last').attr('data-placeholder') || '') + '</option>';
279276
for (k = 0; k < b.length; k++ ) {
280277
l = c.cache[k].row.length;
281278
// loop through the rows
@@ -291,26 +288,27 @@ $.tablesorter.addWidget({
291288
for (k = 0; k < arry.length; k++) {
292289
o += '<option value="' + arry[k] + '">' + arry[k] + '</option>';
293290
}
294-
$t.find('thead').find('select.' + css + '[data-col="' + i + '"]').append(o);
291+
$t.find('thead').find('select.' + css + '[data-column="' + i + '"]').append(o);
295292
};
296293
if (c.debug) {
297294
time = new Date();
298295
}
299296
for (i=0; i < cols; i++){
300-
sel = (wo.filter_functions && wo.filter_functions[i] && typeof wo.filter_functions[i] !== 'function') || $(c.headerList[i]).hasClass('filter-select');
297+
$th = $ths.filter('[data-column="' + i + '"]:last'); // assuming last cell of a column is the main column
298+
sel = (wo.filter_functions && wo.filter_functions[i] && typeof wo.filter_functions[i] !== 'function') || $th.hasClass('filter-select');
301299
fr += '<td>';
302300
if (sel){
303-
fr += '<select data-col="' + i + '" class="' + css;
301+
fr += '<select data-column="' + i + '" class="' + css;
304302
} else {
305-
fr += '<input type="search" placeholder="' + ($(c.headerList[i]).attr('data-placeholder') || "") + '" data-col="' + i + '" class="' + css;
303+
fr += '<input type="search" placeholder="' + ($th.attr('data-placeholder') || "") + '" data-column="' + i + '" class="' + css;
306304
}
307305
// use header option - headers: { 1: { filter: false } } OR add class="filter-false"
308306
if ($.tablesorter.getData) {
309307
// get data from jQuery data, metadata, headers option or header class name
310-
fr += $.tablesorter.getData(c.headerList[i], c.headers[i], 'filter') === 'false' ? ' disabled" disabled' : '"';
308+
fr += $.tablesorter.getData($th[0], c.headers[i], 'filter') === 'false' ? ' disabled" disabled' : '"';
311309
} else {
312310
// only class names and header options - keep this for compatibility with tablesorter v2.0.5
313-
fr += ((c.headers[i] && c.headers[i].hasOwnProperty('filter') && c.headers[i].filter === false) || $(c.headerList[i]).hasClass('filter-false') ) ? ' disabled" disabled' : '"';
311+
fr += ((c.headers[i] && c.headers[i].hasOwnProperty('filter') && c.headers[i].filter === false) || $th.hasClass('filter-false') ) ? ' disabled" disabled' : '"';
314312
}
315313
fr += (sel ? '></select>' : '>') + '</td>';
316314
}
@@ -336,7 +334,7 @@ $.tablesorter.addWidget({
336334
if (wo.filter_functions) {
337335
// i = column # (string)
338336
for (i in wo.filter_functions) {
339-
t = $(c.headerList[i]);
337+
t = $ths.filter('[data-column="' + i + '"]:last');
340338
fr = '';
341339
if (typeof i === 'string' && wo.filter_functions[i] === true && !t.hasClass('filter-false')) {
342340
buildSelect(i);
@@ -348,13 +346,13 @@ $.tablesorter.addWidget({
348346
fr += '<option>' + j + '</option>';
349347
}
350348
}
351-
$t.find('thead').find('select.' + css + '[data-col="' + i + '"]').append(fr);
349+
$t.find('thead').find('select.' + css + '[data-column="' + i + '"]').append(fr);
352350
}
353351
}
354352
}
355353
// build default select dropdown
356-
for (i = 0; i < c.headerList.length; i++) {
357-
t = $(c.headerList[i]);
354+
for (i = 0; i < cols; i++) {
355+
t = $ths.filter('[data-column="' + i + '"]:last');
358356
// look for the filter-select class, but don't build it twice.
359357
if (t.hasClass('filter-select') && !t.hasClass('filter-false') && !(wo.filter_functions && wo.filter_functions[i] === true)){
360358
buildSelect(i);

0 commit comments

Comments
 (0)