I am using a filter on an array of items in a ng-repeat like this:
<input type="text" ng-model="filterText">
<li ng-repeat="item in displayItems | filter : {item_name: filterText}>
This works fine and filters the items perfectly.The problem arises because I am using displayItems with a uib-pagination element. The pagination uses totalItems instead of displayItems.
<uib-pagination class="pagination-sm pagination"
total-items="totalItems.length" ng-model="page" ng-change="changeItems()">
</uib-pagination>
Obviously I need to somehow apply the filter on the total-items, then create the displayItems, and put the filteredItems in the uib-pagination.
Ie. When the person types, in the controller the filter is applied to the totalItems, and I create the filteredItems needed for the pagination. I also then create the displayItems from the filteredItems. How do I go about putting this into the controller and not in the HTML for the ng-repeat?
I'm not sure how to do this. Any thoughts? All help much appreciated....
I've included a plunker to show it (not) working in all its glory.... https://plnkr.co/edit/EYX6zO1795sD9TYq2J8U?p=preview
Try,
HTML,
<li ng-repeat="item in filterData = (totalItems | filter : {itemName: filterText}) | limitTo:3:3*(page-1)">
and
<uib-pagination class="pagination-sm pagination" total-items="filterData.length" ng-model="page"
ng-change="pageChanged()" previous-text="‹" next-text="›" items-per-page=3></uib-pagination>
JavaScript,
$scope.pageChanged = function() {
//var startPos = ($scope.page - 1) * 3;
//$scope.displayItems = $scope.totalItems.slice(startPos, startPos + 3);
};
https://plnkr.co/edit/m3jXsxsCGfqKCJYHsw0u?p=preview
You can access the filtered array by using as alias
in ng-repeat
expression
<li ng-repeat="item in displayItems | filter : {item_name: filterText} as filteredArray">
Then use that alias for total-items
in pagination
<uib-pagination class="pagination-sm pagination"
total-items="filteredArray.length" ng-model="page" ng-change="changeItems()">
</uib-pagination>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With