5

I'm trying to integrate Angular's material table in a new project but for some reason the data is not displayed, only the empty rows. I used the same logic in another project and there it works, I spent a lot of time to find the reason but nothing works.

I need to add the table in a lazy loaded module called "cores" and I imported the material table module:

import { MatTableModule } from '@angular/material';
imports: [
    CommonModule,
    CoresRoutingModule,
    MatTableModule
  ]

I created a services which returns an array of "ICore" objects:

getCores(): Observable<ICore[]> {
    return this.http.get<ICore[]>('/cores');
  }

The ICore interface:

export interface ICore {
  id: number;
  name: string;
}

The HTML from my "index.component.html" file:

<mat-table [dataSource]="dataSource">
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.id }}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.name }}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

The logic from "index.components.ts" file:

dataSource = new MatTableDataSource<ICore>();
columnsToDisplay = ['id', 'name'];

constructor(private coreService: CoreService) { }

  ngOnInit() {
    this.coreService.getCores().subscribe(
      res => {
        this.dataSource.data = res;
        console.log(this.dataSource);
      },
      err => console.log(err)
    );
  }

From my console.log I see that the dataSource is populated:

MatTableDataSource {_renderData: BehaviorSubject, _filter: BehaviorSubject, _internalPageChanges: Subject, _renderChangesSubscription: Subscriber, sortingDataAccessor: ƒ, …}
data: Array(3)
0: {id: 1, name: "core1"}
1: {id: 2, name: "core2"}
2: {id: 3, name: "core3"}

I also tried this method:

dataSource = new CoreDataSource(this.coreService);
columnsToDisplay = ['id', 'name'];

constructor(private coreService: CoreService) { }

export class CoreDataSource extends DataSource<any> {   
    constructor(private coreService: CoreService) {
        super();
    }

  connect(): Observable<ICore[]> {
    console.log(this.coreService.getCores());
    return this.coreService.getCores();   }

  disconnect() {} }

But with the same result. I feel that is something stupid that I forget to do but I don't find it.

Thanks in advance!

4
  • 2
    In your template you use displayedColumns for the columns but in your component the variable is called columnsToDisplay. Is that a typo in your question or an actual error in the code? Commented May 6, 2019 at 9:23
  • OMG. I'm so embarrassed, I checked everything but not this variable name. It solved the problem. thank you!. You can add an answer and I will mark it as solved. Commented May 6, 2019 at 10:00
  • Happens to the best of us, sometimes all you just need is just another pair of eyes to look over your code. Commented May 6, 2019 at 10:03
  • @FabianKüng, You are right. Thanks again. Commented May 6, 2019 at 10:04

2 Answers 2

6

Make sure you use the same variable to display the columns that you defined in your component.

Component:

columnsToDisplay = ['id', 'name'];

Template:

<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
<mat-row *matRowDef="let row; columns: columnsToDisplay;"></mat-row>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks man, I'm using angular 5. Documentation says <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr> <tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>. It was showing nothing
0

Since your data source is in a sub variable data it wont render this way. Try the following code

<mat-table [dataSource]="dataSource.data">
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.id }}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.name }}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

3 Comments

Same result... it this would be the problem it would with the second solution that I tried.. :(
Have you tries this.dataSource = res;
Yes but in this case an error is displayed: Type 'ICore[]' is missing the following properties from type 'MatTableDataSource<ICore>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.