为什么是 没有在Angular 7中显示的数据源

问题描述 投票:0回答:1

我想使用<mat-table>来显示一个选项的名称,它是相应的给定值(供用户更改)。由于选项可能需要通过不同的方式设置它们的值(例如滑动切换或mat选择),我必须在没有数据源的情况下写下这个(或者至少我找不到,不能在打字稿文件中使用预先写好的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>

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

Blank Line

如何正确显示表格?


*编辑:*因为它被要求,这里也是.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>
angular angular-material mat-table
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.