Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the column index in ag-grid?

In ag-grid, when I want to retrieve the row index I use:

params.node.id

However, I couldn't find a way to do the same for columns. All that I found is retrieve the columnId which refers to the field variable in the column definition.
I.e: If this is the column definition:

 {
   headerName: "checkButton 2",
   field: "checkbuttonTwo",
   cellRenderer: "checkButtonComponent",
   width: 150
 }

This:

params.column.getId()  

Would return:

 checkbuttonTwo

So my question is:
Is there any way to retrieve the column index? For example: For the second column I would have 2 and so on and so forth.
Thank you

like image 398
Ahmed Ghrib Avatar asked May 25 '19 13:05

Ahmed Ghrib


2 Answers

I wrote a helper function in TypeScript for getting the column index by col id.

function getColumnIndexByColId(api: ColumnApi, colId: string): number {
    return api.getAllColumns().findIndex(col => col.getColId() === colId);
}
like image 140
webpreneur Avatar answered Oct 28 '22 21:10

webpreneur


There is no direct way to get the column index, however you can use the getAllColumns() method on Column API to get all columns and then find the index of your column from that array.

More info on Column API here

like image 29
asimhashmi Avatar answered Oct 28 '22 20:10

asimhashmi