How to do paging in AngularJS?

后端 未结 21 1900
轮回少年
轮回少年 2020-11-22 07:17

I have a dataset of about 1000 items in-memory and am attempting to create a pager for this dataset, but I\'m not sure on how to do this.

I\'m using a custom filter

21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:57

    I've extracted the relevant bits here. This is a 'no frills' tabular pager, so sorting or filtering is not included. Feel free to change/add as needed:

         //your data source may be different. the following line is 
         //just for demonstration purposes only
        var modelData = [{
          text: 'Test1'
        }, {
          text: 'Test2'
        }, {
          text: 'Test3'
        }];
    
        (function(util) {
    
          util.PAGE_SIZE = 10;
    
          util.range = function(start, end) {
            var rng = [];
    
            if (!end) {
              end = start;
              start = 0;
            }
    
            for (var i = start; i < end; i++)
              rng.push(i);
    
            return rng;
          };
    
          util.Pager = function(data) {
            var self = this,
              _size = util.PAGE_SIZE;;
    
            self.current = 0;
    
            self.content = function(index) {
              var start = index * self.size,
                end = (index * self.size + self.size) > data.length ? data.length : (index * self.size + self.size);
    
              return data.slice(start, end);
            };
    
            self.next = function() {
              if (!self.canPage('Next')) return;
              self.current++;
            };
    
            self.prev = function() {
              if (!self.canPage('Prev')) return;
              self.current--;
            };
    
            self.canPage = function(dir) {
              if (dir === 'Next') return self.current < self.count - 1;
              if (dir === 'Prev') return self.current > 0;
              return false;
            };
    
            self.list = function() {
              var start, end;
              start = self.current < 5 ? 0 : self.current - 5;
              end = self.count - self.current < 5 ? self.count : self.current + 5;
              return Util.range(start, end);
            };
    
            Object.defineProperty(self, 'size', {
              configurable: false,
              enumerable: false,
              get: function() {
                return _size;
              },
              set: function(val) {
                _size = val || _size;
              }
            });
    
            Object.defineProperty(self, 'count', {
              configurable: false,
              enumerable: false,
              get: function() {
                return Math.ceil(data.length / self.size);
              }
            });
          };
    
        })(window.Util = window.Util || {});
    
        (function(ns) {
          ns.SampleController = function($scope, $window) {
            $scope.ModelData = modelData;
            //instantiate pager with array (i.e. our model)
            $scope.pages = new $window.Util.Pager($scope.ModelData);
          };
        })(window.Controllers = window.Controllers || {});
    
    
    Col1
    « {{n + 1}} »

提交回复
热议问题