我有5个按钮占据mat-grid
的5列。按钮应该像切换按钮一样,所有5按钮应该属于一个组
我尝试将mat-grid
与mat-button-toggle-group
括起来,但仍然无效
非常感谢任何帮助。
代码:
<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>
答案 0 :(得分:2)
网格的DOM结构妨碍了切换组。您可以通过将组添加为独立元素并将组分配给控制器中的每个按钮来解决此问题。例如:
<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。