Default sorting in Sort header
See original GitHub issueQuestion:
How can I change Angular Material code below, so that data-table is sorted by ‘name’ column, ascending order by default. Arrow (indicating current sort direction) must be displayed.
<table matSort (matSortChange)="sortData($event)">
<tr>
<th mat-sort-header="name">Dessert (100g)</th>
<th mat-sort-header="calories">Calories</th>
<th mat-sort-header="fat">Fat (g)</th>
<th mat-sort-header="carbs">Carbs (g)</th>
<th mat-sort-header="protein">Protein (g)</th>
</tr>
<tr *ngFor="let dessert of sortedData">
<td>{{dessert.name}}</td>
<td>{{dessert.calories}}</td>
<td>{{dessert.fat}}</td>
<td>{{dessert.carbs}}</td>
<td>{{dessert.protein}}</td>
</tr>
</table>
What is the expected behavior?
What is the current behavior?
I was trying something like this, to show arrow (indicating sort order) but it doesn’t work.
<table matSort (matSortChange)="sortData($event)" matSortActive="name" matSortStart="asc" matSortDisableClear>
What are the steps to reproduce?
Here’s link to Plunker StackOverflow Question
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Default sorting in Angular Material - Sort header - Stack Overflow
This is proper way to set default sorting if you know which one is that. In case that you get sorting from somewhere...
Read more >Angular Material Sort Header - Javatpoint
By default, the sort header starts sorting on asc and then desc. ... Set matSortStart to descending on the matSort directive to reverse...
Read more >Angular material table with initial sort header - Medium
Sorting with an certain item by default. As sorting header page states: When used on a mat-table header, it is not required to...
Read more >Angular Mat Sort Default - StackBlitz
Table with sorting. ... import {MatSort, MatTableDataSource, Sort}. from '@angular/material';. export interface PeriodicElement {. name: string;.
Read more >Default Sort sorting header row - Microsoft Community
Found the answer - Data tab, Sort dialog - in upper right is a checkbox My Data Has Headers. 3 people found this...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can do it like this:
You’re mistaking
matSortStart
formatSortDirection
.Try this:
https://plnkr.co/edit/sg0hC5d8LTjLKhbH9Eug?p=preview
matSortStart
can be used to reverse the cycle used when sort (e.g. when the user clicks to sort, it starts atdesc
instead ofasc
).