How to do client-side pagination with ngGrid?

风流意气都作罢 提交于 2019-12-04 03:22:51

问题


If you set the enablePaging options of an ng-grid to true, you enable server-side pagination.

What about client-side one? I could not find any hint on this in the documentation, but I can not imagine that ng-grid does not support client-side paging as well.

Any hint?


回答1:


I think the example given on the angular page (http://angular-ui.github.io/ng-grid/) actually shows an example of client-side paging. If you look at the data load that is being called by the sample script (http://angular-ui.github.io/ng-grid/jsonFiles/largeLoad.json), you'll see that its not actually doing any server-side paging... it's coming down as one big file.




回答2:


It might help you!!

The AngularJs code-sample

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope, $http) {
  $scope.filterOptions = {
    filterText: ""
  };

  $scope.pagingOptions = {
    pageSizes: [25, 50, 100],
    pageSize: 25,
    totalServerItems: 0,
    currentPage: 1
  };

  $scope.setPagingData = function(data, page, pageSize) {
    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
    $scope.myData = pagedData;
    $scope.pagingOptions.totalServerItems = data.length;
    if (!$scope.$$phase) {
      $scope.$apply();
    }
  };

  $scope.getPagedDataAsync = function(pageSize, page) {
    setTimeout(function() {      
        $http.get('json').success(function(largeLoad) {
          $scope.setPagingData(largeLoad, page, pageSize);
        });
    }, 100);
  };

  $scope.$watch('pagingOptions', function() {
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
  }, true);

  $scope.gridOptions = {
    data: 'myData',
    enablePaging: true,
    pagingOptions: $scope.pagingOptions,
    showFooter: true
  };

  $scope.gridOptions.columnDefs = 'gridColumnDefs';

  // city loc pop state
  $scope.gridColumnDefs = [{
      field: "city"
    },
    {
      field: "state"
    }, {
      field: "pop"
    }, {
      field: "loc"
    }
  ];

});

The HTML code-sample

   <div ng-app="myApp" ng-controller="MyCtrl">
        <div class="gridStyle" ng-grid="gridOptions"></div>
    </div> 


来源:https://stackoverflow.com/questions/20261365/how-to-do-client-side-pagination-with-nggrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!