Is there a way to programmatically filter an Angular list?
I'm currently working on a project where I need to filter subcategories by clicking on categories.
For example, when clicking on "Drinks," I want to display items like Coke, Fanta, Pepsi...
The filtering of subgroups works when clicked on a group, but I want to set it manually when the .html is loaded, to achieve the same effect as if the user had pressed on a category.
Below is the code snippet:
filteredSubGroups: Group[];
ngOnInit() {
// Getting data from the database and console logging them
this.groups = this._activatedRoute.snapshot.data['groups'];
this.subGroups = this._activatedRoute.snapshot.data['subGroups'];
}
ngAfterViewInit() {
this.selectedId = '78ebcad8-8cb0-4172-8cd8-bb6fb6b3bf53';
this.filteredSubGroups = this.subGroups.filter(item => item.parentId === "78ebcad8-8cb0-4172-8cd8-bb6fb6b3bf53");
}
Template below:
<li *ngFor="let subgroup of filteredSubGroups">
<button type="button" data-toggle="" data-target="" class="btn categories-btn" (click)="getArticlesByGroupId(subgroup.id)" [class.active]="subgroup.id == selectedSubId">
{{subgroup.title | uppercase}}
</button>
</li>
However, upon running the application, the following error occurs:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngForOf: undefined'. Current value: 'ngForOf: [object Object],[object Object],[object Object]'. at viewDebugError (core.js:9817) at expressionChangedAfterItHasBeenCheckedError (core.js:9795) at checkBindingNoChanges (core.js:9962) at checkNoChangesNodeInline (core.js:14010) at checkNoChangesNode (core.js:13984) at debugCheckNoChangesNode (core.js:14813) at debugCheckDirectivesFn (core.js:14715) at Object.eval [as updateDirectives] (MyComponent.html:38) at Object.debugUpdateDirectives [as updateDirectives] (core.js:14697) at checkNoChangesView (core.js:13822)
Addition to original post:
https://i.sstatic.net/21n6o.png
Clicking on main categories displays subcategories:
<li *ngFor="let group of groups;let isFirst = first" (click)="filterSubgroupsByGroupId(group.id)">
<button type="button" data-toggle="tab" data-target="#food" class="btn xbutton-square" [class.active]="group.id == selectedId">
<i [class]="getClass(group.image)" [innerHTML]="getUnicode(group.image)"></i>
</button>
</li>
I am aiming to show categories manually or programmatically upon app launch, without requiring the user to click directly on a category for visibility.