ag-grid - AgGrid 中的行数

标签 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 AgGrid:
getDisplayedRowCount()  Returns the total number of displayed rows.

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

最佳答案

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

gridReady: ag-Grid has initialised. Use this event if, for example, you need to use the grid's API to fix the columns to size. 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 有一个注释:

Sometimes the gridReady grid event can fire before the Angular component is ready to receive it, so in an Angular environment its safer to rely on AfterViewInit instead before using the API.



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

关于ag-grid - AgGrid 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128503/

相关文章:

angular - 具有简单类型行数据的 ag-grid

html - 在 Ag Grid 中动态更改或设置单元格类别

javascript - cypress 中 .click() 函数的行为不一致

reactjs - 如何在 react 中实现 AG 网格行内的按钮

javascript - 在 Internet Explorer 的 ag-grid 中移动行选择选择额外的文本

ag-grid 列菜单重置列事件?

angular - 类型 'getFrameworkComponentInstance' 上不存在属性 'ICellRendererComp'

angular - 行的 Ag-Grid DOM

typescript - 无法使用比较器自定义排序我的 ag-grid

vuejs2 - 如何在 Nuxt 应用程序中使用 ag-grid