Madhes
Madhes

Reputation: 33

Angularjs - Pagination appear after search filter

I am newbie on AngularJS.

Search result can be displayed in one page but why the next and previous button is showing ?

http://jsfiddle.net/2ZzZB/1473/

 <input type="text" id="txtNotessearch" ng-model="search_notes" class="form-control input-sm" placeholder="SEARCH">
 ...
    <ul>
        <li ng-repeat="item in data | filter:search_notes | startFrom:currentPage*pageSize | limitTo:pageSize">
                    {{item}}
        </li>
    </ul>

Correct me, if I am wrong.

Upvotes: 2

Views: 4434

Answers (2)

Mario Levrero
Mario Levrero

Reputation: 3367

Because NumberOfPages is not filtered. Instead of using $scope.data.length, you should use the length after the filter.

function MyCtrl($scope, $filter) { //Do not forget to inject $filter
    $scope.currentPage = 0;
    $scope.pageSize = 10;
    $scope.data = [];

    $scope.numberOfPages=function(){
        var myFilteredData = $filter('filter')($scope.data,$scope.search_notes); //Filter the data
        return Math.ceil(myFilteredData.length/$scope.pageSize);                
    }
    for (var i=0; i<45; i++) {
        $scope.data.push("Item "+i);
    }
} 

In addition, I would modify the ng-disable next button to

button "ng-disabled="(currentPage + 1) == numberOfPages()"

And I would add to search_notes onchange currentPage=1.

Here you have the fiddle http://jsfiddle.net/rLots5zd/

Upvotes: 2

khemry
khemry

Reputation: 585

In case, you want to hide the next and previous button when the result can be displayed in one page, please see the fiddle here: http://jsfiddle.net/rLots5zd/3/

Upvotes: 0

Related Questions