我正在使用Angular-Filter的groupBy过滤器。
来自GitHub的示例:
$scope.players = [
{name: 'Gene', team: 'alpha'},
{name: 'George', team: 'beta'},
{name: 'Steve', team: 'gamma'},
{name: 'Paula', team: 'beta'},
{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},
{name: 'George', team: 'beta', age: 19},
{name: 'Steve', team: 'gamma', age: 23},
{name: 'Paula', team: 'beta', age: 23},
{name: 'Scruath', team: 'gamma', age: 23}
];
我希望按团队和年龄进行分组。我怎么能这样做?
答案 0 :(得分:15)
按照多个字段的示例
angular.module('app',['angular.filter'])
.controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene', team: 'alpha', age: 19},
{name: 'George', team: 'beta', age: 19},
{name: 'Steve', team: 'gamma', age: 23},
{name: 'Paula', team: 'beta', age: 23},
{name: 'Scruath', team: 'gamma', age: 23}
];
});
&#13;
<!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>
&#13;
答案 1 :(得分:3)
尝试以下我假设您希望使用多个值作为条件创建组
get: function(document, oData) {
var self = this;
var oInnerHTML;
if (oData) {
var oParsedHTML = this._parseHtml(document);
oInnerHTML = oParsedHTML;
oData.forEach(function(configEntry) {
oInnerHTML = self._routeExtentionTypes(configEntry, oInnerHTML);
});
oInnerHTML = this._convertBackToHtml(oInnerHTML);
} else {
oInnerHTML = document;
}
return oInnerHTML
}