I'm trying to implement pagination in my angular app. I have an array of 1000+ books, and I'd like my currentPage / totalPages
vars to update based on my filter
ed ngRepeat
.
Here's my current Angular code:
function Books($scope, $http, $rootScope){
$scope.books = [1000+ books];
$scope.filteredbooks = [];
$scope.numberOfPages = function(){
return Math.ceil($scope.filteredbooks.length / $scope.pageSize);
}
}
My next / prev buttons:
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
Previous
</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= data.length/pageSize - 1"
ng-click="currentPage=currentPage+1">
Next
</button>
Here's my ngRepeat:
<tr ng-repeat="book in (filteredbooks = books | filter: search) |
orderBy: 'title' | startFrom: currentPage * pageSize |
limitTo: pageSize">
{{book.title}}
</tr>
My problem is that filteredbooks
in my ngRepeat
isn't setting my $scope.filteredbooks
, so my length for total pages isn't updating. Is there a way to do this?
Also is there an event or directive that I can hook into to set my $scope.currentPage
to 0 when the search term is changed?
Thanks in advance!
Edit: Here's a fiddle of the go-to example with a search box: http://jsfiddle.net/4LRbN/