Ngfor Inangular Concretepage Com

Elena Vance
-
ngfor inangular concretepage com

'How to output groups with *ngFor in angular based on property? Assume I have the following object 'myarr': [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ] Is it possible to make it so that I can output the following: Chapter 1 Mary Joseph Chapter 2 John Chapter 3 Carl Jacob If so, what is a good way to accomplish this with just one list? I don't want to hardcode Chapter 1, Chapter 2, Chapter 3.

I would like to infer from the data. <div *ngFor="let name of myarr"> // two ngFor to get the chapter? {{name}} </div> Solution 1:[1] There is one "dirty" hack that you can make in order to achieve it. Personally I recommend you to go with the grouping of items, but here is the 100% working solution in case you want to stick to a single list.

Steps are: - Sort your array by chapter - Rely on having different chapter for the previous item when rendering header TypeScript export class AppComponent { arr = [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ]; constructor() { this.arr = this.arr.sort((a,b) => a.chapter > b.chapter ?

1 : -1); } } HTML <div *ngFor="let item of arr; let index = index"> <h3 *ngIf="!arr[index-1] || item.chapter !== arr[index-1].chapter">{{item.chapter}}</h3> {{item.name}} </div> StackBlitz: https://stackblitz.com/edit/angular-4y6anf Solution 2:[2] Check this example here: https://stackblitz.com/edit/group-by-inangular You need to group your items by chapter first, and that should be done in your ts file like this: groupArr = this.myarr.reduce((r,{group})=>{ if(!r.some(o=>o.chapter==chapter)){ r.push({chapter,groupItem:this.myarr.filter(v=>v.chapter==chapter)}); } return r; },[]); and then in your html do this: <table> <tr> <th>ID</th> <th>Name</th> </tr> <tbody *ngFor="let item of groupArr"> <ng-container> <tr> <td colspan="2"><b>{{item.group}}</b></td> </tr> <tr *ngFor="let value of item.groupItem"> <td>{{value.name}}</td> </tr> </ng-container> </tbody> </table> Solution 3:[3] Use a function to determine whether to display the chapter.

component: lastChapter = ''; myarr = [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ]; displayChapter(item): boolean { if (item.chapter !== this.lastChapter) { this.lastChapter = item.chapter; return true; } else { return false; } } trackChapter(index: number; item: any): string { return item.name + item.chapter; } template: <div *ngFor="let item of myarr; trackBy: trackChapter"> <h1 *ngIf="displayChapter(item)">{{ item.chapter }}</h1> <p>{{ item.name }}</p> </div> Sources This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0. Source: Stack Overflow

People Also Asked

NgForin Angular -ConcretePage.com?

'How to output groups with *ngFor in angular based on property? Assume I have the following object 'myarr': [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ] Is it possible to make it so that I can output the following: Chapter 1 Mary Joseph Ch...

Angular ng-template -ConcretePage.com?

I would like to infer from the data. <div *ngFor="let name of myarr"> // two ngFor to get the chapter? {{name}} </div> Solution 1:[1] There is one "dirty" hack that you can make in order to achieve it. Personally I recommend you to go with the grouping of items, but here is the 100% working solution in case you want to stick to a single list.

angular - How to dynamically create elements with *ngForfrom an...?

'How to output groups with *ngFor in angular based on property? Assume I have the following object 'myarr': [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ] Is it possible to make it so that I can output the following: Chapter 1 Mary Joseph Ch...

How to Display Components usingngForin angular 2+?

1 : -1); } } HTML <div *ngFor="let item of arr; let index = index"> <h3 *ngIf="!arr[index-1] || item.chapter !== arr[index-1].chapter">{{item.chapter}}</h3> {{item.name}} </div> StackBlitz: https://stackblitz.com/edit/angular-4y6anf Solution 2:[2] Check this example here: https://stackblitz.com/edit/group-by-inangular You need to group your items by chapter first, and that should be done in your t...

[SOLVED] How to output groups with *ngForin... - DeveloperLoad?

'How to output groups with *ngFor in angular based on property? Assume I have the following object 'myarr': [ {'name':'Mary', 'chapter':'Chapter 1'}, {'name':'Joseph', 'chapter':'Chapter 1'}, {'name':'John', 'chapter':'Chapter 2'}, {'name':'Carl', 'chapter':'Chapter 3'}, {'name':'Jacob', 'chapter':'Chapter 3'} ] Is it possible to make it so that I can output the following: Chapter 1 Mary Joseph Ch...