Can't bind to 'dataSource' since it isn't a known property of 'table'

前端 未结 10 2080
南方客
南方客 2020-12-03 00:32

I am new in angular 5 development. I am trying to develop a data table with angular material using the example provided here: \"https://material.angular.io/components/table/

相关标签:
10条回答
  • 2020-12-03 00:42

    The problem is your angular material version, I have the same, and I have resolved this when I have installed the good version of angular material in local.

    Hope it solve yours too.

    0 讨论(0)
  • 2020-12-03 00:48

    I was also breaking my head for a long time with this error message and later I identified that I was using [datasource] instead of [dataSource].

    0 讨论(0)
  • 2020-12-03 00:50

    Thanx to @Jota.Toledo, I got the solution for my table creation. Please find the working code below:

    component.html

    <mat-table #table [dataSource]="dataSource" matSort>
      <ng-container matColumnDef="{{column.id}}" *ngFor="let column of columnNames">
        <mat-header-cell *matHeaderCellDef mat-sort-header> {{column.value}}</mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element[column.id]}}</mat-cell>
      </ng-container>
    
      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>
    

    component.ts

    import { Component, OnInit, ViewChild } from '@angular/core';
    import { MatTableDataSource, MatSort } from '@angular/material';
    import { DataSource } from '@angular/cdk/table';
    
    @Component({
      selector: 'app-m',
      templateUrl: './m.component.html',
      styleUrls: ['./m.component.css'],
    })
    export class MComponent implements OnInit {
    
      dataSource;
      displayedColumns = [];
      @ViewChild(MatSort) sort: MatSort;
    
      /**
       * Pre-defined columns list for user table
       */
      columnNames = [{
        id: 'position',
        value: 'No.',
    
      }, {
        id: 'name',
        value: 'Name',
      },
        {
          id: 'weight',
          value: 'Weight',
        },
        {
          id: 'symbol',
          value: 'Symbol',
        }];
    
      ngOnInit() {
        this.displayedColumns = this.columnNames.map(x => x.id);
        this.createTable();
      }
    
      createTable() {
        let tableArr: Element[] = [{ position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
          { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
          { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
          { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
          { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
          { position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
        ];
        this.dataSource = new MatTableDataSource(tableArr);
        this.dataSource.sort = this.sort;
      }
    }
    
    export interface Element {
      position: number,
      name: string,
      weight: number,
      symbol: string
    }
    
    

    app.module.ts

    imports: [
      MatSortModule,
      MatTableModule,
    ],
    
    0 讨论(0)
  • 2020-12-03 00:51

    Remember to add MatTableModule in your app.module's imports i.e.

    In Angular 9+

    import { MatTableModule } from '@angular/material/table'  
    
    @NgModule({
      imports: [
        // ...
        MatTableModule
        // ...
      ]
    })
    

    Less than Angular 9

    import { MatTableModule } from '@angular/material'  
    
    @NgModule({
      imports: [
        // ...
        MatTableModule
        // ...
      ]
    })
    
    0 讨论(0)
  • 2020-12-03 00:51

    Material example is using the wrong table tags. Change

    <table mat-table></table>
    <th mat-header-cell></th>
    <td mat-cell></td>
    
    <tr mat-header-row></tr>
    <tr mat-row></tr>
    

    to

    <mat-table></mat-table>
    <mat-header-cell></mat-header-cell>
    <mat-cell></mat-cell>
    
    <mat-header-row></<mat-header-row>
    <mat-row></<mat-row>
    
    0 讨论(0)
  • 2020-12-03 00:53

    Remember to import the MatTableModule module and remove the table element show below for reference.
    wrong implementation
    <table mat-table [dataSource]=”myDataArray”> ... </table>
    correct implementation:
    <mat-table [dataSource]="myDataArray"> </mat-table>

    0 讨论(0)
提交回复
热议问题