Reputation: 937
I am using angular material table to display data. Currently, i want to load angular material table sorted from status. I have 3 status - complete, pending & fail. I want fail record to show last and complete and pending records should display first.
I used below sorting function to do it, but its not working.
this.dataSource = new MatTableDataSource(results['InvoiceHeaders']);
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'status': return item.element.status;
default: return item[property];
// default: return item[property];
}
};
this.dataSource.sort = this.viewsort;
Can u help me to do these type of sample.
Thanks
Upvotes: 0
Views: 1638
Reputation: 15041
One solution could be to create a field 'statusWeight' and assign a sortable numeric value to the 'status' column, and then use the default material sorting on that arbitary column.
relevant HTML:
<table mat-table [dataSource]="dataSource" matSort matSortActive="statusWeight" matSortDirection='asc' class="mat-elevation-z8">
....
</table>
working stackblitz available here
Upvotes: 1