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

500
Vistas
Angular Material table column

How can I get in a table made in Angular material the index of a clicked column?

I use <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true; let i = index;" (click)="clickEventT(i)"></tr>

but something missing.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The <tr ...></tr> tag is actually the header row and not the header cell. Now the index of each header cell within the table depends on the displayedColumns ordering, depending on the use case it might be dynamically changing or not. An hardcoded solution could be perfectly valid as in many cases re-ordering columns wont happen.

<table mat-table [dataSource]="dataSource">
  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef (click)="showIndex('weight')"> Weight </th>
    <td mat-cell *matCellDef="let element" (click)="showIndex('weight')"> {{element.weight}} </td>
  </ng-container>
  <!-- ... -->
  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

In scenarios where the displayed columns change The showIndex(columnName: string) function can display/return the column index as follows

displayedColumns: string[] = ['weight','...'];
showIndex(columnName: string): void {
   const index = this.displayedColumns.indexOf(columnName);
   console.log(index);
   alert(index);
}
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