Javascript 注册表上的角度UI网格API未触发
我已经试了好几个小时来让它工作。我加载一些按数据类别组织的网格,然后我希望在页面底部有另一个网格,所有数据类别都不可知。网格首先是隐藏的,因此用户可以单击每个单独的类别来查看相应的网格,因此我需要提取每个网格的api,并在单击它们时调用handleIndowreSize() 我认为问题在于onRegisterApi没有针对allData网格执行。其他一切都很有魅力Javascript 注册表上的角度UI网格API未触发,javascript,angularjs,ng-grid,angular-ui-grid,Javascript,Angularjs,Ng Grid,Angular Ui Grid,我已经试了好几个小时来让它工作。我加载一些按数据类别组织的网格,然后我希望在页面底部有另一个网格,所有数据类别都不可知。网格首先是隐藏的,因此用户可以单击每个单独的类别来查看相应的网格,因此我需要提取每个网格的api,并在单击它们时调用handleIndowreSize() 我认为问题在于onRegisterApi没有针对allData网格执行。其他一切都很有魅力 $scope.globalColumnDefs = [//column definitions here]; $scope.data
$scope.globalColumnDefs = [//column definitions here];
$scope.dataList = {};
$scope.allData = [];
$scope.gridOptions = [];
$scope.gridApis = [];
dataFactory.getData().success(function (data) {
$scope.dataList = data.dataCategories;
//set up each grid for data categories
angular.forEach($scope.dataList, function (value) {
$scope.allData = $scope.allData.concat(value.items);
$scope.gridOptions.push({
data: value.items,
minRowsToShow: 25,
showColumnFooter: true,
enableFiltering: true,
columnDefs: $scope.globalColumnDefs,
onRegisterApi: function (api) {
$scope.gridApis.push(api);
}
});
});
//set up the allData grid
$scope.gridOptions.push({
data: $scope.allData,
minRowsToShow: 25,
showColumnFooter: true,
enableFiltering: true,
columnDefs: $scope.globalColumnDefs,
onRegisterApi: function (api) {
console.log("Regiestering API");
$scope.gridApis.push(api);
}
});
});
console.log语句从不激发(但如果我将其放入第一个foreach循环中,就会激发)。我开始时将循环组合在一起,每次只更改gridOptions对象的数据,但这不起作用。我还尝试将allData移动到具有新网格选项的新网格变量,但没有成功
为什么不为allData网格启动onRegisterApi???将interval设置为0解决了我的问题:
$interval(function (){
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN); }
,0);
ui-grid指令调用了onRegisterApi,因此它与网格的呈现相关 在这里,您似乎是在返回某些数据时添加侦听器,这也可能在渲染完成后发生。如果是这种情况(如果没有一个有效的示例或plunkr,很难说),那么侦听器将永远不会被调用
我的建议是在控制器内部(即在页面加载时)定义
gridOptions
,然后使用dataFactory.getData()。success
承诺只更新gridOptions
,数据
属性在dataFactory请求结束之前已经注册了Api
所以您的事件被触发,但您的侦听器尚未连接。我遇到了类似的问题。那是因为我忘了把
$scope.globalColumnDefs = [//column definitions here];
$scope.dataList = {};
$scope.allData = [];
$scope.gridOptions = [];
$scope.gridOption = {
minRowsToShow: 25,
showColumnFooter: true,
enableFiltering: true,
columnDefs: $scope.globalColumnDefs,
onRegisterApi: function (api) {
alert('now it work')
$scope.gridApis.push(api);
}
};
$scope.gridApis = [];
dataFactory.getData().success(function (data) {
$scope.dataList = data.dataCategories;
//set up each grid for data categories
angular.forEach($scope.dataList, function (value) {
$scope.allData = $scope.allData.concat(value.items);
$scope.gridOption.data = value.items;
$scope.gridOptions.push($scope.gridOption);
});
//set up the allData grid
$scope.gridOption.data = $scope.allData;
$scope.gridOptions.push($scope.gridOption);
});
<div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
在.html文件中。结果是,
ui grid=“griOptions”
在.js中触发了onRegisterApi
。有没有可能创建一个plunker?OP是问如何让onRegisterApi触发,在这个地方可以获得gridApi的引用,所以它显然与您的回答无关。@imbalind onRegisterApi在UI上呈现数据之前不会激发。。OP提到网格是隐藏的。。设置interval是唯一一个解决方案,正如前面提到的,这个问题似乎已经解决了:正如您所看到的,不再使用$interval(尽管依赖项仍然存在),并且我多次使用隐藏网格,而无需在任何地方使用您建议的解决方案。我认为我们最好等到OP发布一个plunker,看看是什么导致了这一点。请添加关于更改内容和原因的评论。