I am trying to sort the table but it's not getting sort. I have found the problem for not happening. It's not happening because mat-sort-header is not matching with the model. But the problem is my JSON object is not the same as this example. Also for the name part, my data is coming from firstName, middleName, lastName and I have to show it as Name only. Let me show my JSON object and my HTML below:
JSON object received from API
[
{
"exam": {
"examCategory": {
"examCategoryName": "Freshers",
"id": 157,
"timestamp": "2020-07-07 11:55:22.425"
},
"examName": "First Demo",
"timestamp": "2020-07-07 15:17:32.0"
},
"id": 94,
"user": {
"id": 9,
"firstName": "AVISHEK",
"middleName": "DATTA",
"lastName": "RAY",
"timestamp": "2020-07-07 15:06:58.186"
},
"timestamp": "2020-07-07 13:15:24.898"
}
]
example.component.html
isplayMode="flat" class="mat-table" [class.d-none]="isLoading || length==0">
<section matSort class="mat-elevation-z2 mat-header-row">
<span class="mat-header-cell" mat-sort-header="user.id">User Id</span>
<span class="mat-header-cell" mat-sort-header="name">Name</span>
<span class="mat-header-cell" mat-sort-header="examName">Exam Name</span>
<span class="mat-header-cell" mat-sort-header="examCategoryName">Exam Category</span>
<span class="mat-header-cell" mat-sort-header="marks">Marks Obtained</span>
</section>
<mat-expansion-panel *ngFor="let result of selectedResult | async">
<mat-expansion-panel-header class="mat-row">
<span class="mat-cell">{{ result.user.id }}</span>
<span class="mat-cell">
{{ result.user.firstName | titlecase }}
{{ result.user.middleName | titlecase }}
{{ result.user.lastName | titlecase }}
</span>
<span class="mat-cell">{{ result.exam.examName }}</span>
<span class="mat-cell">{{ result.exam.examCategory.examCategoryName }}</span>
</mat-expansion-panel-header>
<div class="row">
// Any Body //
</div>
</mat-expansion-panel>
</mat-accordion>
As in TS I have declared selectedResult as selectedResult : Observable<any[]> I have given mat-sort-header names on my own.
What change do I need to make the sorting work?