我正在使用
Angular-Filter的
groupBy过滤器.
GitHub的例子:
$scope.players = [ {name: 'Gene',team: 'alpha'},{name: 'George',team: 'beta'},{name: 'Steve',team: 'gamma'},{name: 'Paula',{name: 'Scruath',team: 'gamma'} ]; <ul> <li ng-repeat="(key,value) in players | groupBy: 'team'"> Group name: {{ key }} <ul> <li ng-repeat="player in value"> player: {{ player.name }} </li> </ul> </li> </ul>
因此,该示例按团队分组球员集合.想象一下,每个玩家对象都有一个年龄属性:
$scope.players = [ {name: 'Gene',team: 'alpha',age: 19},team: 'beta',team: 'gamma',age: 23},age: 23} ];
我想按团队和年龄分组.我怎样才能做到这一点?
按照多个字段的示例
原文链接:https://www.f2er.com/angularjs/143510.htmlangular.module('app',['angular.filter']) .controller('MainController',function($scope) { $scope.players = [ {name: 'Gene',age: 23} ]; });
<!DOCTYPE html> <html ng-app="app"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script> <Meta name="description" content="[groupBy example]"/> <Meta charset="utf-8"> <title>JS Bin</title> </head> <body> <div ng-controller="MainController"> <ul ng-repeat="(key,value) in players | groupBy: '[team,age]'"> <b>Group name: {{key}}</b> <br> <li ng-repeat="player in value"> <b>player:</b> {{ player.name }} <br> <b>team:</b> {{player.team}} <br> <span ng-if="player.age"><b>age:</b> {{ player.age }}</span> </li> </ul> </div> </body> </html>