.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>
Sorting is not working because my header ids don't match the properties on the array of objects i'm getting from my backend.
What I understand so far is I have to make a custom sortingDataAccessor
but have no idea how to achieve that.
Thanks in advance for the help!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…