Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matt Slawson
11,406 PointsAngularJS Filter Issue
I'm working on implementing pagination on a filtered list. I'd like that pagination to be updated based on the query or the category selected. I'm sure there is likely a much cleaner way to implement this using filters, can anyone help me out?
Here is the part from my view where this lives:
product.html
<tbody>
<tr ng-repeat="product in filteredProducts | orderBy: 'Name' | filter:currentCategory() | filter:query" >
<td> </td>
<td>{{product.Name}}</td>
<!-- using if to not display $0 if no value -->
<td ng-if="product.Threehour">{{product.Threehour | currency}}</td>
<td ng-if="!product.Threehour"> </td>
<td>{{product.Daily | currency}}</td>
<!-- using if to not display $0 if no value -->
<td ng-if="product.Weekly">{{product.Weekly | currency}}</td>
<td ng-if="!product.Weekly"> </td>
<pagination total-items="totalItems" items-per-page="itemsPerPage" ng-model="currentPage" ng-change="pageChanged()"></pagination>
</tr>
<tr>
Items per page: {{itemsPerPage}}
Page Count: {{pageCount()}}
Total Items: {{}}
</tr>
</tbody>
Here's the controller:
main.js
app.controller('MainCtrl', function ($scope, $location, productsBase, servCategory, servEquipment, productsFactory, filterFilter, $anchorScroll ) {
$scope.query = servEquipment.getEquip();
$scope.setCurrentCategory = function(cat) {
$scope.newCat = servCategory.setName(cat);
};
$scope.currentCategory = servCategory.getName;
$scope.oneAtATime = true;
// Pagination Logic
$scope.products = productsFactory.query();
$scope.itemsPerPage = 30;
$scope.currentPage = 1;
$scope.pageCount = function(){
return Math.ceil($scope.products.length / $scope.itemsPerPage);
};
$scope.products.$promise.then(function () {
$scope.totalItems = $scope.products.length;
$scope.$watch('currentPage + itemsPerPage', function(term) {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;
$scope.filtered = filterFilter($scope.products, term);
$scope.filteredProducts = $scope.products.slice(begin, end);
});
});
Finally, here are my services (for reference):
app.js
app.service('servCategory', function servCategory(){
var catName = "";
this.getName = function(){
return catName;
};
this.setName = function (name){
catName = name;
return catName;
};
});
app.service('servEquipment', function servEquipment(){
var equipName = "";
this.getEquip = function(){
return equipName;
};
this.setEquip = function(name){
equipName = name;
return equipName;
};
});
1 Answer
Steve Smith
12,956 PointsI'm not sure if you made any headway on this, but I've been having a lot of success with using the ui-bootstrap project with Angular - http://angular-ui.github.io/bootstrap/
There's a specific set of elements specifically to handle pagination, and you might find that useful. The documentation and examples aren't bad, and there's a lot of help available on Stackoverflow if you get stuck implementing something.