I would like to know how the setFilter method works for the EntityCollectionService in @ngrx/data. The documentation hints at how it is used, but there is no example showing the actual setFilter(pattern: any) function being used. Since the argument can be of type any, I cannot really infer what should be done here.
Basically, I have a list of objects in the data store using the @ngrx/data module. I would like to define a filter so that I can subscribe to the filteredEntities$ observable of the EntityCollectionService. I can successfully subscribe to the entities$ observable and receive the full unfiltered list. Previously, I was doing the filtering outside of the EntityCollectionService, but I would like to utilize the built-in filtering mechanism.
export class MyComponent implements OnInit {
filteredProjects$: Observable<Project[]>;
typeFilterOptions: FilterOption[];
stageFilterOptions: FilterOption[];
constructor(private projectService: ProjectEntityService, ptivate metadataService: MetadataService) {}
ngOnInit() {
this.typeFilterOptions = this.metadataService.getProjectTypes();
this.stageFilterOptions = this.metadataService.getProjectStages();
this.filteredProjects$ = this.projectService.filteredEntities$;
}
onFilterChange() {
typeFilter = typeFilterOptions.filter(option => option.isChecked).map(option.name);
stageFilter = stageFilterOptions.filter(option => option.isChecked).map(option.name);
this.projectService.setFilter(project => {
return (typeFilter.indexOf(project.type) >= 0) &&
(stageFilter.indexOf(project.stage) >= 0);
}
}
}
The above code is my best approach at trying to set the filter correctly. Obviously, that is not working as I expected it would. When setting the filter to a filter function nothing changes even though I can see the set filter
action firing as expected. The entities are still not being filtered at that point. The argument being label as pattern: any
make me think that it should be something other than a function, but again I cannot infer off of the documentation what it is expecting.