Ask Your Question
4

How can I obtain the index of a column in an Angular Material matTable?

asked 2023-05-14 12:42:51 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-14 12:57:01 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-14 12:42:51 +0000

Seen: 13 times

Last updated: May 14 '23