如何在 ngGridEventScroll 上使用页面滚动?

How to use page scroll on ngGridEventScroll?

使用 ngGrid v2.X,我正在尝试开发一个网格,当页面滚动(不是网格滚动)到达底部时加载更多数据。

通过搜索类似问题,我找到了第一个问题的解决方案: ngGrid 必须有动态高度,所以我这样做了

.ngViewport{ height:auto !important; } .ngCanvas, .ngViewport, .ngRow, .ngFooterPanel, .ngTopPanel { width: 100% !important; } .ngRow { border-bottom:none !important; }

这把我带到了 我的第二个问题。 我需要通过滚动加载数据,我发现:ngGridEventScroll,但是o仅在使用ngGrid intern scroll时触发,我需要page scroll

  $scope.$on('ngGridEventScroll', function () {
        $scope.currentDataPosition++;
        $scope.setPagingData($scope.dataArray, $scope.currentDataPosition, $scope.pagingOptions.pageSize);
    });

因此,解决方案 1 打破了解决方案 2,因为 ngGridEventScroll 不再具有实习生滚动。

 $scope.setPagingData = function (data, page, pageSize) {
        cfpLoadingBar.start();
        var pagedData = data.slice((page - 1) * pageSize, page * pageSize, page * pageSize);
        $scope.totalServerItems = data.length;
        $scope.myData = pagedData;
        cfpLoadingBar.complete();
    };

 $scope.gridOptions = {
        data: 'myData',
        virtualizationThreshold: 50,
        enableRowSelection: false,
        enablePaging: false,
        enableSorting: false,
        showFooter: false,
        pagingOptions: $scope.pagingOptions,
        filterOptions: $scope.filterOptions,
    }

可以做什么?

您可以使用 ngInfiniteScroll (https://sroze.github.io/ngInfiniteScroll/index.html) 并应用以下策略。

在您的控制器中

    $scope.limit = 50; // Initial limit (make sure its enough to cover the whole page, otherwise the raiseLimit function might not be called)
    $scope.raiseLimit = function() { // function called by ngInfiniteScroll
         if ( $scope.limit < data.length) {
             $scope.limit += 10; // adjust to your need
         } else {
             $scope.dataLoaded = true;
         }
    }
    $scope.dataLoaded = false; // prevent useless call
    $scope.myData = $filter('limitTo')(data, $scope.limit); // Do not forget to inject $filter into your controller

在你的html

    <div infinite-scroll='raiseLimit()' infinite-scroll-disabled='dataLoaded'>
        <!-- put your grid with dynamic height here -->
    </div>