为什么没有数据源的 <mat-table> 没有显示在 Angular 7 中

Tar*_*are 6 angular-material angular mat-table

我想使用 a<mat-table>来显示选项的名称及其对应的给定值(供用户更改)。由于选项可能需要通过不同方式(例如滑动切换或垫子选择)设置其值,因此我必须在没有数据源的情况下将其写下来(或者至少据我所知,不能在打字稿文件中使用预先编写的 html 标签)。

因此,我的 MWE 将是这样的:

<mat-table>
    <mat-header-row *matHeaderRowDef>
        <mat-header-cell>header</mat-header-cell>
    </mat-header-row>

    <mat-row>
        <mat-cell>
             cell
        </mat-cell>
    </mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)

但是,当我查看我的页面时,它只显示一行没有任何字符串。我想在一个<mat-card>(或者更确切地说<mat-card-content>)内使用这个表,但即使我在它之外尝试它,我也只是得到了这条线而没有别的。这是垫卡中的样子:

空行

如何正确显示表格?


*编辑:* 因为它被要求,这里也是.ts文件。

import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  selector: 'app-general',
  templateUrl: './general.component.html',
  styleUrls: ['./general.component.scss']
})
export class GeneralComponent implements OnInit {


  @ViewChild('resolutionSelect') resolutionSelect: MatSelect;

  resolutions: ResolutionOptionsInterface[] = [
    { value: '1920 x 1080' },
    { value: '800 x 600' }
  ];
  public selectedRes = this.resolutions[0];
  public isFullscreenEnabled = true;

  constructor() { }

  ngOnInit() {
    console.log("in oninit");
    this.resolutionSelect.value = this.resolutions[0].value;
  }

}
Run Code Online (Sandbox Code Playgroud)

这比最小的工作示例多一点,所以我会解释一下:

  • 我的选择之一是分辨率,它可以由 mat-select
  • mat-select在 html 文件中定义如下
  • mat-select将被赋予预定义的值,如resolutions数组中所定义
  • 我的另一个选择就是选择全屏,使它成为一个mat-slide-toggle(但是,这还没有完全实现)

    <mat-select #resolutionSelect fxFlex="200px"> <mat-option *ngFor="let res of resolutions" [value]="res.value" class="right"> {{res.value}} </mat-option> </mat-select>

tal*_*ure 11

事实上,可以在没有数据源的情况下创建材料表。您需要确保使用显示的列和所有内容创建标题定义。

示例 - html 文件:

<table mat-table class="mat-elevation-z8">
  <ng-container matColumnDef="someValue">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>Some Value Header</th>
  </ng-container>

  <ng-container matColumnDef="someOtherValue">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>
      Some Other Value Header
    </th>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

在 ts 文件中,您需要定义数组

displayedColumns: string[] = ['someValue', 'someOtherValue'];
Run Code Online (Sandbox Code Playgroud)

编辑: 如果您的要求只是一个带有几个预定义值的简单表格,您可以通过使用带有材料 css 类的本机表格元素来实现它:

<table class="mat-table" >
  <tr class="mat-header-row">
    <th class="mat-header-cell">A</th>
    <th class="mat-header-cell">B</th>
  </tr>
  <tr class="mat-row">
    <td class="mat-cell">A</td>
    <td class="mat-cell">B</td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

  • 我不明白如何以这种方式单独添加行和单元格。1.我是否必须在每一行添加 `*matRowDef="let row; columns: displayedColumsn"` ?2.我尝试只使用这一行,并将 `&lt;mat-cell&gt;A&lt;/mat-cell&gt;&lt;mat-cell&gt;B&lt;/mat-cell&gt;` 添加到该行中,但仍然没有显示任何内容(尽管显然应该有一些东西,因为我猜是“mat-elevation-z8”) (2认同)