【发布时间】:2014-09-12 05:37:31
【问题描述】:
我在 angularjs 应用程序中的实时搜索功能有问题:http://www.monde-du-rat.fr/zombieReport/#/ratousearch
我的工厂正确检索了数据,并显示了第一页,但是当我单击转到下一个或上一个时,过滤器似乎不起作用,数据是相同的。
我的 ctrl
/* 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>
过滤器:
app.filter('startFrom', function () {
return function (input, start) {
if (input === undefined || input === null || input.length === 0) {
return [];
} else {
start = +start; //parse to int
return input.slice(start);
}
}
});
怎么了?您可以在搜索输入中尝试“aria”进行测试并查看控制台。没有错误,所以我认为这是我的分页部分的问题,但 html 或过滤器?
【问题讨论】:
-
您需要在视图中使用
startFrom过滤器,您现在使用它的方式只会在最初加载数据时调用一次。 -
或关注页面变化并再次运行过滤器
标签: javascript angularjs pagination