javascript 如何在 AngularJS ui-grid 中更改网格时收到通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30492159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:16:03  来源:igfitidea点击:

How to get notified when grid changed in AngularJS ui-grid?

javascriptangularjsangular-ui-grid

提问by Tony

If there is a way in ui-grid that I can know a grid is finish updating the rows?(like a filter is being applied etc)? I want to run some function after the grid view changes.

如果 ui-grid 中有一种方法可以让我知道网格正在更新行?(比如正在应用过滤器等)?我想在网格视图更改后运行一些函数。

I tried the following method:

我尝试了以下方法:

$scope.filteredRows = $scope.gridApi.core.getVisibleRows($scope.gridApi.grid);

$scope.$watch('filteredRows', function(){console.log('view updated');});

The above approach works when the grid just finish initiating, after that, it won't work anymore.

当网格刚刚完成启动时,上述方法有效,之后它将不再起作用。

I also tried using the filterChangedapi:

我也尝试使用filterChangedapi:

$scope.gridApi.core.on.filterChanged($scope, function() {
    console.log('filter changed');
    foo();
});

The problem with this method is that although I can get notified when the filter is changed, but if the grid is very large, it needs some time to finish updating the view, and before that, the function foo()is being called before the grid update is finished.

这种方法的问题是,虽然可以在过滤器改变时得到通知,但是如果网格很大,则需要一些时间来完成视图的更新,而在此之前,该函数foo()是在网格更新之前被调用的完成的。

Any idea will be appreciated.

任何想法将不胜感激。

回答by PaulL

I've seen use of $scope.grid.api.core.on.rowsRendered( $scope, $scope.col.updateAggregationValue );in ui-grid-footer-cell.js. I'm not sure exactly when rowsRendered fires, but given it's being used to calculate aggregations and aggregations require knowledge whenever the rows are changed, and must run after the rowsProcessors finish running, there's a good chance that it's what you want.

$scope.grid.api.core.on.rowsRendered( $scope, $scope.col.updateAggregationValue );在 ui-grid-footer-cell.js 中看到过使用。我不确定 rowRendered 何时触发,但鉴于它被用于计算聚合,并且聚合需要在行更改时获得知识,并且必须在 rowsProcessors 完成运行后运行,很有可能它就是您想要的。

EDIT: the framework to use it would be:

编辑:使用它的框架是:

  1. Define a function that you want to call when the visible rows have changed

    var myFunction = function() { do some stuff };

  2. Set this function to be called whenever rows are rendered

    $scope.gridApi.core.on.rowsRendered( $scope, myFunction );

  1. 定义一个在可见行发生变化时要调用的函数

    var myFunction = function() { do some stuff };

  2. 将此函数设置为在呈现行时调用

    $scope.gridApi.core.on.rowsRendered( $scope, myFunction );

回答by Tony

Well, I found a workaround, in order to call the function after the grid is updated, which takes some time, I added a delay in filterChangedevent:

好吧,我找到了一个解决方法,为了在网格更新后调用该函数,这需要一些时间,我在filterChanged事件中添加了一个延迟:

$scope.gridApi.core.on.filterChanged($scope, function() {
    console.log('filter changed');
    $timeout(foo(),800);
});

To use the $timeout, you need to add that to your controller first.

要使用$timeout,您需要先将其添加到控制器中。