Angularjs - Pagination appear after search filter

2019-07-19 13:24发布

问题:

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.

回答1:

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/



回答2:

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/