Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martindrapeau/9812237 to your computer and use it in GitHub Desktop.
Save martindrapeau/9812237 to your computer and use it in GitHub Desktop.
Extend Backgrid ClientSideFilter with a pick filter, to pre-filter data set. Useful to combine text search with a select, checkboxes or radio buttons filters.
// Subclass Backgrid Filter to pass an extra 'pick' filter
Backgrid.ClientSideFilterWithPickFilter = Backgrid.Extension.ClientSideFilter.extend({
pickFilter: null,
setPickFilter: function (attrs) {
this.pickFilter = attrs;
this.search();
return this;
},
makeMatcher: function (query) {
var regexp = this.makeRegExp(query);
return function (model) {
var json = model.toJSON();
// Test the pick filter (if set)
if (this.pickFilter) {
var match = _.find(this.pickFilter, function (value, key) {
return json[key] === value;
});
if (match === undefined) return false;
}
// Test the search filter
var keys = this.fields || model.keys();
for (var i = 0, l = keys.length; i < l; i++) {
if (regexp.test(json[keys[i]] + "")) return true;
}
return false;
};
}
});
// Assuming you have a checkbox to only show rows with Valid=true
var filter = new Backgrid.ClientSideFilterWithPickFilter({...});
$('input[name=valid]').change(function(e) {
filter.setPickFilter(e.target.checked ? {Valid: true} : null);
});
@martindrapeau
Copy link
Author

Charlie Irish asked me for a way to combine Backgrid Select Filter with Backgrid Filter. They were written to be standalone so hard to do. However, Backgrid.ClientSideFilter can be extended with a pick filter, to pre-filter data. You can then bind input or select elements to narrow down the rows, prior to the text search.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment