Skip to content

Instantly share code, notes, and snippets.

@qazimobeen
Created May 14, 2018 10:41
Show Gist options
  • Save qazimobeen/50cfc2742dd6c50619328ee491eeedcc to your computer and use it in GitHub Desktop.
Save qazimobeen/50cfc2742dd6c50619328ee491eeedcc to your computer and use it in GitHub Desktop.
$scope.originalItems = [];
$scope.pageSizeOptions = [
{ count: '10', value: '10' },
{ count: '20', value: '20'},
{ count: '50', value: '50' }
];
$scope.pageSize = $scope.pageSizeOptions[1]; // default page size as per AC
$scope.$watch('items', function (newValue, oldValue) {
if (newValue !== oldValue) {
if ($scope.items.length > 0 && $scope.originalItems.length === 0) {
$scope.originalItems = $scope.items;
$scope.currentPage = 1; // default page is first page
$scope.lastPage = parseInt(Math.ceil($scope.originalItems.length / $scope.pageSize.value)); // calculates the last page
$scope.items = $scope.originalItems.slice(0, $scope.pageSize.value);
}
else if ($scope.items.length > 0 && $scope.originalItems.length > 0 && $scope.items.length === $scope.originalItems.length) {
$scope.originalItems = [];
$scope.originalItems = $scope.items;
$scope.lastPage = parseInt(Math.ceil($scope.originalItems.length / $scope.pageSize.value)); // calculates the last page
var begin = (($scope.currentPage - 1) * $scope.pageSize.value);
var end = begin + parseInt($scope.pageSize.value);
if (parseInt(end) > $scope.originalItems.length) {
// set the last item index to length of array
end = $scope.originalItems.length;
}
$scope.items = $scope.originalItems.slice(begin, end);
}
}
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment