Skip to content

Commit

Permalink
fix filter + ajax issue #270 & added public filter get & set functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed Apr 12, 2013
1 parent 7333519 commit 1042655
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions js/jquery.tablesorter.widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,7 @@ ts.addWidget({
// dig fer gold
checkFilters = function(filter){
var arry = $.isArray(filter),
v = (arry) ? filter : c.$filters.map(function(t){
// make sure input arry index matches header indexes.
t = $(this).find('select.' + css + ', input.' + css);
return t.length ? t.val() || '' : '';
}).get(),
v = (arry) ? filter : ts.getFilters(table),
cv = (v || []).join(''); // combined filter values
// add filter array back into inputs
if (arry) {
Expand Down Expand Up @@ -464,7 +460,7 @@ ts.addWidget({
}

last = cv; // save last search
$t.data('lastSearch', last);
$t.data('lastSearch', v);
if (c.debug){
ts.benchmark("Completed filter widget search", time);
}
Expand Down Expand Up @@ -580,23 +576,24 @@ ts.addWidget({
$t.find('.' + css).val('');
}
// send false argument to force a new search; otherwise if the filter hasn't changed, it will return
filter = e.type === 'search' ? filter : e.type === 'updateComplete' ? $t.data('lastSearch') : false;
filter = e.type === 'search' ? filter : e.type === 'updateComplete' ? $t.data('lastSearch') : '';
checkFilters(filter);
return false;
})
.find('input.' + css).bind('keyup search', function(e, filter){
// ignore arrow and meta keys; allow backspace
if (e.type === 'keyup' && ((e.which < 32 && e.which !== 8) || (e.which >= 37 && e.which <=40) || (e.which !== 13 && !wo.filter_liveSearch))) { return; }
// skip delay
if (typeof filter !== 'undefined' && filter !== true){
checkFilters(filter);
if (typeof filter === 'undefined' || filter === false){
checkFilters();
} else {
// delay filtering
clearTimeout(timer);
timer = setTimeout(function(){
checkFilters(filter);
}, wo.filter_searchDelay);
return false;
}
// delay filtering
clearTimeout(timer);
timer = setTimeout(function(){
checkFilters(false);
}, wo.filter_searchDelay);
return false;
});

// parse columns after formatter, in case the class is added at that point
Expand Down Expand Up @@ -635,8 +632,8 @@ ts.addWidget({
// it would append the same options twice.
buildDefault(true);

$t.find('select.' + css).bind('change search', function(){
checkFilters();
$t.find('select.' + css).bind('change search', function(e, filter){
checkFilters(filter);
});

if (wo.filter_hideFilters){
Expand Down Expand Up @@ -714,6 +711,21 @@ ts.addWidget({
if (wo.filterreset) { $(wo.filter_reset).unbind('click.tsfilter'); }
}
});
ts.getFilters = function(table) {
var c = table ? $(table)[0].config : {};
return c && c.$filters ? c.$filters.find('.' + c.widgetOptions.filter_cssFilter).map(function(i, el) {
return $(el).val();
}).get() || [] : false;
};
ts.setFilters = function(table, filter, apply) {
var $t = $(table),
c = $t.length ? $t[0].config : {},
valid = c && c.$filters ? c.$filters.find('.' + c.widgetOptions.filter_cssFilter).each(function(i, el) {
$(el).val(filter[i] || '');
}) || false : false;
if (valid && apply) { $t.trigger('search'); }
return !!valid;
};

// Widget: Sticky headers
// based on this awesome article:
Expand Down

0 comments on commit 1042655

Please sign in to comment.