Filter: filter_liveSearch set to number now searches when pressing enter. Fixes #654

This commit is contained in:
Mottie 2014-06-20 12:31:00 -05:00
parent 931e4c1bae
commit ea48d95fbc
2 changed files with 39 additions and 31 deletions

View File

@ -431,7 +431,7 @@ $(function(){
</tbody>
<tbody>
<tr><th colspan="3">Removed options</th><tr>
<tr><th colspan="3">Removed options</th></tr>
</tbody>
<tbody>
@ -562,12 +562,12 @@ $(".search").bind('search keyup', function (e) {
<h3><a href="#">Changes</a></h3>
<div class="inner">
<p>Moved to the wiki pages - <a href="https://github.com/Mottie/tablesorter/wiki/Changes">filter change log</a>.
Moved to the wiki pages - <a href="https://github.com/Mottie/tablesorter/wiki/Changes">filter change log</a>.
</div>
<h3><a href="#">Localization</a></h3>
<div class="inner">
<p>You can now change the language of the searches used within the filter widget. For example, to change the localization to French, do the following:
You can now change the language of the searches used within the filter widget. For example, to change the localization to French, do the following:
<pre class="prettyprint lang-js">// add French support
$.extend($.tablesorter.language, {
to : 'à',

View File

@ -568,6 +568,9 @@ ts.filter = {
}
c.$table.addClass('hasFilters');
// define searchTimer so using clearTimeout won't cause an undefined error
wo.searchTimer = null;
$.extend( regex, {
child : new RegExp(c.cssChildRow),
filtered : new RegExp(wo.filter_filteredRow),
@ -592,6 +595,7 @@ ts.filter = {
ts.filter.buildDefault(table, true);
}
if (event.type === 'filterReset') {
c.$table.find('.' + ts.css.filter).add(wo.filter_$externalFilters).val('');
ts.filter.searching(table, []);
} else if (event.type === 'filterEnd') {
ts.filter.buildDefault(table, true);
@ -788,29 +792,46 @@ ts.filter = {
.attr('data-lastSearchTime', new Date().getTime())
.unbind('keypress keyup search change '.split(' ').join(c.namespace + 'filter '))
// include change for select - fixes #473
.bind('keyup search change '.split(' ').join(c.namespace + 'filter '), function(event) {
.bind('keyup' + c.namespace + 'filter', function(event) {
$(this).attr('data-lastSearchTime', new Date().getTime());
// emulate what webkit does.... escape clears the filter
if (event.which === 27) {
this.value = '';
// live search
} else if ( wo.filter_liveSearch === false ) {
return;
// don't return if the search value is empty (all rows need to be revealed)
} else if ( this.value !== '' && (
// liveSearch can contain a min value length; ignore arrow and meta keys, but allow backspace
} else if ( (typeof wo.filter_liveSearch === 'number' && this.value.length < wo.filter_liveSearch && this.value !== '') ||
( event.type === 'keyup' && ( (event.which < 32 && event.which !== 8 && wo.filter_liveSearch === true && event.which !== 13) ||
( event.which >= 37 && event.which <= 40 ) || (event.which !== 13 && wo.filter_liveSearch === false) ) ) ) {
( typeof wo.filter_liveSearch === 'number' && this.value.length < wo.filter_liveSearch ) ||
// let return & backspace continue on, but ignore arrows & non-valid characters
( event.which !== 13 && event.which !== 8 && ( event.which < 32 || (event.which >= 37 && event.which <= 40) ) ) ) ) {
return;
}
// change event = no delay; last true flag tells getFilters to skip newest timed input
ts.filter.searching( table, event.type !== 'change', true );
ts.filter.searching( table, true, true );
})
.bind('keypress.' + c.namespace + 'filter', function(event){
if (event.which === 13) {
.bind('search change keypress '.split(' ').join(c.namespace + 'filter '), function(event){
// triggered change event won't fire a search unless the return key was pressed
if (event.which === 13 || event.type === 'search') {
event.preventDefault();
$(this).blur();
// init search with no delay
ts.filter.searching( table, false, true );
}
});
c.$table.bind('filterReset', function(){
$el.val('');
});
},
searching: function(table, filter, skipFirst) {
var wo = table.config.widgetOptions;
clearTimeout(wo.searchTimer);
if (typeof filter === 'undefined' || filter === true) {
// delay filtering
wo.searchTimer = setTimeout(function() {
ts.filter.checkFilters(table, filter, skipFirst );
}, wo.filter_liveSearch ? wo.filter_searchDelay : 10);
} else {
// skip delay
ts.filter.checkFilters(table, filter, skipFirst);
}
},
checkFilters: function(table, filter, skipFirst) {
var c = table.config,
@ -1221,19 +1242,6 @@ ts.filter = {
ts.filter.buildSelect(table, columnIndex, updating, $header.hasClass(wo.filter_onlyAvail));
}
}
},
searching: function(table, filter, skipFirst) {
if (typeof filter === 'undefined' || filter === true) {
var wo = table.config.widgetOptions;
// delay filtering
clearTimeout(wo.searchTimer);
wo.searchTimer = setTimeout(function() {
ts.filter.checkFilters(table, filter, skipFirst );
}, wo.filter_liveSearch ? wo.filter_searchDelay : 10);
} else {
// skip delay
ts.filter.checkFilters(table, filter, skipFirst);
}
}
};