0
votes

I have stuck in one problem, i want to add new row to the ag grid for specified row(i.e i just want to add a new row just below the selected row in a ag grid)

As a first step i am trying to get the index number of a selected row. But unable to get the index of a selected row.

Below is the code that i have tried to print index in ag grid.

In columnDefs:

public columnDefs = [
      {headerName: 'index', valueGetter: (args) => this._getIndexValue(args), field: 'index',editable: true, width:100}]

 _getIndexValue(args: ValueGetterParams): any {
     return args.node.rowIndex;
  }

But this code working as expected and i am able to see the index column in ag grid.But how do i get the index for particular selected row?

I am using below code to get the selected row.

this.gridApi.getSelectedRows();

But no where i am getting the index of that row.

Please help me in this.

2
I am able to get the data of the selected Row using gridApi.getSelectedRows() method but i am unable to find out the index there. - Akshay Krishna

2 Answers

3
votes

From the answer I linked, you can use (rowClicked)="onRowClick($event)" on the ag-grid-angular element and in your .ts have:

onRowClick(event: any): void {
    console.log(event.rowIndex);
}

Check stackblitz example.

0
votes

Using ag-grid JS, below method works fine for me to get the row index. Sharing here so that it might be needful to someone in some cases.

var rowIndex = $($(this).closest('.ag-row')[0]).attr('row-index');