0

Hy, I'm working on a web-based application in angular 5. In this app, we use the angular material. On one of the pages we have a mat-table but we can't get it to work. The table never shows us any data. The data we call from multiple backends and with a debugger, we know the data isn't null. Even if we try to use the angular material example it still doesn't work. What do we wrong?

HTML

  <mat-table #invoiceTable [dataSource]="datasource" matSort>
    <ng-container matColumnDef="SerialNr">
      <th mat-header-cell *matHeaderCellDef> SerialNr. </th>
      <td mat-cell *matCellDef="let movement" > {{movement?.serialNr}} </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;">
    </tr>
  </mat-table>

TS

displayedColumns = ['SerialNr'];
movements = [];
// datasource = new MatTableDataSource<Movement>(this.movements);
datasource: MovementDataSource;

  getMovementsByVehicleID() {
 this.authUser.ownedVehicles.forEach((vehicle) => {
    this.movementService.getMovementsByCarID(vehicle.id).subscribe(
      (newMovemnts) => {
        this.movements = newMovemnts;
        this.datasource.update(newMovemnts);
      },
    );
  }
);
}




export class MovementDataSource extends DataSource<Movement> {

constructor(private movements: Movement[]) {
  super();
}

connect(): Observable<Movement[]> {
  return Observable.of(this.movements);
}

update(movements: Movement[]) {
  movements.forEach((move) => {
    this.movements.push(move);
  });
}

   disconnect(collectionViewer: CollectionViewer): void {
   }
}

MATERIAL.TS

@NgModule({
  imports: [
    MatButtonModule,
    MatToolbarModule,
    MatGridListModule,
    MatIconModule,
    MatInputModule,
    MatMenuModule,
    MatPaginatorModule,
    MatSidenavModule,
    MatSnackBarModule,
    MatCardModule,
    MatTableModule,
    MatSortModule,
    MatProgressSpinnerModule,
    CdkTableModule,
  ],
  exports: [
    MatButtonModule,
    MatToolbarModule,
    MatGridListModule,
    MatIconModule,
    MatInputModule,
    MatMenuModule,
    MatPaginatorModule,
    MatSidenavModule,
    MatSnackBarModule,
    MatCardModule,
    MatTableModule,
    MatSortModule,
    MatProgressSpinnerModule,
    CdkTableModule
  ],
  declarations: []
})
export class MaterialModule {
}

We have tried a lot of things. this is the latest version but it still doesn't work. Off course we added the material module in the imports of the app.module.ts. The strange thing is even the examples doesn't work. Any idea's?

I have updated my question. It still isn't working

Ties Theunissen
  • 136
  • 2
  • 16

2 Answers2

0

By the documentation Table | Angular Material,

  1. <th mat-header-cell *matHeaderCellDef> SerialNr. </th> needs to be part of the ng-container as per the docuentation. Tried without it and the table does not render. In you case this is missing.
  2. displayedColumns needs to be an array of strings which contains the columns you need to be displayed. I see that missing too in your component.ts. In your case it should be displayedColumns = ['SerialNr'];

Rest all looks fine.

Have forked an existing example on stackblitz

Ravi Sankar Rao
  • 1,050
  • 11
  • 26
0

I had the same problem and forgot to call renderRows() method when the async data was updated. See Angular 5 material table documentation https://v5.material.angular.io/components/table/overview.

This answer gave me the inspiration: Calling renderRows() on Angular Material Table

Aspix
  • 41
  • 1
  • 6