我在项目中有一个ag-grid工作。但现在,当我将相同的移植到另一个页面时,我得到了一些错误..我试图避免之间的http调用,并直接尝试通过
设置ROW数据 $scope.gridOptions.api.setRowData(RowDatas);
但错误是“TypeError:无法读取未定义的属性'setRowData'”
所以n调试我意识到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>
答案 0 :(得分:4)
这个问题似乎是由于方法被调用两次造成的。在定义api之前的第一次,使内部代码崩溃。在第二遍,定义了api。
OnGridReady对我不起作用,也没有使用vue的nextTick或超时。
最后使用if:
解决了这个问题if (this.gridOptions.api) {
this.gridOptions.api.setFilterModel(null);
}
这允许它第一次传递它而不是崩溃,然后第二次捕获并完成工作。
(希望这有助于某人。让我感到非常沮丧。)
答案 1 :(得分:1)
只有在网格初始化后,网格api才会准备就绪。您可以使用gridReady事件:
$scope.gridOptions = {
onGridReady: function() {
$scope.gridOptions.api.setRowData(...your data);
}
答案 2 :(得分:1)
试试这个:
onGridReady: function(params) {
params.api.setRowData(...your data);
}