0

I am trying to register to the scroll event (get notified when new rows appear) like this:

 $scope.$on('ngGridEventRows', function (event, rows) {
            console.log("Scroll")
  });

But it does not fire.. (angular ui-grid version: v3.0.6)

What would be the correct way to achieve it?

3 Answers 3

1

Im not sure about their native events, but you could create your own watcher for their scrolling-event.

In this Plunkr I made a watcher that broadcasts on scroll.

$scope.gridOptions.onRegisterApi = function(gridApi){
  $scope.gridApi = gridApi;
  $scope.$watch('gridApi.grid.isScrollingVertically', watchFunc);
  function watchFunc(newData) {
    if(newData === true) {
      $rootScope.$broadcast('scrolled');
    }
  }
};

And your receiver

app.controller('SecondCtrl', ['$scope', function ($scope){
  $scope.$on('scrolled', function(event, args) {
    console.log('was scrolled');
  });
}]);

Plunkr was created from their Grid Scrolling tutorial.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ! Works as expected! BTW do you know a way to get the list of rows are actually displayed (out of the total rows) in the grid ?
Cant think of an easy way right now. Maybe their API offers something like that. The problem comes with their dynamic viewport generation, the DOM changes all the time and you would need to calculate the current scrollbar position, total height of the viewport and rowheight or something like that.
It seems like they got something for that: github.com/angular-ui/ui-grid/pull/1495 ui-grid.info/docs/#/tutorial/210_selection . Maybe you can do something with selectAllVisibleRows.
Note that this will only tell you when scrolling has begun. For a mousewheel, this will mean every click. If you drag the scrollbar, it'll only trigger when you first move. If you need to know each change in scroll position, you can check scrollLeft/scrollTop, or just start/stop an interval based on the watch condition.
0

If you want more exact data about the scroll (i.e. the actual scroll event), you can instead wrap the grid in a directive. I will not include a template here, since the link config is the crucial part:

angular.module('my-app').directive('myGrid', function($timeout) {
    return {
      restrict: 'E',
      template: "<div><ui-grid></ui-grid></div>",
      link: function($scope, $element, $attributes) {
        $timeout(function() {
          $('div.ui-grid-viewport').on('scroll', function() {
            // Your event handling here
          });
        });
      }
    };
  });

1 Comment

there's no reason to be using full jquery with angular anymore. should just use the element parameter directly.
0
$scope.$watch("gridApi.grid.isScrollingHorizontally",function(){
//your code
});

1 Comment

Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.