我limitTo
在文档中看到了过滤器,该过滤器使我可以限制前5个或最后5个结果,但是我想设置限制的起始位置,以便显示第二组5个结果。
是否有内置的过滤器?
Answers:
从Angular 1.4.0开始,limitTo
过滤器接受一个可选begin
参数:
<div ng-repeat="item in items | limitTo:5:5">{{item}}</div>
在旧版本中,编写自定义过滤器非常简单。这是一个简单的实现Array#slice
(请注意,您传递的是第一个和最后一个索引,而不是一个计数):
app.filter('slice', function() {
return function(arr, start, end) {
return (arr || []).slice(start, end);
};
});
<div ng-repeat="item in items | slice:6:10">{{item}}</div>
可用的jsFiddle:http://jsfiddle.net/BinaryMuse/vQUsS/
另外,您可以简单地窃取Angular 1.4.0的整个实现limitTo
:
function limitToFilter() {
return function(input, limit, begin) {
if (Math.abs(Number(limit)) === Infinity) {
limit = Number(limit);
} else {
limit = toInt(limit);
}
if (isNaN(limit)) return input;
if (isNumber(input)) input = input.toString();
if (!isArray(input) && !isString(input)) return input;
begin = (!begin || isNaN(begin)) ? 0 : toInt(begin);
begin = (begin < 0 && begin >= -input.length) ? input.length + begin : begin;
if (limit >= 0) {
return input.slice(begin, begin + limit);
} else {
if (begin === 0) {
return input.slice(limit, input.length);
} else {
return input.slice(Math.max(0, begin + limit), begin);
}
}
};
}
ControllerAs
符号表示:jsfiddle.net/r9kvaydh/3
item in main.items
{{ limitTo_expression | limitTo : limit : begin}}
AngularJS提供了开箱即用的功能。如果您仔细阅读limitTo文档,则可以为该限制指定一个负值。这意味着最后有N个元素,因此,如果要在偏移量5之后处理5个结果,则需要执行以下操作:
<div ng-repeat="item in items | limitTo: 10 | limitTo: -5">{{item}}</div>
The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length
我认为这个答案不正确。
我开始使用客户过滤器,但是发现您可以slice
在ng-repeat
表达式内部调用:
<div ng-repeat="item in items.slice(6, 10)">{{item}}</div>
就像蓝屏所说的那样limitTo
,尽管要处理Harry Oosterveen注意到的最后一页问题还需要做一些额外的工作,但是只能使用过滤器来完成。
即使用ui-bootstrap pagination
指令属性:
ng-model = page // current page
items-per-page = rpp // records per page
total-items = count // total number of records
表达式应为:
<div ng-repeat="item in items | limitTo: rpp * page | limitTo: rpp * page < count ? -rpp : rpp - (rpp * page - count)">{{item}}</div>
这是一个有关如何使用偏移量和限制过滤列表的功能示例:
<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
<script src="lib/angular/angular.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-controller="PhoneListCtrl">
Offset: <input type="text" ng-model="offset" value="0" /><br />
Limit: <input type="text" ng-model="limit" value="10" /><br />
<p>offset limitTo: {{offset - phones.length}}</p>
<p>Partial list:</p>
<ul>
<li ng-repeat="phone in phones | limitTo: offset - phones.length | limitTo: limit">
{{$index}} - {{phone.name}} - {{phone.snippet}}
</li>
</ul>
<hr>
<p>Whole list:</p>
<ul>
<li ng-repeat="phone in phones">
{{$index}} - {{phone.name}} - {{phone.snippet}}
</li>
</ul>
</body>
</html>
该示例基于AngularJS教程的第二步。
请注意,如果要涵盖限制为已知的一般情况,则必须将偏移量的过滤器作为第一个过滤器。
有一种更简洁的方法,而不必调用过滤器,其行为更像是分页器:
$scope.page = 0;
$scope.items = [ "a", "b", "c", "d", "e", "f", "g" ];
$scope.itemsLimit = 5;
$scope.itemsPaginated = function () {
var currentPageIndex = $scope.page * $scope.itemsLimit;
return $scope.items.slice(
currentPageIndex,
currentPageIndex + $scope.itemsLimit);
};
并坚持您的观点:
<ul>
<li ng-repeat="item in itemsPaginated() | limitTo:itemsLimit">{{item}}</li>
</ul>
然后,您可以增加/减少$scope.page
:
$scope.page++;
对于基于离子的项目,请找到以下解决方案:
mainpage.html:
<ion-view view-title="My Music">
<ion-content>
<ion-list>
<ion-item ng-repeat="track in tracks | limitTo: limit | limitTo: -10 ">
{{track.title}}
</ion-item>
</ion-list>
<div class="list"></div>
<div align="center">
<button class="button button-small button-positive" ng-disabled="currentPage == 0" ng-click="decrementLimit()">
Back
</button>
<button class="button button-small button-energized">
{{currentPage+1}}/{{numberOfPages()}}
</button>
<button class="button button-small button-positive" ng-disabled="currentPage >= data_length/pageSize - 1" ng-click="incrementLimit()">
Next
</button>
</div>
</ion-content>
</ion-view>
controller.js:
var myApp = angular.module('musicApp.controllers', []);
myApp.controller('AppCtrl', function($scope, $http) {
console.log('AppCtrl called');
// $scope.tracks = MusicService.query();
$http.get('api/to/be/called').then(function(result){
$scope.tracks = result.data['results'];
$scope.data_length = $scope.tracks.length;
console.log(result)
// console.log($sco pe.tracks.length)
});
var currentPage = 0;
$scope.pageSize = 10;
$scope.numberOfPages = function(){
return Math.ceil($scope.tracks.length/$scope.pageSize);
}
var limitStep = 10;
$scope.limit = limitStep;
$scope.currentPage = currentPage;
$scope.incrementLimit = function(){
$scope.limit += limitStep;
$scope.currentPage += 1
};
$scope.decrementLimit = function(){
$scope.limit -= limitStep;
$scope.currentPage -= 1
}
});