Javascript 使用angularjs分页,在所有页面中显示相同的数据
我在angularjs应用程序中的实时搜索功能有问题: 我的工厂已正确检索数据,并显示第一页,但当我单击“下一步”或“上一步”时,过滤器似乎不起作用,数据是相同的 我的ctrlJavascript 使用angularjs分页,在所有页面中显示相同的数据,javascript,angularjs,pagination,Javascript,Angularjs,Pagination,我在angularjs应用程序中的实时搜索功能有问题: 我的工厂已正确检索数据,并显示第一页,但当我单击“下一步”或“上一步”时,过滤器似乎不起作用,数据是相同的 我的ctrl /* INIT pagination */ $scope.currentPage = 0; $scope.pageSize = 7; /* AJAX lordREST QUERY : calling lord watching */ $scope.change = functio
/* INIT pagination */
$scope.currentPage = 0;
$scope.pageSize = 7;
/* AJAX lordREST QUERY : calling lord watching */
$scope.change = function() {
if ($scope.myFormZR_lord.$valid) {
// RETRIEVE DATAS
var dataSearch = $scope.myForm_lord.search;
var dataParam = $scope.myForm_lord.param['value'];
// CONSOLE LOG CONTROL
console.log($rootScope.defineCLC + "Search requested with params : " + dataParam + " : " + dataSearch);
lordREST.query({search: dataSearch, param: dataParam}, function(response) {
// CONSOLE LOG CONTROL
console.log($rootScope.defineCLC + "number of results : " + response.length);
/* 1 : test if not empty */
if (response.length >= 1) {
// ng-show things
$scope.successLordZR = true;
$scope.failLordZR = false;
// populate scope
$scope.posts = $filter('startFrom')(response, $scope.currentPage*$scope.pageSize);
// pagination , calcul of numberOfPages
$scope.numberOfPages = function() {
return Math.ceil(response.length/$scope.pageSize);
}
console.log($rootScope.defineCLC + "number of pages : " + Math.ceil(response.length/$scope.pageSize));
} else {
//
}
});
} else {
//
}
}
和html部分:
<div id="pagination">
<div class="myLeft">
<button type="button" class="btn btn-primary btn-xs" ng-disabled="currentPage == 0" ng-hide="posts.length <= 7" ng-click="currentPage = 0">{{ 'TRS_CTRL3_FIRST' | translate }}</button>
<button type="button" class="btn btn-primary btn-xs" ng-disabled="currentPage == 0" ng-click="currentPage = currentPage - 1"><i class="fa fa-arrow-circle-o-left"></i> {{ 'TRS_CTRL3_PREV' | translate }} </button>
</div>
<div class="myCenter">
{{currentPage+1}}/{{numberOfPages()}}
</div>
<div class="myRight">
<button type="button" class="btn btn-primary btn-xs" ng-disabled="currentPage >= posts.length/pageSize - 1" ng-click="currentPage = currentPage + 1"> {{ 'TRS_CTRL3_NEXT' | translate }} <i class="fa fa-arrow-circle-o-right"></i></button>
<button type="button" class="btn btn-primary btn-xs" ng-disabled="currentPage >= posts.length/pageSize - 1" ng-hide="posts.length <= 7" ng-click="currentPage = numberOfPages() - 1">{{ 'TRS_CTRL3_LAST' | translate }}</button>
</div>
</div>
怎么了?您可以在搜索输入中尝试“aria”进行测试,并查看控制台。没有错误,因此我认为这是我的分页部分的问题,但html或筛选器有问题?您需要使用ngRepeat应用
开始的筛选器,而不只是在获取数据时调用它一次。试试这个:
<li class="list-group-item ng-scope" ng-repeat="post in posts | orderBy:'name' | startFrom:currentPage*pageSize | limitTo:pageSize" ng-if="posts">
到
您需要在视图中使用您的startFrom
过滤器,您现在使用它的方式在最初加载数据时,它只会被调用一次。或者在页面更改上放置一个手表,然后再次运行过滤器的return me:undefined不是@JoDiii的函数,如果(输入===undefined | |输入===null | |输入.长度===0)
toif(!angular.isArray(input))
Thks,因此第一页显示正确的数据,但在完成后empty@JoDiii更改$scope.posts=$filter('startFrom')(响应,$scope.currentPage*$scope.pageSize);
to$scope.posts=response
@JoDiiistartFrom
应放在limito
之前,否则startFrom
的输入将始终只有pageSize
项目数。
<li class="list-group-item ng-scope" ng-repeat="post in posts | orderBy:'name' | startFrom:currentPage*pageSize | limitTo:pageSize" ng-if="posts">
$scope.posts = $filter('startFrom')(response, $scope.currentPage*$scope.pageSize);
$scope.posts = response;