Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can obtain the index of a column in an Angular Material matTable by using the following code:

  1. First, import the MatHeaderCellDef and MatColumnDef from @angular/material:

    import { MatHeaderCellDef, MatColumnDef } from '@angular/material';
    
  2. 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;
    
  3. 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.