【问题标题】:Split the toggle buttons into multiple rows将切换按钮拆分为多行
【发布时间】:2020-12-07 07:20:16
【问题描述】:

代码:

<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>

这是 test.length 大于 5 时的样子:

想要的输出:

我尝试了显示、宽度和高度 CSS 的各种组合,但没有任何效果。如何将按钮拆分成行?

【问题讨论】:

  • 您只需要 2 ngfor ,其他代码很好,1 ng for row 另一个 ng for 1.2.3 ....(将 maxlimit 设置为 5 for 5 个数字)
  • 你能分享一个例子吗?我找不到为此所需的正确嵌套 ngFor 示例。

标签: css angular angular-material material-ui togglebutton


【解决方案1】:

根据this 文章,您可以做一个小功能将您的第一个数组拆分为相同长度的数组。

一旦你得到你的数组数组,你就可以像@Frost 在评论中所说的那样轻松地遍历数组中的数组。

<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>

如果你想保持你的一维数组,你应该迭代它并使用模数添加一个空白元素到下一行,如下所示

<ng-container *ngFor="let item of test; let i = index">
  <div style="display:block" *ngIf="i%5 == 0">
  </div>
  {{i}}
</ng-container>

【讨论】:

  • 我必须将 let item to array 更改为 let item of array 才能编译。之后我收到Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. 这个错误。
  • 您是否按照我建议的文章创建了一个函数?这个函数应该创建一个数组数组。对于 to,这是我的错,我已经编辑了它。
  • 我已经编辑了另一个不需要拆分数组的解决方案
  • 我已经尝试过第二种解决方案,但它并没有像我想象的那样工作。 display: block 不会将其移至下一行。但是第一个解决方案效果很好 - 你必须使用 toggle-group 的 CSS 和两个 div 让它看起来像第二个图像,但现在它的功能完全符合我的要求。非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
相关资源
最近更新 更多