I am trying to add a matSort to this already working and existing MatTable. I've tried to implement what I found on this other thread here mat-sort not working on mat-table, as it looks like describing the same issue, but can't manage to make it work. I believe one of the reasons might be that my dataSource is asynchronously calculated in the template rather than in the TS file I reckon. Just for clarity, I had previously changed the way it was retrieving the dataSource, I moved it to the TS file, but still the UI wasn't updating.
Here a simplified version of the code:
TEMPLATE:
<table matSort mat-table [dataSource]="(searchMode === 'shift') ? shifts : (searchMode === 'booking') ? bookings">
<ng-container matColumnDef="orgName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Organisation </th>
<td mat-cell *matCellDef="let element"> {{element.orgName}} </td>
</ng-container>
<ng-container matColumnDef="workerName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Worker </th>
<td mat-cell *matCellDef="let element"> {{element.workerName}} </td>
</ng-container>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20, 50, 100]" [pageSize]="20" [length]="total" (page)="onPageChange()" showFirstLastButtons></mat-paginator>
TS file:
import {MatTable, MatTableDataSource} from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
export class TsGenerationSearchTableComponent implements OnInit, OnChanges, AfterViewInit {
dataSource = new MatTableDataSource<Shift>();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
selectedWeekend: string;
selectedWeekStart: string;
ngOnInit() {
this.dataSource.paginator = this.paginator;
}
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
onPageChange() {
const data: GetShiftTimesheetData = {
action: (this.searchMode === 'shift') ? 'PageGetShiftsForTsAction' : (this.searchMode === 'booking') ? 'PageGetBookingsForTsAction',
weekStartDate: this.selectedWeekStart,
weekEndDate: this.selectedWeekend,
paginator: {
pageSize: this.paginator.pageSize,
pageIndex: this.paginator.pageIndex
}
};
this.search.emit(data);
}
The sort does absolutely nothing visible in the UI. I've tried to subscribe in ngOnChanges
to see if it was at least seen, like this:
ngOnChanges(changes: SimpleChanges): void {
if (this.dataSource && this.paginator && this.sort) {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.sort.sortChange.subscribe(val => {
console.log(val);
});
}
}
and the log shows that it's at least reacting to the sortChange
:
{active: "workerName", direction: "asc"}
and
{active: "workerName", direction: "desc"}
but nothing in the UI. I've tried doing this.dataSource.sort = val;
in the subscription but throws the error ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
Any help/tip is welcome. Thanks a lot guys.