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

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

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