Skip to content

Instantly share code, notes, and snippets.

@jsanta
Created March 9, 2018 15:07
Show Gist options
  • Save jsanta/9666206c0071f6c30b458418e114b5a9 to your computer and use it in GitHub Desktop.
Save jsanta/9666206c0071f6c30b458418e114b5a9 to your computer and use it in GitHub Desktop.
toggleFilter function. Adds or removes a simple search filter from the activeFilters array. Then it filters the original data to a filteredData array.
private activeFilters: Array<string> = [];
toggleFilter(_filter: string) {
const filterIdx: number = findIndex(this.activeFilters, (v) => v === _filter );
if (filterIdx !== -1) {
pullAt(this.activeFilters, [ filterIdx ]);
} else {
this.activeFilters.push(_filter);
}
this.filteredData = filter(this.data, (v) => includes(this.activeFilters, v.color));
}
@jsanta
Copy link
Author

jsanta commented Mar 9, 2018

Beware that this code uses lodash to search, remove and check active filters (findindex, pullAt, filter and includes functions).
Import them from lodash-es like this:

import filter from 'lodash-es/filter';
import findIndex from 'lodash-es/findIndex';
import includes from 'lodash-es/includes';
import pullAt from 'lodash-es/pullAt';

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