.ts
this.displayedColumns = [
{ key: 'id', header: '#' },
{ key: 'fullname', header: 'Full name' },
{ key: 'email', header: 'email' },
{ key: 'roleName', header: 'Role' }
];
this.displayedColumnsKeys = this.displayedColumns.map(col => col.key);
.html
<table mat-table [dataSource]="dataSource" multiTemplateDataRows matSort class="mat-elevation-z8">
<ng-container *ngFor="let dispCol of displayedColumns; let colIndex = index" matColumnDef="{{dispCol.key}}">
<th mat-header-cell *matHeaderCellDef mat-sort-header> {{dispCol.header}} </th>
<td mat-cell *matCellDef="let element"> {{element[dispCol.key]}} </td>
</ng-container>
<!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div class="example-element-detail"
[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
<div class="example-element-diagram">
<div class="example-element-position"> {{element.position}} </div>
<div class="example-element-symbol"> {{element.symbol}} </div>
<div class="example-element-name"> {{element.name}} </div>
<div class="example-element-weight"> {{element.weight}} </div>
</div>
<div class="example-element-description">
{{element.description}}
<span class="example-element-description-attribution"> -- Wikipedia </span>
</div>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumnsKeys"></tr>
<tr mat-row *matRowDef="let element; columns: displayedColumnsKeys;"
class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
<tr class="mat-row" *matNoDataRow>
<td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
</tr>
</table>
It seems like the sorting functionality is not working properly due to mismatching header ids and properties in the array of objects fetched from the backend.
I've realized that I need to create a custom sortingDataAccessor
function, but I'm unsure about how to go about implementing it. Any guidance on this matter would be greatly appreciated!