I have a mat-table with customer info. I would like to sort the rows by column value. There is no error but the sorting is not working.
The table tag has matSort
:
<mat-table [dataSource]="dataSource" matSort>
Each column definition has mat-sort-header
:
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
<mat-cell *matCellDef="let customer"> {{customer.name}} </mat-cell>
</ng-container>
In the component class I have
...
@ViewChild(MatSort) sort: MatSort;
...
ngOnInit() {
this.dataSource = new MatTableDataSource(this.data as any);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
So everything is pretty much like in the documentation which states
If you are using the
MatTableDataSource
for your table's data source, provide theMatSort
directive to the data source and it will automatically listen for sorting changes and change the order of data rendered by the table.
but neither the styling nor the functionality for sorting is being applied to the table.
This question is not relevant because the problem is present even with my data hardcoded in the class.
I don't see what I am missing. The root of the problem is evading me. You can see my full code and run the example on Stackblitz