How To Add Row After Every 3 Columns In Angular
What i am trying to do is add a row div after every 3 colum divs Example Output need:
Solution 2:
Maybe this works, but Im unsure
<ng-container *ngFor="let p of relatedProperties; let i = index"><divclass="row" *ngIf="(i + 1) / 3 === 1"><divclass="col-md-6" *ngIf="relatedProperties[i - 2].title != null">{{ relatedProperties[i].title }}</div><divclass="col-md-6" *ngIf="relatedProperties[i - 1].title != null">{{ relatedProperties[i].title }}</div><divclass="col-md-6" *ngIf="relatedProperties[i].title != null">{{ relatedProperties[i].title }}</div></div></ng-container>
Also, I gotta say this feels pretty hacky, but if this is what you request, this should work
edit:
Note that strict null (a == null) is better than checking for undefined (a === undefined), as it will check for both undefined or null. In your case title != null
.
Also, you could build an iterable that holds the structure you want in a cleaner way.
instead of having [title1, title2, title3, title4, title5, title6...]
you should try to have [[title1, title2, title3], [title4, title5, title6], ...] which is way cleaner and allows you to simply have two *ngFors inside your template
<element1 *ngFor="let innerArray of myArray; let i = index"><element2 *ngFor="let title of innerAray; let j = index"></element2></element1>
And finally, I suggest you avoid calling a variable 'p', that's bad practice.
Solution 3:
You can use this to add a row after every 3 columns.
<ng-container *ngFor="let p of relatedProperties; let i = index"><divclass="row" *ngIf="(i%3)==0"><divclass="col-md-6" *ngFor="let p of relatedProperties.slice(i, i+3)>
<span *ngIf="relatedProperties[i].title !== undefined"> {{ relatedProperties[i].title }} </span></div></div></ng-container>
Post a Comment for "How To Add Row After Every 3 Columns In Angular"