Angular UI-Grid onRegisterApi未触发

时间:2015-07-28 17:56:42

标签: javascript angularjs ng-grid angular-ui-grid

我已经花了好几个小时才开始工作。我加载了一些按数据类别组织的网格,然后我想在页面底部有另一个网格,所有数据类别都不可知。网格最初是隐藏的,因此用户可以单击每个单独的类别以查看相应的网格,因此我需要为每个网格提取api,并在单击它们时调用它们上的handleWindowResize()。

我认为问题是onRegisterApi没有为allData网格执行。其他一切都像魅力一样。

$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移动到新的网格变量,但没有运气。

为什么没有ongisterApi为allData网格触发?

5 个答案:

答案 0 :(得分:1)

我有类似的问题。那是因为我忘记了

<div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
.html 中的

。事实证明ui-grid="griOptions"会触发 .js 中的onRegisterApi

答案 1 :(得分:0)

onRegisterApi由ui-grid指令调用,因此它与网格的渲染有关。

这里似乎是在返回一些数据时添加侦听器,这也可能在渲染完成后发生。如果是这种情况(没有一个工作示例或一个plunkr就很难说),那么监听器永远不会被调用。

我的建议是在控制器内部定义gridOptions(即在页面加载时),然后使用dataFactory.getData().success承诺仅更新gridOptions'data属性

答案 2 :(得分:0)

在您的dataFactory请求结束之前,已经注册了Api。

所以你的事件被解雇了,但你的听众还没有被附加。

答案 3 :(得分:0)

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

    });

答案 4 :(得分:-1)

将间隔设置为0已解决了我的问题:

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