1 | initial version |
You can obtain the index of a column in an Angular Material matTable by using the following code:
First, import the MatHeaderCellDef and MatColumnDef from @angular/material:
import { MatHeaderCellDef, MatColumnDef } from '@angular/material';
In your component, use the @ViewChild decorator to get a reference to the MatHeaderCellDef and MatColumnDef elements:
@ViewChild(MatHeaderCellDef) headerCellDef: MatHeaderCellDef;
@ViewChild(MatColumnDef) columnDef: MatColumnDef;
Use the headerCellDef's columnDef property to get the index of the column:
const index = this.headerCellDef.columnDef.index;
This returns a number representing the index of the column.