question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default sorting in Sort header

See original GitHub issue

Question:

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?

image

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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

34reactions
shalugincommented, Oct 17, 2017

You can do it like this:


  @ViewChild(MatSort) sort: MatSort;

  ngOnInit(): void {
    this.sort.sort(<MatSortable>{
        id: 'id',
        start: 'desc'
      }
    );
  }

33reactions
andrewseguincommented, Oct 17, 2017

You’re mistaking matSortStart for matSortDirection.

Try this:

<table matSort (matSortChange)="sortData($event)" matSortActive="name" matSortDirection="asc" matSortDisableClear>

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 at desc instead of asc).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found