【问题标题】:How to find out the index of a selected row of a ag-grid如何找出 ag-grid 选定行的索引
【发布时间】:2022-04-08 20:55:59
【问题描述】:

我遇到了一个问题,我想在指定行的 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 网格中看到索引列。但是如何获取特定选定行的索引?

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

this.gridApi.getSelectedRows();

但是我没有从哪里得到该行的索引。

请帮帮我。

【问题讨论】:

标签: angular indexing ag-grid-angular


【解决方案1】:

从我linked 的答案中,您可以在ag-grid-angular 元素和.ts 中使用(rowClicked)="onRowClick($event)"

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

检查stackblitz example

【讨论】:

    【解决方案2】:

    使用 ag-grid JS,下面的方法对我来说可以很好地获取行索引。在这里分享,以便在某些情况下可能需要某人。

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

    【讨论】:

      【解决方案3】:

      在下面使用:

      gridApi.getSelectedNodes()
      

      根据以下文档

      getSelectedRows - 返回选定行的列表(即您提供的行数据)。

      getSelectedNodes - 返回选定节点的列表。获取 底层节点(而不是数据)在使用时很有用 树/聚合数据,因为节点可以遍历。

      【讨论】:

        猜你喜欢
        • 2022-01-16
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 2018-10-27
        • 2018-03-15
        • 2019-02-10
        • 2019-08-17
        相关资源
        最近更新 更多