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

时间:2015-07-17 13:38:44

标签: javascript jquery angularjs ng-grid infinite-scroll

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

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

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

带我去我的第二个问题。我需要通过滚动加载数据,我发现: ngGridEventScroll ,但是o nly触发器当使用ngGrid实习生滚动时,我需要页面滚动

  $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,
    }

可以做些什么?

1 个答案:

答案 0 :(得分:0)

您可以使用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>