Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing definitions for header, footer, and row; cannot determine which columns should be rendered - Angular Material Table

I'm using Angular 9.0.4 with @angular/material 9.1.3 and I want to show a basic material table:

<table mat-table [dataSource]="devizas" class="mat-elevation-z8">
  <!-- name -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef>Name</th>
    <td mat-cell *matCellDef="let element"> {{ element.name }} </td>
  </ng-container>

  <!-- code -->
  <ng-container matColumnDef="code">
    <th mat-header-cell *matHeaderCellDef>Code</th>
    <td mat-cell *matCellDef="let element"> {{ element.code }} </td>
  </ng-container>
</table>

I imported in my app.module.ts this:

import { MatTableModule } from '@angular/material/table';

@NgModule({
  imports: [
    MatTableModule,
  ],
})

In my component.ts:

export class DevizaListComponent implements OnInit {
  devizas: DevizaInterface[] = [
    {
      name: 'dollar',
      code: 'USD' ,
    }
  ];

  constructor() { }

  ngOnInit() { }
}

Now I get this error message in the browser:

Error: Missing definitions for header, footer, and row; cannot determine which columns should be rendered. at getTableMissingRowDefsError (table.js:996) at MatTable.push../node_modules/@angular/cdk/ivy_ngcc/fesm5/table.js.CdkTable.ngAfterContentChecked (table.js:1296)

The official documentation says nothing relevant. Any idea?

like image 782
netdjw Avatar asked Mar 27 '20 16:03

netdjw


People also ask

What is a Cdk data table?

The CdkTable is an unopinionated, customizable data-table with a fully-templated API, dynamic columns, and an accessible DOM structure. This component acts as the core upon which anyone can build their own tailored data-table experience.

Can we use mat table without DataSource?

In fact, it is possible to create a material table without a datasource. You need to make sure you made the header definitions with displayed columns and all.

What is DataSource in mat table?

A DataSource is simply a class that has at a minimum the following methods: connect and disconnect . The connect method will be called by the table to provide an Observable that emits the data array that should be rendered.

What is data source angular?

The DataSource is meant to serve a place to encapsulate any sorting, filtering, pagination, and data retrieval logic specific to the application. So, a datasource therefore contains all logic required for sorting, filtering and paginating data.


2 Answers

Try this!

<table mat-table [dataSource]="devizas" class="mat-elevation-z8">
 ....

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

like image 55
vargaadam Avatar answered Sep 24 '22 20:09

vargaadam


You are missing below line in html code :

<tr mat-header-row *matHeaderRowDef="columndefs"></tr>
  <tr mat-row *matRowDef="let row; columns: columndefs;"></tr>
  1. create an array with column definitions as below in component.ts file :

    columndefs : any[] = ['name','code'];

  2. Update table code as below :

<table mat-table [dataSource]="devizas" class="mat-elevation-z8">
          <ng-container matColumnDef="name">
            <th mat-header-cell *matHeaderCellDef>Name</th>
            <td mat-cell *matCellDef="let element"> {{ element.name }} </td>
          </ng-container>
        
          <ng-container matColumnDef="code">
            <th mat-header-cell *matHeaderCellDef>Code</th>
            <td mat-cell *matCellDef="let element"> {{ element.code }} </td>
          </ng-container>
        
          <tr mat-header-row *matHeaderRowDef="columndefs"></tr>
          <tr mat-row *matRowDef="let row; columns: columndefs;"></tr>
        
        </table>

Here is the demo : demo

like image 45
programoholic Avatar answered Sep 21 '22 20:09

programoholic