如何找出农业网格选定行的索引

How to find out the index of a selected row of a ag-grid

我遇到了一个问题,我想为指定行向 ag 网格添加新行(即我只想在 ag 网格中所选行的正下方添加新行)

作为第一步,我试图获取选定行的索引号。 但是无法获取所选行的索引。

下面是我尝试在 ag 网格中打印索引的代码。

在 columnDefs 中:

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

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

但此代码按预期工作,我能够在 ag 中看到索引列 grid.But 我如何获取特定选定行的索引?

我正在使用下面的代码来获取选定的行。

this.gridApi.getSelectedRows();

但是我不知道从哪里获取该行的索引。

请帮帮我。

根据我 的回答,您可以在 ag-grid-angular 元素上使用 (rowClicked)="onRowClick($event)",并且在您的 .ts 中有:

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

检查stackblitz example

使用 ag-grid JS,下面的方法很适合我获取行索引。在这里分享,以便在某些情况下可能对某人有帮助。

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

使用如下:

gridApi.getSelectedNodes()

根据以下文档

getSelectedRows - Returns a list of selected rows (i.e. row data that you provided).

getSelectedNodes - Returns a list of selected nodes. Getting the underlying node (rather than the data) is useful when working with tree / aggregated data, as the node can be traversed.