【问题标题】:Row Count in AgGridAgGrid 中的行数
【发布时间】:2018-10-12 04:49:33
【问题描述】:

我想在我的 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 AgGrid:

getDisplayedRowCount()  Returns the total number of displayed rows.

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

【问题讨论】:

    标签: ag-grid


    【解决方案1】:

    (您提到了 ngOnInit,所以我假设您使用的是 Angular)
    问题是 api 在ngOnInit 中不可用。如果您需要网格 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 事件时从未遇到任何问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-15
      • 2022-11-18
      • 2023-01-19
      • 2020-03-12
      • 2020-03-13
      • 1970-01-01
      • 2019-01-30
      • 2020-09-30
      相关资源
      最近更新 更多