Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda