【发布时间】:2020-06-24 04:29:53
【问题描述】:
这是 .html 文件 -
<ag-grid-angular
#agGrid
style="width: 100%; height: 100%; "
class="ag-theme-alpine"
[sideBar]="sidebar"
[ngClass]="theme"
[rowData]="rowData"
[columnDefs]="columnDefs"
[domLayout]="domLayout"
rowSelection="multiple"
[gridOptions]="gridOptions"
(gridReady)="OnGridReady($event)"
>
我在 ngOnInit() 函数中将数据插入到 rowData 中,如下所示 -
ngOnInit()
{
fetch('http://dummy.restapiexample.com/api/v1/employees')
.then(res => res.json())
.then((out) => {
this.rowData = out.data;
}).catch(err => console.error(err));
}
在 OnGridReady() 函数中,初始化 gridApi 和 ColumnApi 后,我使用 this.gridApi.foreachnode 遍历所有 rowData 并在控制台中打印它们,如下所示 -
OnGridReady(params)
{
this.gridApi = params.api;
this.columnApi = params.columnApi;
console.log("printing data ->")
this.gridApi.forEachNode((node, index)=>
{
console.log(node.data.id + '-' + node.data.employee_name);
console.log();
});
}
但控制台中没有显示任何内容。此外,没有错误或任何东西。我哪里错了?在 foreachnode() 中使用箭头函数是否存在范围问题?
另外,如果我使用一个按钮来调用一个使用 gridapi 来迭代数据的函数,它就可以工作。这是为什么呢?
【问题讨论】:
-
尝试将一些消息放在 gridApi.forEachNode 循环之外。 OnGridReady 在它获取数据之前被调用,所以没有什么要记录的。
-
这是一个时间问题,ngOnInit 是一个角度钩子,而 OnGridReady 是一个网格生命周期钩子。初始化网格时,不会从服务器获取您的数据。您可以在 onGridReady 中调用 fetch 并在 .then() 中执行 api.forEachNode
-
@user3551808 你能具体说明如何做到这一点。我想在数据加载后显示数据。
-
@PratikBhat 仍然没有输出。