Reputation: 121
Code:
<mat-button-toggle-group value="0" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group2="matButtonToggleGroup">
<div *ngFor="let item of test; let i = index;">
<mat-button-toggle *ngIf="!item.answer" value="{{i}}">{{i}}</mat-button-toggle>
</div>
</mat-button-toggle-group>
This is how it looks when test.length is greater than 5 :
The desired output:
I have tried various combinations of display, width and height CSS but nothing works. How can I split the buttons into rows?
Upvotes: 2
Views: 2746
Reputation: 1784
According to this article, you can do a little function to split your first array into arrays of same length.
Once you got your array of array you can easily iterate over the arrays in your array as @Frost said in a comment.
<mat-button-toggle-group value="0" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group2="matButtonToggleGroup">
<div *ngFor="let array of test">
<div *ngFor="let item of array; let i = index">
<mat-button-toggle *ngIf="!item.answer" value="{{i}}">{{i}}</mat-button-toggle>
</div>
</div>
</mat-button-toggle-group>
If you want to keep your array of 1 dimension, you should iterate over it and use modulo to add a blank element to go to next line as follow
<ng-container *ngFor="let item of test; let i = index">
<div style="display:block" *ngIf="i%5 == 0">
</div>
{{i}}
</ng-container>
Upvotes: 3