When i have nested ngFor how to group children by parent ngFor?
So i had something like this:
html:
<div *ngFor="let item of firstChildrenGroupData">
<app-child #firstChildrenGroup [data]="item.data"></app-child>
</div>
<button (click)="doSomethingForGroup(firstGroup)">Click here</button>
<div *ngFor="let item of secondChildrenGroupData">
<app-child #secondChildrenGroup [data]="item.data"></app-child>
</div>
<button (click)="doSomethingForGroup(secondGroup)">Click here</button>
ts:
@ViewChildren('firstChildrenGroup') firstGroup: QueryList<ChildComponent>;
@ViewChildren('secondChildrenGroup') secondGroup: QueryList<ChildComponent>;
doSomethingForGroup(group: QueryList<ChildComponent>) {
group.foreach(item => {
item.doAction();
})
}
And i want to achieve this
html:
<div *ngFor="let group of ChildrenGroups">
<div #group>
<div *ngFor="let item of group.childrenData">
<app-child #child [data]="item.data"></app-child>
</div>
<button (click)="doSomethingForGroup(????)">Click here</button>
</div>
</div>
and this is wrong, because now action will be taken for ALL children if i use this in button but i want to take action only for one group of children per button.
@ViewChildren('child') allChildren: QueryList<ChildComponent>
@edit:
Maybe i could do something like another component <app-parent></app-parent>
that will get group data and then inside this ngFor and <app-child></appChild>
but i would like to avoid making another component in the middle just for grouping.
you can "filter" the queryList before make something, If I suppose your item.data was a property "groupId"
If has no property groupId, you can pass as argument group.childrenData
and use some like
NOTE: I don't check the code, use only as inspiration