简体   繁体   中英

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.
Ie: 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

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

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);
}

You can get the column headerName by running this function:

function CustomTooltip() {}

CustomTooltip.prototype.init = function(params) {
  var headerName = params.colDef.headerName;
}

If you use GridApi to retrieve columnDefs like this:

api.getColumnDefs();

returned array has the same order as displayed data on the table.

For each column, You can define a unique id this way:

{
          headerName: "Axa premises",
          field: "axPr",
          width: 150,
          id:0,
          cellRenderer:'iasaCheckboxRendererComponent'
            }

And you can access it in this manner:

this.params.column.colDef.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM