I want to show a list through Angular material design's data table mat-table. Here is my component:
import { LeaveapplicationService } from './../../services/leaveapplication.service';
import { leaveapplication } from './../../models/leaveapplication';
import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material';
// import { DataSource } from '@angular/cdk/table';
@Component({
selector: 'app-leaveapplication-list',
templateUrl: './leaveapplication-list.component.html',
styleUrls: ['./leaveapplication-list.component.scss']
})
export class LeaveapplicationListComponent implements OnInit {
leaveApplications: leaveapplication[];
displayedColumns: ['id', 'applicant', 'manager', 'leavetype', 'start', 'end', 'return', 'status'];
dataSource: MatTableDataSource<leaveapplication>;
constructor(private leaveApplicationService: LeaveapplicationService) { }
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
ngOnInit() {
this.leaveApplicationService.getLeaveApplications().subscribe(leaveapplications => {
this.leaveApplications = leaveapplications;
this.dataSource = new MatTableDataSource<leaveapplication>(this.leaveApplications);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
console.log(this.dataSource);
});
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
}
The leaveapplication interface:
export interface leaveapplication {
id: number;
applicant: string;
manager: string;
startdate: Date;
enddate: Date;
returndate: Date;
leavetype: string;
status: string;
}
I am getting the dataSource properly in the console:
But the mat-table return empty cells:
Here is my template:
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Form ID </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.id}} </td>
</ng-container>
<ng-container matColumnDef="applicant">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Applicant </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.applicant}} </td>
</ng-container>
<ng-container matColumnDef="manager">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Manager </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.manager}} </td>
</ng-container>
<ng-container matColumnDef="leavetype">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Leave Type </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.leaveType}} </td>
</ng-container>
<ng-container matColumnDef="start">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Start Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.startDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="end">
<th mat-header-cell *matHeaderCellDef mat-sort-header> End Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.endDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="return">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Return Date </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.returnDate | date:'dd-MM-yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Status </th>
<td mat-cell *matCellDef="let leaveapplication"> {{leaveapplication.status}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
Why am I not getting the mat-table populated? Is it because of the asynchronous nature of the response?