问题
I have 5 buttons occupying 5 columns of a mat-grid
. The buttons should behave as toggle buttons and all 5 should belong to a single group.
I tried enclosing the mat-grid
with mat-button-toggle-group
, but still isn't working.
Any help is much appreciated.
Code :
<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>
回答1:
The DOM structure of the grid is getting in the way of the toggle group. You can get around that by adding the group as a standalone element, and assigning the group to each button in your controller. For example:
<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);
});
}
}
A stackblitz example is here.
来源:https://stackoverflow.com/questions/50563805/use-mat-button-toggle-group-inside-a-mat-grid