使用静态数组作为mat-table的数据源 [英] Using a static array as datasource for mat-table

查看:103
本文介绍了使用静态数组作为mat-table的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用角度材料表.我正在尝试使用与示例相同的代码,但是在必须定义[dataSource]="data"时出现问题.

I'm trying to make use of the Angular Material table. I'm trying to use the same code as the examples they have but I tin into a problem when I have to define the [dataSource]="data".

这个问题听起来很愚蠢,但是我的表数据是一个简单的对象数组,我该如何实现呢?

This question may sound stupid but my table data is a simple array of objects, how can I implement that?

为了解释,我的数据看起来像这样:

for the sake of explaining let's say my data looks like this:

public data = [{ ID: 1, Code: "Hi" }, { ID: 2, Code: "Bye" }];

这是我当前拥有的代码:

Here's the code I currently have:

<div class="example-container mat-elevation-z8">
    <mat-table #table [dataSource]="data">
        <ng-container matColumnDef="number">
            <mat-header-cell *matHeaderCellDef> Number </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{ row.ID }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="Code">
            <mat-header-cell *matHeaderCellDef> Code </mat-header-cell>
            <mat-cell *matCellDef="let row">{{row.Code}}</mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>
</div>

推荐答案

我找到了提供的说明缺少角材料表上.也许我可以帮助澄清所提供的示例.

I found the provided instructions on Angular Material Table rather lacking. Maybe I can help clarify the provided examples.

<mat-table [dataSource]="dataSource">
  ...
</mat-table>

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆