简体   繁体   English

angularjs ui-grid滚动条到达终点时如何获取事件

[英]How can I get an event when angularjs ui-grid scroll bar reaches the end

I want to display a button when the scroll bar reaches the end of the result in angular ui grid v3. 我想在角度ui网格v3中滚动条到达结果结尾时显示一个按钮。

What I was thinking was to attach an event on scroll using directive, but ui grid is a directive itself and I cannot get its scroll length and height because the data is not loaded at that moment. 我当时的想法是使用指令在滚动上附加一个事件,但是ui grid本身就是一个指令,我无法获得其滚动长度和高度,因为此时未加载数据。

If anyone can give me the hint about how this problem can be solved, it would me much appreciated. 如果有人能给我有关如何解决此问题的提示,我将不胜感激。 Thanks 谢谢

This is not the complete solution but it might get you started. 这不是完整的解决方案,但可能会让您入门。

I attached to scrollEnd event and was able to check for the y-position. 我附加到scrollEnd事件,并能够检查y位置。 I did not find a way yet to calculate the bottom of the viewport, thats why I just put a arbitrary 2000 value there. 我还没有找到一种方法来计算视口的底部,这就是为什么我只在其中放置了任意2000值的原因。

You might check scrollToIfNecessary in https://github.com/angular-ui/ui-grid/blob/a42dab24532fb896725b9fc8359e4526e436c891/src/js/core/factories/Grid.js to calculate the real height of the grid. 您可以在https://github.com/angular-ui/ui-grid/blob/a42dab24532fb896725b9fc8359e4526e436c891/src/js/core/factories/Grid.js中检查scrollToIfNecessary以计算网格的实际高度。 It is definitely no easy calculation. 绝对不容易计算。

http://plnkr.co/edit/hluhWCZz05WB0Itdumbf?p=preview http://plnkr.co/edit/hluhWCZz05WB0Itdumbf?p=preview

   gridApi.core.on.scrollEnd($scope,function(row){

        if( row.newScrollTop > 2000) alert('far down south');

      });

The following lets you detect if the user has scrolled all the way to the right and/or bottom of the grid. 以下内容使您可以检测用户是否一直滚动到网格的右侧和/或底部。 Just insert the necessary handlers. 只需插入必要的处理程序即可。 You achieve it by wrapping it in a directive, and then tapping into the native scroll events. 您可以通过将其包装在指令中,然后利用本机滚动事件来实现它。

Please note that I am not a JQuery expert, so there most certainly is a better way than repeating the selector. 请注意,我不是JQuery专家,因此肯定有比重复选择器更好的方法。

angular.module('myGridApp')
  .directive('myGrid', function($timeout) {
    return {
      restrict: 'E',
      scope: { data: '=' },
      template: "<div><ui-grid></ui-grid></div>",
      link: function($scope, $element, $attributes) {

        $timeout(function() {
          var maxHorizontalScroll = $('div.ui-grid-viewport')[0].scrollWidth - $('div.ui-grid-viewport')[0].clientWidth;
          var maxVerticalScroll = $('div.ui-grid-viewport')[0].scrollHeight - $('div.ui-grid-viewport')[0].clientHeight;
          $('div.ui-grid-viewport').on('scroll', function(par, arg) {
            var currentHorizontalScroll = $('div.ui-grid-viewport').scrollLeft();
            var currentVerticalScroll = $('div.ui-grid-viewport').scrollTop();

            if(currentHorizontalScroll >= maxHorizontalScroll) {
              // User has scrolled as far to the right as possible
            }

            if(currentVerticalScroll >= maxVerticalScroll) {
              // User has scrolled as far to the bottom as possible
            }
          });
        });
      }
    };
  });

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

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