javascript - 在 mat-grid 中使用 mat-button-toggle-group

标签 javascript css angular angular5 angular-material-5

我有 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/

相关文章:

javascript - 如何全局定义nconf的默认值?

javascript - AngularJs:相对路径不使用 $location.path 重定向

javascript - react native : Why doesn't <TextInput/> center with flexbox?

html - 如何在 <h1> 标签内垂直对齐图像

html - 使子 flexbox 容器可滚动

html - 如何根据事件选项卡隐藏/显示动画 svg?

javascript - 如何测试@Input()项: any in Angular?

javascript - 单击警报消息中的确定后如何关闭浏览器中的当前窗口?

javascript - Angular 4 typescript Cordova相机插件错误 "Property '相机'在类型 'Navigator'上不存在”

javascript - Angular 2 foreach 在数组被另一个函数更改之前执行