In my Angular application, I have a material table with multiple columns that I am sorting using matSort. While I can successfully sort the last two columns in ascending or descending order, I am facing an issue with the first column which contains date values. The problem is that I can only sort the date column once and subsequent attempts to sort it again do not work.
I attempted to override the sortingDataAccessor property on MatTableDataSource but it did not resolve the issue:
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'day': return new Date(item.day);
default: return item[property];
}
};
this.dataSource.sort = this.sort;
}
Here is the section of code in my trips.component.html file where the date (day) column is defined within the material table:
<div class="table-responsive">
<table class="table table-bordered>
<mat-table [dataSource]="dataSource" matSort>
<!-- Day column -->
<ng-container matColumnDef="day">
<mat-header-cell *matHeaderCellDef mat-sort-header>Day</mat-header-cell>
<mat-cell *matCellDef="let trip">{{ trip.start | date: 'dd/MM/yyyy' }}</mat-cell>
</ng-container>
This snippet reflects the structure in my trips.component.ts file:
import { Component, OnInit, ViewChild } from '@angular/core';
import { Trip } from 'src/app/models/Trip';
import { MatSort, MatSortable, MatTableDataSource } from '@angular/material';
import { DataSource } from '@angular/cdk/collections';
@Component({
selector: 'app-trips',
templateUrl: './trips.component.html',
styleUrls: ['./trips.component.css']
})
export class TripsComponent implements OnInit {
@ViewChild(MatSort, {static:false}) sort: MatSort;
private trips: Trip[];
displayedColumns = ['day', 'time', 'duration', 'from', 'to', 'mileage', 'cost'];
dataSource;
constructor() {
this.trips = [new Trip("12/11/2019 02:30:15", "12/11/2019 06:04:43", "Landsmeer", "Amsterdam-West", 1.97, 1.06), new Trip("0 8/29/2019 16:10:14", "08/29/2019 18:19:54", "Amsterdam-West", "Vlaggemast", 14.74, 9.20),new Trip("08/16/2019 01:56:42", "08/16/2019 03:23:26", "Amsterdam-Zuid", "Amsterdam-Noord", 7.88, 7.61), new Trip("03/25/2019 04:01:27", "03/25/2019 09:58:27", "NDSM-Plein", "Amsterdam-Zuid", 5.49, 5.01), new Trip("12/23/2018 14:27:06", "12/23/2018 18:47:34", "Westpoort", "Schakelstraat", 2.00, 1.98), new Trip("12/10/2018 12:26:56", "12/23/2018 14:39:29", "Ijdok", "Amsterdam-Oost", 3.46, 2.47), new Trip("07/03/2018 11:20:39", "03/07/2018 12:37:51", "Amsterdam-Noord", "Huidekoperstraat", 7.66, 5.22), new Trip("05/19/2018 22:41:39", "05/20/2018 03:29:20", "Amsterdam Nieuw-West", "Amsterdam-Centrum", 13.32, 6.93), new Trip("04/21/2018 08:37:04", "04/21/2018 15:24:38", "Buiksloterweg", "Amsterdam-Noord", 13.72, 13.13)
]
}
ngAfterViewInit(){
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'day': return new Date(item.day);
default: return item[property];
}
};
this.dataSource.sort = this.sort;
}
ngOnInit() {
this.dataSource = new MatTableDataSource(this.trips);
}
}
Any assistance on resolving this sorting issue would be greatly appreciated.