我有 5 个按钮,占据了 mat-grid
的 5 列。这些按钮应该像切换按钮一样工作,并且所有 5 个应该属于一个组。
我尝试用 mat-button-toggle-group
封闭 mat-grid
,但仍然无法正常工作。
非常感谢任何帮助。
代码:
<mat-grid-list cols="11" rowHeight="40px">
<mat-grid-tile >
<label>Label 1</label><!-- below buttons should be a single group -->
</mat-grid-tile>
<mat-grid-tile colspan='2'>
<mat-button-toggle >Button 1</mat-button-toggle>
</mat-grid-tile>
....
<mat-grid-tile colspan='2'>
<mat-button-toggle >Button 5</mat-button-toggle>
</mat-gri-tile>
最佳答案
网格的 DOM 结构妨碍了切换组。您可以通过将组添加为独立元素并将组分配给 Controller 中的每个按钮来解决这个问题。例如:
<mat-grid-list cols="3">
<mat-grid-tile>
<mat-button-toggle value="bold">Bold</mat-button-toggle>
</mat-grid-tile>
<mat-grid-tile>
<mat-button-toggle value="italic">Italic</mat-button-toggle>
</mat-grid-tile>
<mat-grid-tile>
<mat-button-toggle value="underline">Underline</mat-button-toggle>
</mat-grid-tile>
</mat-grid-list>
<mat-button-toggle-group name="fontStyle" aria-label="Font Style">
</mat-button-toggle-group>
import { Component, ViewChild, ViewChildren, QueryList } from '@angular/core';
import { MatButtonToggle, MatButtonToggleGroup } from '@angular/material';
@Component({
selector: 'button-toggle-overview-example',
templateUrl: 'button-toggle-overview-example.html',
styleUrls: ['button-toggle-overview-example.css'],
})
export class ButtonToggleOverviewExample {
@ViewChild(MatButtonToggleGroup) group: MatButtonToggleGroup;
@ViewChildren(MatButtonToggle) toggles: QueryList<MatButtonToggle>;
ngAfterViewInit() {
setTimeout(() => {
this.toggles.forEach(toggle => toggle.buttonToggleGroup = this.group);
});
}
}
stackblitz 示例是 here .
关于javascript - 在 mat-grid 中使用 mat-button-toggle-group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50563805/