I'm trying to sort the MatTableDataSource programmatically so that I can sort the data via the use of a button rather than by the table column header when viewing the data in a specific mobile layout. However, I'm having trouble doing so.
I've followed this post's advice on how to do it but the data sort is not being reflected. The mobile layout design for using the same data as the table:
<mat-card matSort *ngFor="let item of dataSource.filteredData">
The function I'm using to try and sort the data is:
sortDataSource(id: string, start: string){
this.dataSource.sort.sort(<MatSortable>({id: id, start: start}));
}
which is called from tapping onto a button within a mat-menu e.g.
<mat-menu #sortMenu="matMenu" yPosition="below" [overlapTrigger]="false">
<button mat-menu-item (click)="sortDataSource('createdDate', 'asc')"><span>Creation Date</span><mat-icon>arrow_upward</mat-icon></button>
Any help on this would be greatly appreciated!
Edit: Adding where I'm assigning the data to the table's dataSource which comes from an Observable array:
this.data$.subscribe(data => {
this.dataSource.data = data;
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
Edit 2: Removed error image as fixed by add "matSort" to the the mat-card
using *ngFor
for creating the mobile layout version of the mat-table
.
this.dataSource
? You are not actually sorting anything but rather setting a sorting id and direction. – Julien Ambos