简体   繁体   English

Angular UI-Grid onRegisterApi未触发

[英]Angular UI-Grid onRegisterApi not firing

I've been trying for hours to get this working. 我一直在努力工作几个小时。 I load a few grids organized by data category, then I'd like to have another grid at the bottom of the page with all data category agnostic. 我加载了一些按数据类别组织的网格,然后我想在页面底部有另一个网格,所有数据类别都是不可知的。 The grids are hidden at first, so users can click on each individual category to see the respective grid, so I need to extract the api for each grid and call handleWindowResize() on them when they are clicked. 网格最初是隐藏的,因此用户可以单击每个单独的类别以查看相应的网格,因此我需要为每个网格提取api,并在单击它们时调用它们的handleWindowResize()。

I think the problem is that onRegisterApi is not executing for the allData grid. 我认为问题是onRegisterApi没有为allData网格执行。 Everything else works like a charm. 其他一切都像魅力一样。

$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);
            }
        });
    });

the console.log statement never fires (but it will if I put it in the first foreach loop). console.log语句永远不会触发(但如果我将它放在第一个foreach循环中,它将会触发)。 I started out with the loops combined and simply changing the data of the gridOptions object each time, but that didn't work. 我开始使用循环组合,每次只更改gridOptions对象的数据,但这不起作用。 I also tried moving the allData to a new grid variable with new grid options with no luck. 我还尝试使用新的网格选项将allData移动到新的网格变量,没有运气。

Why isn't onRegisterApi firing for the allData grid?? 为什么没有onRegisterApi为allData网格触发?

I had a similar issue. 我有一个类似的问题。 That was because I forgot to put 那是因为我忘记了

<div ui-grid="gridOptions" ui-grid-selection class="grid"></div>

in the .html . .html中 Turns out that the ui-grid="griOptions" fires the onRegisterApi in the .js . 原来, ui-grid="griOptions"会激活.js中onRegisterApi

onRegisterApi is called by ui-grid directive, so it's related to the rendering of the grid. onRegisterApi由ui-grid指令调用,因此它与网格的渲染有关。

Here it seems you are adding the listener when some data gets returned, and this may as well happen after the rendering is complete. 这里似乎是在返回一些数据时添加侦听器,这也可能在渲染完成后发生。 If this is the case (which is quite hard to say without a working example or a plunkr) then the listener will never get called. 如果是这种情况(没有一个工作示例或一个plunkr就很难说),那么监听器永远不会被调用。

My suggestion is to define the gridOptions inside of your controller (ie on page load) and then to use the dataFactory.getData().success promise to update only gridOptions ' data property 我的建议是在gridOptions内部定义gridOptions (即在页面加载时),然后使用dataFactory.getData().success promise来仅更新gridOptionsdata属性

Api has been alreadyd registered prior to your dataFactory request ended. 在您的dataFactory请求结束之前,Api已被alreadyd注册。

So you event was fired, but your listener was not attached yet. 所以你的事件被解雇了,但你的听众还没有被附加。

$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);

    });

Setting interval to 0 had solved my problem: 将间隔设置为0已解决了我的问题:

$interval(function (){
    $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN); }
    ,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM