Angularjs - Pagination appear after search filter

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 03:22:34

问题


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/



来源:https://stackoverflow.com/questions/25524713/angularjs-pagination-appear-after-search-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!