Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

342
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda