ng-options 过滤器(范围选择)
ng-options filter (range selecting)
我无法真正正确地解释我想要什么,但我尝试在 angularJS 中使用 ng-options 工作:
<select ng-options="object.id as object.name for object in objects" ng-model="selected"></select>
因此当前输出为:
1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960 ...
我想实现的是:
1950 - 1954, 1955 - 1959, ....
所以每隔X年就会显示一次。
有什么办法可以做到这一点?
我用 limitTo 尝试过,然后每 5 次 + 5 但没有成功。
有人有想法吗?
我个人只是将该范围分组逻辑推入 javascript 控制器。然后你可以绑定到预先分组的对象数组。
Javascript:
$scope.groupedObjects = [];
var groupDictionary = {}; //for keeping track of the existing buckets
var bucketId = 0;
//assuming objects is an array I prefer forEach, but a regular for loop would work
$scope.objects.forEach(function (obj) {
var yearBucket = Math.floor(obj.name / 5) * 5; //puts things into 5-year buckets
var upperBound = yearBucket + 4;
var bucketName = yearBucket + '-' + upperBound; //the name of the bucket
if (!groupDictionary[bucketName]) { //check whether the bucket already exists
groupDictionary[bucketName] = true;
$scope.groupedObjects.push( {id: "id" + bucketId, name: bucketName} );
bucketId += 1;
}
});
并且只需使用 groupedObjects
<select ng-options="object.id as object.name for object in groupedObjects"
ng-model="group"></select>
这里 plunker 展示了这个想法。
我无法真正正确地解释我想要什么,但我尝试在 angularJS 中使用 ng-options 工作:
<select ng-options="object.id as object.name for object in objects" ng-model="selected"></select>
因此当前输出为:
1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960 ...
我想实现的是:
1950 - 1954, 1955 - 1959, ....
所以每隔X年就会显示一次。 有什么办法可以做到这一点? 我用 limitTo 尝试过,然后每 5 次 + 5 但没有成功。
有人有想法吗?
我个人只是将该范围分组逻辑推入 javascript 控制器。然后你可以绑定到预先分组的对象数组。
Javascript:
$scope.groupedObjects = [];
var groupDictionary = {}; //for keeping track of the existing buckets
var bucketId = 0;
//assuming objects is an array I prefer forEach, but a regular for loop would work
$scope.objects.forEach(function (obj) {
var yearBucket = Math.floor(obj.name / 5) * 5; //puts things into 5-year buckets
var upperBound = yearBucket + 4;
var bucketName = yearBucket + '-' + upperBound; //the name of the bucket
if (!groupDictionary[bucketName]) { //check whether the bucket already exists
groupDictionary[bucketName] = true;
$scope.groupedObjects.push( {id: "id" + bucketId, name: bucketName} );
bucketId += 1;
}
});
并且只需使用 groupedObjects
<select ng-options="object.id as object.name for object in groupedObjects"
ng-model="group"></select>
这里 plunker 展示了这个想法。