Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(pagination): retain model on initialization
Browse files Browse the repository at this point in the history
- When `total-items` initializes as undefined, ignore value and do not update page information
- Remove extra watcher and call action manually

Closes #3786
Closes #4783
Fixes #2956
  • Loading branch information
wesleycho committed Oct 29, 2015
1 parent 167cfad commit 30099a0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@ angular.module('ui.bootstrap.pagination', [])
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
self.itemsPerPage = parseInt(value, 10);
$scope.totalPages = self.calculateTotalPages();
updatePage();
});
} else {
this.itemsPerPage = config.itemsPerPage;
}

$scope.$watch('totalItems', function() {
$scope.totalPages = self.calculateTotalPages();
});

$scope.$watch('totalPages', function(value) {
setNumPages($scope.$parent, value); // Readonly variable

if ($scope.page > value) {
$scope.selectPage(value);
} else {
ngModelCtrl.$render();
$scope.$watch('totalItems', function(newTotal, oldTotal) {
if (angular.isDefined(newTotal) || newTotal !== oldTotal) {
$scope.totalPages = self.calculateTotalPages();
updatePage();
}
});
};
Expand Down Expand Up @@ -71,6 +65,16 @@ angular.module('ui.bootstrap.pagination', [])
$scope.noNext = function() {
return $scope.page === $scope.totalPages;
};

function updatePage() {
setNumPages($scope.$parent, $scope.totalPages); // Readonly variable

if ($scope.page > $scope.totalPages) {
$scope.selectPage($scope.totalPages);
} else {
ngModelCtrl.$render();
}
}
}])

.constant('uibPaginationConfig', {
Expand Down Expand Up @@ -111,7 +115,7 @@ angular.module('ui.bootstrap.pagination', [])

// Setup configuration parameters
var maxSize = angular.isDefined(attrs.maxSize) ? scope.$parent.$eval(attrs.maxSize) : paginationConfig.maxSize,
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
scope.boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$parent.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
scope.directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$parent.$eval(attrs.directionLinks) : paginationConfig.directionLinks;

Expand Down
24 changes: 24 additions & 0 deletions src/pagination/test/pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,27 @@ describe('pagination directive', function() {
});
});
});

describe('pagination directive', function() {
var $compile, $rootScope, element;
beforeEach(module('ui.bootstrap.pagination'));
beforeEach(module('template/pagination/pagination.html'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it('should retain the model value when total-items starts as undefined', function() {
$rootScope.currentPage = 5;
$rootScope.total = undefined;
element = $compile('<uib-pagination total-items="total" ng-model="currentPage"></uib-pagination>')($rootScope);
$rootScope.$digest();

expect($rootScope.currentPage).toBe(5);

$rootScope.total = 100;
$rootScope.$digest();

expect($rootScope.currentPage).toBe(5);
});
});

1 comment on commit 30099a0

@Streebor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found this fix not to fully work for me so I have added changed the updatePage function from:

ctrl.updatePage = function() {
        ctrl.setNumPages($scope.$parent, $scope.totalPages); // Readonly variable

        if ($scope.page > $scope.totalPages) {
          $scope.selectPage($scope.totalPages);
        } else {
          ctrl.ngModelCtrl.$render();
        }
      };

to:

ctrl.updatePage = function() {
        ctrl.setNumPages($scope.$parent, $scope.totalPages); // Readonly variable
        //Added angular.isDefined($scope.totalItems) to fix page resets to 1 if total items is undefined.
        if (angular.isDefined($scope.totalItems) && $scope.page > $scope.totalPages) {
          $scope.selectPage($scope.totalPages);
        } else {
          ctrl.ngModelCtrl.$render();
        }
      };

I hope this will help somebody.

Please sign in to comment.