Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
Why does angular material table sort break when I change where I set this.dataSource.sort

In a related question I was told that if I wanted to sort an angular material table that was populated from an http.get() request, I would have to set the sort after the response returned (e.g. in the subscribe()). This makes sense to me.

However, after playing around with it a bit, I tried to modify my original Stackblitz to set the sort for the angular material table that was populated from inline static json in the same way, in the subscribe(). This broke the sort for this table.

Stackblitz: https://stackblitz.com/edit/angular-ivy-pvmzbs

Before:


  ngOnInit(): void {
    if (this.name === 'Inline') {
      this.api.getDataInline().subscribe((response) => {
        this.dataSource = new MatTableDataSource(response);
      });
    } else if (this.name === 'Http') {
      this.api.getDataHttp().subscribe((response) => {
        this.dataSource = new MatTableDataSource(response);
        this.dataSource.sort = this.sort;
      });
    }
  }

  ngAfterViewInit() {
    this.dataSource.sort = this.sort;
  }

After:


  ngOnInit(): void {
    if (this.name === 'Inline') {
      this.api.getDataInline().subscribe((response) => {
        this.dataSource = new MatTableDataSource(response);
        this.dataSource.sort = this.sort;
      });
    } else if (this.name === 'Http') {
      this.api.getDataHttp().subscribe((response) => {
        this.dataSource = new MatTableDataSource(response);
        this.dataSource.sort = this.sort;
      });
    }
  }

  ngAfterViewInit() {}

Why does this break the sort for the inline table?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Replace ngOnInit with ngAfterViewInit. In the OnInit call sort was not yet initialized, in AfterViewInit you are sure that elements from the template are initialized as well. And the Http table was working because matSort was not yet initialized during the first subscription, but then on the second probably it already was.

over 4 years ago · Santiago Trujillo Report

0

As mentioned in other answer, the static data arrives before the view has been rendered, thus it is undefined. You can provide { static: true } in ViewChild. You need to set that for being able to run your code in OnInit.

@ViewChild(MatSort, { static: true }) sort!: MatSort;

Your forked STACKBLITZ

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!