diff --git a/js/jquery.tablesorter.widgets.js b/js/jquery.tablesorter.widgets.js index e6685163..f96c7d02 100644 --- a/js/jquery.tablesorter.widgets.js +++ b/js/jquery.tablesorter.widgets.js @@ -245,7 +245,7 @@ ts.addWidget({ ts.benchmark("Applying " + theme + " theme", time); } }, - remove: function(table, c) { + remove: function(table, c, wo) { var $table = c.$table, theme = c.theme || 'jui', themes = ts.themes[ theme ] || ts.themes.jui, @@ -354,7 +354,6 @@ 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_defaultType : [], // 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 @@ -411,25 +410,24 @@ 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 - query : /\{query\}/i // replace filter queries + operators : /[<>=]/g // replace operators }, - // 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.cached = 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) + // 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) types: { // Look for regex - regex: function( c, data ) { - if ( ts.filter.regex.regex.test(data.iFilter) ) { + regex: function( filter, iFilter, exact, iExact ) { + if ( ts.filter.regex.regex.test(iFilter) ) { var matches, - regex = ts.filter.regex.regex.exec(data.iFilter); + regex = ts.filter.regex.regex.exec(iFilter); try { - matches = new RegExp(regex[1], regex[2]).test( data.iExact ); + matches = new RegExp(regex[1], regex[2]).test( iExact ); } catch (error) { matches = false; } @@ -438,29 +436,27 @@ ts.filter = { return null; }, // Look for operators >, >=, < or <= - operators: function( c, data ) { - if ( /^[<>]=?/.test(data.iFilter) ) { + operators: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) { + if ( /^[<>]=?/.test(iFilter) ) { var cachedValue, result, - table = c.table, - index = data.index, - parsed = data.parsed[index], - query = ts.formatFloat( data.iFilter.replace(ts.filter.regex.operators, ''), table ), + c = table.config, + query = ts.formatFloat( 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 || parser.type === 'numeric') { - result = ts.filter.parseFilter(c, $.trim('' + data.iFilter.replace(ts.filter.regex.operators, '')), index, parsed, true); + if (parsed[index] || parser.type === 'numeric') { + result = ts.filter.parseFilter(table, $.trim('' + iFilter.replace(ts.filter.regex.operators, '')), index, parsed[index], 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 || parser.type === 'numeric' ) && !isNaN(query) && typeof data.cached !== 'undefined' ? data.cached : - isNaN(data.iExact) ? ts.formatFloat( data.iExact.replace(ts.filter.regex.nondigit, ''), table) : - ts.formatFloat( data.iExact, table ); + 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 ); - if ( />/.test(data.iFilter) ) { result = />=/.test(data.iFilter) ? cachedValue >= query : cachedValue > query; } - if ( //.test(iFilter) ) { result = />=/.test(iFilter) ? cachedValue >= query : cachedValue > query; } + if ( /= 0); + var indx = iExact.search( $.trim(iFilter) ); + return iFilter === '' ? true : !(wo.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( c, data ) { + exact: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed, rowArray ) { /*jshint eqeqeq:false */ - 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; + 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; } return null; }, // Look for an AND or && operator (logical and) - 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, + 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, indx = query.length - 1; while (result && indx) { - result = result && data.iExact.search( $.trim( ts.filter.parseFilter(c, query[indx], index, parsed) ) ) >= 0; + result = result && iExact.search( $.trim(ts.filter.parseFilter(table, query[indx], index, parsed[index])) ) >= 0; indx--; } return result; @@ -509,55 +502,52 @@ ts.filter = { return null; }, // Look for a range (using " to " or " - ") - see issue #166; thanks matzhu! - range : function( c, data ) { - if ( ts.filter.regex.toTest.test(data.iFilter) ) { + range : function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) { + if ( ts.filter.regex.toTest.test(iFilter) ) { var result, tmp, - table = c.table, - index = data.index, - parsed = data.parsed[index], + c = table.config, // make sure the dash is for a range and not indicating a negative number - 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 ); + 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 ); // parse filter value in case we're comparing numbers (dates) - if (parsed || c.parsers[index].type === 'numeric') { + if (parsed[index] || 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 || c.parsers[index].type === 'numeric' ) && !isNaN(range1) && !isNaN(range2) ? data.cached : - isNaN(data.iExact) ? ts.formatFloat( data.iExact.replace(ts.filter.regex.nondigit, ''), table) : - ts.formatFloat( data.iExact, table ); + 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 ); 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( 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); + 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]); // 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 = data.anyMatch && $.isArray(data.rowArray) ? '(' + query + ')' : '^(' + query + ')$'; + query = $.isArray(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(data.iExact); + return new RegExp( query.replace(/\?/g, '\\S{1}').replace(/\*/g, '\\S*') ).test(iExact); } return null; }, // fuzzy text search; modified from https://github.com/mattyork/fuzzy (MIT license) - fuzzy: function( c, data ) { - if ( /^~/.test(data.iFilter) ) { + fuzzy: function( filter, iFilter, exact, iExact, cached, index, table, wo, parsed ) { + if ( /^~/.test(iFilter) ) { var indx, patternIndx = 0, - len = data.iExact.length, - pattern = ts.filter.parseFilter(c, data.iFilter.slice(1), data.index, data.parsed[data.index]); + len = iExact.length, + pattern = ts.filter.parseFilter(table, iFilter.slice(1), index, parsed[index]); for (indx = 0; indx < len; indx++) { - if (data.iExact[indx] === pattern[patternIndx]) { + if (iExact[indx] === pattern[patternIndx]) { patternIndx += 1; } } @@ -790,9 +780,10 @@ ts.filter = { c.$table.data('lastSearch', filters); return filters; }, - parseFilter: function(c, filter, column, parsed, forceParse){ + parseFilter: function(table, filter, column, parsed, forceParse){ + var c = table.config; return forceParse || parsed ? - c.parsers[column].format( filter, c.table, [], column ) : + c.parsers[column].format( filter, table, [], column ) : filter; }, buildRow: function(table, c, wo) { @@ -1002,26 +993,24 @@ ts.filter = { }, findRows: function(table, filters, combinedFilters) { if (table.config.lastCombinedFilter === combinedFilters) { return; } - var len, $rows, rowIndex, tbodyIndex, $tbody, $cells, columnIndex, - childRow, lastSearch, matches, result, showRow, time, val, indx, - query, notFiltered, searchFiltered, filterMatched, fxn, ffxn, + 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, 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 - 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. ) - ts.getData && ts.getData(c.$headers.filter('[data-column="' + columnIndex + '"]:last'), ts.getColumnData( table, c.headers, columnIndex ), 'filter') === 'parsed' || - $(this).hasClass('filter-parsed'); - }).get(); - + 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. ) + 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; @@ -1071,21 +1060,12 @@ ts.filter = { ts.log( "Searching through " + ( searchFiltered && notFiltered < len ? notFiltered : "all" ) + " rows" ); } if ((wo.filter_$anyMatch && wo.filter_$anyMatch.length) || filters[c.columns]) { - data.anyMatch = true; - data.filter = wo.filter_$anyMatch && wo.filter_$anyMatch.val() || filters[c.columns] || ''; + anyMatch = wo.filter_$anyMatch && wo.filter_$anyMatch.val() || filters[c.columns] || ''; if (c.sortLocaleCompare) { // replace accents - data.filter = ts.replaceAccents(data.filter); + anyMatch = ts.replaceAccents(anyMatch); } - if (regex.query.test(wo.filter_defaultType[c.columns] || '')) { - query = data.filter.split(/\s/); - val = wo.filter_defaultType[c.columns]; - for (indx = 0; indx < query.length; indx++) { - val = val.replace(regex.query, query[indx]); - } - data.filter = val; - } - data.iFilter = data.filter.toLowerCase(); + iAnyMatch = anyMatch.toLowerCase(); } // loop through the rows for (rowIndex = 0; rowIndex < len; rowIndex++) { @@ -1098,14 +1078,14 @@ 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 - data.childRowText = (childRow.length && wo.filter_childRows) ? childRow.text() : ''; - data.childRowText = wo.filter_ignoreCase ? data.childRowText.toLocaleLowerCase() : data.childRowText; + childRowText = (childRow.length && wo.filter_childRows) ? childRow.text() : ''; + childRowText = wo.filter_ignoreCase ? childRowText.toLocaleLowerCase() : childRowText; $cells = $rows.eq(rowIndex).children(); - if (data.anyMatch) { - data.rowArray = $cells.map(function(i){ + if (anyMatch) { + rowArray = $cells.map(function(i){ var txt; - if (data.parsed[i]) { + if (parsed[i]) { txt = c.cache[tbodyIndex].normalized[rowIndex][i]; } else { txt = wo.filter_ignoreCase ? $(this).text().toLowerCase() : $(this).text(); @@ -1115,13 +1095,13 @@ ts.filter = { } return txt; }).get(); - data.exact = data.rowArray.join(' '); - data.iExact = data.exact.toLowerCase(); - data.cached = c.cache[tbodyIndex].normalized[rowIndex].slice(0,-1).join(' '); + rowText = rowArray.join(' '); + iRowText = rowText.toLowerCase(); + rowCache = c.cache[tbodyIndex].normalized[rowIndex].slice(0,-1).join(' '); filterMatched = null; $.each(ts.filter.types, function(type, typeFunction) { - if ($.inArray(type, noAnyMatch) < 0) { - matches = typeFunction( c, data ); + if ($.inArray(type, anyMatchNotAllowedTypes) < 0) { + matches = typeFunction( anyMatch, iAnyMatch, rowText, iRowText, rowCache, columns, table, wo, parsed, rowArray ); if (matches !== null) { filterMatched = matches; return false; @@ -1133,33 +1113,30 @@ ts.filter = { } else { if (wo.filter_startsWith) { showRow = false; - columnIndex = c.columns; + columnIndex = columns; while (!showRow && columnIndex > 0) { columnIndex--; - showRow = showRow || data.rowArray[columnIndex].indexOf(data.iFilter) === 0; + showRow = showRow || rowArray[columnIndex].indexOf(iAnyMatch) === 0; } } else { - showRow = (data.iExact + data.childRowText).indexOf(data.iFilter) >= 0; + showRow = (iRowText + childRowText).indexOf(iAnyMatch) >= 0; } } - data.anyMatch = false; } - for (columnIndex = 0; columnIndex < c.columns; columnIndex++) { - data.filter = filters[columnIndex]; - data.index = columnIndex; + for (columnIndex = 0; columnIndex < columns; columnIndex++) { // ignore if filter is empty or disabled - if (data.filter) { - data.cached = c.cache[tbodyIndex].normalized[rowIndex][columnIndex]; + if (filters[columnIndex]) { + cached = c.cache[tbodyIndex].normalized[rowIndex][columnIndex]; // check if column data should be from the cell or from parsed data - if (wo.filter_useParsedData || data.parsed[columnIndex]) { - data.exact = data.cached; + if (wo.filter_useParsedData || parsed[columnIndex]) { + exact = cached; } else { // using older or original tablesorter - data.exact = $.trim( $cells.eq(columnIndex).text() ); - data.exact = c.sortLocaleCompare ? ts.replaceAccents(data.exact) : data.exact; // issue #405 + exact = $.trim($cells.eq(columnIndex).text()); + exact = c.sortLocaleCompare ? ts.replaceAccents(exact) : exact; // issue #405 } - data.iExact = !regex.type.test(typeof data.exact) && wo.filter_ignoreCase ? data.exact.toLocaleLowerCase() : data.exact; + iExact = !regex.type.test(typeof exact) && wo.filter_ignoreCase ? exact.toLocaleLowerCase() : 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" @@ -1167,28 +1144,28 @@ ts.filter = { c.$filters.add(c.$externalFilters).filter('[data-column="'+ columnIndex + '"]').find('select option:selected').attr('data-function-name') || '' : ''; // replace accents - see #357 - data.filter = c.sortLocaleCompare ? ts.replaceAccents(data.filter) : data.filter; - // val = case insensitive, columnFilter = case sensitive - data.iFilter = wo.filter_ignoreCase ? (data.filter || '').toLocaleLowerCase() : data.filter; + 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]; 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')) ? - data.iExact.search(data.iFilter) >= 0 : data.filter === data.exact; + iExact.search(iFilter) >= 0 : filters[columnIndex] === exact; } else if (typeof fxn === 'function') { // filter callback( exact cell content, parser normalized content, filter input value, column index, jQuery row object ) - result = fxn(data.exact, data.cached, data.filter, columnIndex, $rows.eq(rowIndex)); - } else if (typeof fxn[ffxn || data.filter] === 'function') { + result = fxn(exact, cached, filters[columnIndex], columnIndex, $rows.eq(rowIndex)); + } else if (typeof fxn[ffxn || filters[columnIndex]] === 'function') { // selector option function - result = fxn[ffxn || data.filter](data.exact, data.cached, data.filter, columnIndex, $rows.eq(rowIndex)); + result = fxn[ffxn || filters[columnIndex]](exact, cached, filters[columnIndex], 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( c, data ); + matches = typeFunction( filters[columnIndex], iFilter, exact, iExact, cached, columnIndex, table, wo, parsed ); if (matches !== null) { filterMatched = matches; return false; @@ -1198,8 +1175,8 @@ ts.filter = { result = filterMatched; // Look for match, and add child row data for matching } else { - 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) ); + exact = (iExact + childRowText).indexOf( ts.filter.parseFilter(table, iFilter, columnIndex, parsed[columnIndex]) ); + result = ( (!wo.filter_startsWith && exact >= 0) || (wo.filter_startsWith && exact === 0) ); } } showRow = (result) ? showRow : false; diff --git a/js/widgets/widget-editable.js b/js/widgets/widget-editable.js index 7eb69bbc..085cd892 100644 --- a/js/widgets/widget-editable.js +++ b/js/widgets/widget-editable.js @@ -128,7 +128,7 @@ column = $this.closest('td').index(); if ( e.which === 27 ) { // user cancelled - $this.html( $this.data('original') ).trigger('blur.tseditable'); + $this.html( $.trim( $this.data('original') ) ).trigger('blur.tseditable'); c.$table.data( 'contentFocused', false ); return false; } @@ -149,7 +149,7 @@ c.$table.find('.tseditable-last-edited-cell').removeClass('tseditable-last-edited-cell'); $this .addClass('tseditable-last-edited-cell') - .html( valid ) + .html( $.trim( valid ) ) .data('before', valid) .data('original', valid) .trigger('change'); @@ -174,7 +174,7 @@ } }, 100)); // restore original content on blur - $this.html( $this.data('original') ); + $this.html( $.trim( $this.data('original') ) ); } }); }