I've read through I don't know how many examples and to my knowledge I'm following the example correctly but the columns do not update when I click on a header.
-I have included MatSortModule in my app.module.ts
-I have included matSort on the mat-table
-I have included mat-sort-header on header-cells of the columns I want to be able to sort by (all of them)
-I have included @ViewChild(MatSort) sort: MatSort; and the corresponding
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
I don't see what else is left. Here is my code snippet:
<mat-table #table [dataSource]="dataSource" matSort class="dataTable">
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell
*matHeaderCellDef
fxLayout="row"
fxLayoutAlign="start center"
mat-sort-header
class="clickable"
>
Product Name
</mat-header-cell>
<mat-cell
*matCellDef="let item"
class="table-cell-content"
>
{{item.name}}
</mat-cell>
</ng-container>
...
defaultData: Array<Object> = null;
dataSource = new MatTableDataSource(this.defaultData);
@ViewChild(MatSort) sort: MatSort;
constructor(private getDataService: GetDataService) {
this.getDataService.getData()
.subscribe(data => {
this.defaultData = data;
this.dataSource.data = this.defaultData;
},
err => console.log(err));
}
...
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}