AgGrid中的行数

时间:2018-05-02 06:38:46

标签: ag-grid

我想在我的AgGrid中显示行数,但是我遇到了问题。

    ngOnInit() { 
      console.log(this.gridOptions.api.getDisplayedRowCount());
    }

错误:

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)

Api the AgGrid:

getDisplayedRowCount()  Returns the total number of displayed rows.

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

1 个答案:

答案 0 :(得分:2)

(你提到ngOnInit所以我假设你正在使用Angular)
问题是api在ngOnInit中不可用。如果需要grid api,您应该听gridReady事件。

  

gridReady: ag-Grid已初始化。例如,如果您需要使用网格的API将列修复为大小,请使用此事件。   Documentation

HTML:

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

组件ts:

onGridReady(params: any) {
  console.log('grid ready');
  console.log(this.gridOptions.api.getDisplayedRowCount());
}

您还应该记住,this site上有一条说明:

  

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

尽管我在使用gridReady事件时从未遇到任何问题。

相关问题