AgGrid中的行数

Edu*_*uBw 2 ag-grid

我想在AgGrid中显示行数,但是出现问题。

    ngOnInit() { 
      console.log(this.gridOptions.api.getDisplayedRowCount());
    }
Run Code Online (Sandbox Code Playgroud)

错误:

ERROR TypeError: Cannot read property 'getDisplayedRowCount' of undefined
    at ProjectListComponent.webpackJsonp../src/app/project-list/project-list.component.ts.ProjectListComponent.ngOnInit (project-list.component.ts:178)
    at checkAndUpdateDirectiveInline (core.js:12411)
    at checkAndUpdateNodeInline (core.js:13935)
    at checkAndUpdateNode (core.js:13878)
    at debugCheckAndUpdateNode (core.js:14771)
    at debugCheckDirectivesFn (core.js:14712)
    at Object.eval [as updateDirectives] (ProjectListComponent_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:14697)
    at checkAndUpdateView (core.js:13844)
    at callViewAction (core.js:14195)
Run Code Online (Sandbox Code Playgroud)

Api AgGrid:

getDisplayedRowCount()  Returns the total number of displayed rows.
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会有这个问题...

Fab*_*ian 6

(您提到ngOnInit,所以我假设您使用的是Angular)
问题是api在中不可用ngOnInitgridReady如果需要网格API,则应监听事件。

gridReady: ag-Grid已初始化。例如,如果您需要使用网格的API将列固定为大小,请使用此事件。 文献资料

的HTML:

<ag-grid-angular   style="width: 100%; height: 372px;"
  (gridReady)="onGridReady()">
</ag-grid-angular>
Run Code Online (Sandbox Code Playgroud)

组件ts:

onGridReady(params: any) {
  console.log('grid ready');
  console.log(this.gridOptions.api.getDisplayedRowCount());
}
Run Code Online (Sandbox Code Playgroud)

您还应该记住,此网站上有一条注释:

有时,gridReadyGrid事件可以在Angular组件准备好接收它之前触发,因此在Angular环境中,使用它更安全地依赖于AfterViewInit使用API​​。

尽管如此,使用该gridReady事件我从未遇到任何问题。