Skip to content

Instantly share code, notes, and snippets.

@gavinr
Created April 7, 2015 02:22
Show Gist options
  • Save gavinr/a7fad05935f9505f4289 to your computer and use it in GitHub Desktop.
Save gavinr/a7fad05935f9505f4289 to your computer and use it in GitHub Desktop.
Function to filter a dgrid/dstore
var filterGrid = function(grid, memory, searchTerm) {
var setToMemory;
// if the search is empty, "turn off" filter
if (searchTerm === "") {
setToMemory = this.memory;
} else {
var mainFilter;
// go though each column, applying the filter for each:
for (var key in grid.columns) {
if (grid.columns.hasOwnProperty(key)) {
var columnName = grid.columns[key].id;
var filter = new Filter().match(columnName, new RegExp(searchTerm + "+", "i"));
if (mainFilter) {
mainFilter = new Filter().or(mainFilter, filter);
} else {
mainFilter = filter;
}
}
}
setToMemory = memory.filter(mainFilter);
}
// set the memory that we've computed above in the if/else
grid.set("collection", setToMemory);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment