【问题标题】:Why is the <mat-table> without a Datasource not displayed in Angular 7为什么没有数据源的 <mat-table> 没有显示在 Angular 7 中
【发布时间】:2019-09-18 01:52:49
【问题描述】:

我想使用&lt;mat-table&gt; 来显示选项的名称及其对应的给定值(供用户更改)。由于选项可能需要通过不同的方式设置它们的值(例如滑动切换或垫选择),我必须在没有数据源的情况下写下来(或者至少据我所知,不能在 typescript 文件中使用预先写好的 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>

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

如何正确显示表格?


*编辑:* 既然被要求了,这里也是.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;
  }

}

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

  • 我的选项之一是分辨率,可以通过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>

【问题讨论】:

  • 你也应该把 ts 代码放到你的问题中
  • 那么,你想显示表格,但没有数据源?
  • 我将添加 .ts 代码,虽然我并没有真正看到好处。是的,我想显示表格,但没有数据源,因为我必须手动构建它:我无法将所有可能的输入可能性存储在外部表格中。
  • 好的,matHeaderRowDef 在组件初始化时是否已知?另外,您似乎没有包含mat-cells?
  • @wentjun 是的,基本上它只是“选项”和“值”作为标题。 mat-cells 在那里(或者至少是最小示例的那个)

标签: angular angular-material mat-table


【解决方案1】:

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

示例 - 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>

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

displayedColumns: string[] = ['someValue', 'someOtherValue'];

编辑: 如果您的需求只是一个带有几个预定义值的简单表格,您可以通过使用带有材质 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>

【讨论】:

  • 我不明白,如何以这种方式单独添加行和单元格。 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我猜应该是可见的)
  • ngFor 在哪里?
猜你喜欢
  • 2021-06-13
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多