ag-grid 无法读取 属性 'setRowData' 因为 gridOptions.api 未定义

ag-grid Cannot read property 'setRowData' because gridOptions.api undefined

我有一个 ag-grid 在项目中工作。但是现在当我把它移植到另一个页面时我得到了一些错误..我试图避免之间的 http 调用并直接尝试通过

设置 ROW 数据
 $scope.gridOptions.api.setRowData(RowDatas);

但错误是"TypeError: Cannot read property 'setRowData' of undefined"

所以在调试时我意识到 api 是未定义的。这是我的完整代码。请检查我错过了什么..

<head>
<script src="js/angular_1_3_8.js"></script>
<script src="js/angular-filter.js"></script>
<script src="workbench/agGrid/dist/ag-grid.js?ignore=notused34"></script>
<script>
    agGrid.initialiseAgGridWithAngular1(angular);
    var app = angular.module("workbenchApp", ['angular.filter',"agGrid"]);
    app.controller("workBenchCtrl", function ($scope, $http, $filter) {

    var columnDefs = [
    {headerName: "Name", field: "Name"},
    {headerName: "Cr", field: "dc"},
    {headerName: "Ac", field: "da"},
    {headerName: "Mo", field: "dm"},

];

var RowDatas=[{"Name": "SUHDLOG.DAT", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "2002-12-27 09:51:22"},
{ "Name": "BOOTLOG.PRV", "dc": "1970-01-01 05:30:00", "da": "2005-04-01 00:00:00", "dm": "2005-04-01 15:13:30"},
{ "Name": "FRUNLOG.TXT", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "2002-12-27 09:52:56"},
{ "Name": "COMMAND.COM", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "1999-04-23 22:22:00"},
{ "Name": "BOOTLOG.TXT", "dc": "1970-01-01 05:30:00", "da": "2005-04-02 00:00:00", "dm": "2005-04-02 14:38:00"},
{ "Name": "DETLOG.TXT", "dc": "1970-01-01 05:30:00", "da": "2002-12-28 00:00:00", "dm": "2002-12-28 09:56:02"},
{ "Name": "CONFIG.SYS", "dc": "1970-01-01 05:30:00", "da": "2005-06-16 00:00:00", "dm": "2003-07-03 18:39:50"},
{ "Name": "DBLSPACE.BIN", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "1999-04-23 22:22:00"},
{ "Name": "MSDOS.SYS", "dc": "1970-01-01 05:30:00", "da": "2003-07-03 00:00:00", "dm": "2002-12-27 10:01:58"},
{ "Name": "DRVSPACE.BIN", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "1999-04-23 22:22:00"},
{ "Name": "MSDOS.---", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "2002-12-27 09:46:28"},
{ "Name": "SETUPLOG.TXT", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "2002-12-27 10:04:12"},
{ "Name": "WSOCK32.DLL", "dc": "1970-01-01 05:30:00", "da": "2005-06-16 00:00:00", "dm": "2002-12-27 09:47:10"},
{ "Name": "CFGWIZ.DLL", "dc": "1970-01-01 05:30:00", "da": "2005-02-26 00:00:00", "dm": "2002-12-27 09:47:12"},
];

$scope.gridOptions = {
        angularCompileHeaders: true,
        columnDefs: columnDefs,
        rowData:[{"Name": "SUHDLOG.DAT", "dc": "1970-01-01 05:30:00", "da": "2002-12-27 00:00:00", "dm": "2002-12-27 09:51:22"}]
                };

    /*
    $http.get('FileList.json').success(function (response) {
    $scope.TData = response;
    DateArray=$scope.TData.Files;
    $scope.gridOptions.api.setRowData(DateArray);


    });
    */
    $scope.gridOptions.api.setRowData(RowDatas);

    });
</script>
</head>
<body>
<div ng-app="workbenchApp">
    <div ng-controller="workBenchCtrl">
        <div ag-grid="gridOptions" class="ag-blue" style="height: 500px;"></div>
    </div>
</div>
</body>

网格 api 只有在网格初始化后才会准备就绪。您可以为此使用 gridReady 事件:

$scope.gridOptions = {
    onGridReady: function() {
        $scope.gridOptions.api.setRowData(...your data);
    }

试试这个:

 onGridReady: function(params) {
            params.api.setRowData(...your data);
        }

有同样的问题,似乎是方法被调用两次引起的。第一次定义api之前,使得内部代码会崩溃。在第二遍中,定义了 api。

OnGridReady 对我不起作用,使用 vue 的 nextTick 或超时也不起作用。

最终使用 if:

解决了它
if (this.gridOptions.api) {
    this.gridOptions.api.setFilterModel(null);
}

这允许它第一次通过它而不是崩溃,然后第二次抓住它并完成工作。

(希望这对某人有所帮助。给我带来了很大的挫败感。)

如果正在使用的 ag 网格行模型的类型不是 client-side,则删除网格上的 rowData 属性。这样你就不会得到错误并且 params.api 不会在 onGridReady.

上被定义

setRowData 仅在客户端行模型上定义。

getRows 函数将处理在 table.

中设置服务器端数据

服务器端行模型参考(示例)-

https://www.ag-grid.com/javascript-data-grid/server-side-operations-nodejs/