Angular Mat Table Error: Missing definitions for header and row

女生的网名这么多〃 提交于 2020-01-21 10:36:38

问题


I am using MatTable from Angular Material 5.2.5. I trying to populate the table with data which is got through a server request.

The UI

  <mat-table [dataSource]="dataSource">
    <ng-container matColumnDef="number">
      <mat-header-cell *matHeaderCellDef>#</mat-header-cell>
      <mat-cell *matCellDef="let i = index">{{i + 1}}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
      <mat-cell *matCellDef="let user">{{user.userName}}</mat-cell>
    </ng-container>
    ...
  </mat-table>

The component code

  public dataSource = new MatTableDataSource<User>()
  public displayedColumns = ['number', 'name', 'email', 'phone', 'joiningDate']

Adding data to dataSource on receiving data from the server

 onGettingPartnerUsers(userList: User[]) {
  console.log('userList', userList)
  if (!Util.isFilledArray(userList)) {
    // TODO show message no user found
  } else {
    this.dataSource.data = userList
  }
 }

I am getting the list of users. But this error is getting thrown.

Error: Missing definitions for header and row, cannot determine which columns should be rendered.

What is the mistake?

PS: I am following this blog on using MatTable


回答1:


You must add mat-row and mat-header-row(optional) tags inside your table:

<mat-table>
  .... columns definitions

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

See more examples here https://material.angular.io/components/table/overview



来源:https://stackoverflow.com/questions/49026943/angular-mat-table-error-missing-definitions-for-header-and-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!