在mat-table上调用renderRow()时出现问题

时间:2018-04-27 04:50:34

标签: angular angular-material2

单击按钮时,我想通过Material Table添加行。但我似乎无法在我的表上调用renderRows()方法。

以下是我的代码中的一些相关内容:

<div fxFlex="20%" fxLayout="row" fxLayoutAlign="center">
        <input fxFlex="10%" matInput placeholder="Part #" type="number" [(ngModel)]="partNumber">
        <input fxFlex="20%" matInput placeholder="Video Url" type="text" [(ngModel)]="videoUrl">
        <input fxFlex="40%" matInput placeholder="Download Url" type="text" [(ngModel)]="downloadUrl">
        <button mat-button [disabled]="partNumber == null || videoUrl == null" (click)="addVideo()"> <-- I would like my table to update on this click
            Add Video
        </button>
    </div>
    <div *ngIf="urlCollection.length > 0">
        <mat-table #videoTable videoTable [dataSource]="dataSource">

            <-- Table columns -->

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

在TS方面:

addVideo()
{
  // get data from the the inputs and add it to the table data collection;
 // Ideally I would like to have the renderRow here. 
}

我尝试在按钮点击事件中调用renderRows(),如下所示:

<button mat-button [disabled]="partNumber == null || videoUrl == null" (click)="addVideo(); videoTable.renderRows()">

就像这样:

<button mat-button [disabled]="partNumber == null || videoUrl == null" (click)="addVideo(); videoTable.table.renderRows()">

我收到的错误类似于“无法调用未定义的renderRow()”

我尝试在我的typescript方法中调用renderRows()方法,如下所示:

@ViewChild('videoTable') videoTable: MatTableModule;
// rest of the component

addVideo()
{
    // add to the datasource collection
    this.videoTable.renderRows();
}

我在类型'MatTableModule'上找不到“属性'renderRows'”错误。

单击按钮并更新数据源时,如何更新my表?

1 个答案:

答案 0 :(得分:2)

您使用错误的类型声明了videoTable,它应该是MatTable

尝试下面的代码示例:

import { MatTable } from '@angular/material';

@ViewChild('videoTable') videoTable: MatTable<your type>;
// rest of the component

addVideo()
{
  // add to the datasource collection
  this.videoTable.renderRows();        // call from typescript
}

// template example
// call renderRows() directly from videoTable
<button mat-button [disabled]="partNumber == null || videoUrl == null" (click)="addVideo(); videoTable.renderRows()">

请参阅 demo