我通过将团队转换为对象将Angular filter group by示例扩展到以下内容。
var app = angular.module('myApp',['angular.filter']);
app.controller('MainController', ['$scope', function($scope){
$scope.players = [
{
name: 'Gene',
team: {
'id' : '1',
'name' : 'alpha'
}
},
{
name: 'George',
team: {
'id' : '2',
'name' : 'beta'
}
},
{
name: 'Steve',
team: {
'id' : '3',
'name' : 'gamma'
}
},
{
name: 'Paula',
team: {
'id' : '2',
'name' : 'beta'
}
},
{
name: 'Scruath',
team: {
'id' : '3',
'name' : 'gamma'
}
}
];
}]);
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script data-require="angular-filter@*" data-semver="0.5.7" src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.7/angular-filter.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="MainController">
<ul>
<li ng-repeat="(team, players) in players | groupBy: 'team.name'">
<a href="#I need the team ID">Group name: {{ team }}</a>
<ul>
<li ng-repeat="player in players">
player: {{ player.name }}
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
但是,如果我需要团队中的团队ID呢?我该怎么办?
<a href="#I need the team ID">Group name: {{ team }}</a>
我尝试按团队对象进行分组,然后使用team.name
和team.id
,但它不起作用。另外,我不知道如何使用多个字段(team.id, team.name)
答案 0 :(得分:4)
我有一个简单的解决方案:
我按team.id
分组
<li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">
然后我在组中使用了players[0].team.name
<li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">
<a href="#Here I can use the group teamid">Group name: {{ players[0].team.name }}</a>
<ul>
<li ng-repeat="player in players">
player: {{ player.name }}
</li>
</ul>
</li>
由于每个组中的players
只是属于该组的玩家,而且所有人都拥有相同的团队,因此players[0]
,players[1]
等具有相同的团队名称
var app = angular.module('myApp',['angular.filter']);
app.controller('MainController', ['$scope', function($scope){
$scope.players = [
{
name: 'Gene',
team: {
'id' : '1',
'name' : 'alpha'
}
},
{
name: 'George',
team: {
'id' : '2',
'name' : 'beta'
}
},
{
name: 'Steve',
team: {
'id' : '3',
'name' : 'gamma'
}
},
{
name: 'Paula',
team: {
'id' : '2',
'name' : 'beta'
}
},
{
name: 'Scruath',
team: {
'id' : '3',
'name' : 'gamma'
}
}
];
}]);
&#13;
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script data-require="angular-filter@*" data-semver="0.5.7" src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.7/angular-filter.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="MainController">
<ul>
<li ng-repeat="(teamid, players) in players | groupBy: 'team.id'">
<a href="#Here I can use the group teamid">Group name: {{ players[0].team.name }}</a>
<ul>
<li ng-repeat="player in players">
player: {{ player.name }}
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
&#13;
答案 1 :(得分:1)
您应该按team.id
分组,因为团队名称不是唯一的。这样可以避免错误的数据输出。好吧好吧。我能够通过在控制器中预先收集您的团队并通过team.id
在单独的对象中对其进行规范化来解决您的问题 - demo fiddle:
<body ng-app="myApp">
<div ng-controller="MainController">
<ul>
<li ng-repeat="(team, players) in players | groupBy: 'team.id'">
<a href="{{ team }} ">Group name: {{ teams[team].name }}</a>
<ul>
<li ng-repeat="player in players">
player: {{ player.name }}
</li>
</ul>
</li>
</ul>
</div>
</body>
var app = angular.module('myApp',['angular.filter']);
app.controller('MainController', ['$scope', function($scope){
$scope.players = [
{
name: 'Gene',
team: {
'id' : '1',
'name' : 'alpha'
}
}, {
name: 'George',
team: {
'id' : '2',
'name' : 'beta'
}
}, {
name: 'Steve',
team: {
'id' : '3',
'name' : 'gamma'
}
}, {
name: 'Paula',
team: {
'id' : '4',
'name' : 'beta'
}
}, {
name: 'Scruath',
team: {
'id' : '5',
'name' : 'gamma'
}
}];
$scope.teams = {};
$scope.players.forEach(function (player) {
if (angular.isUndefined($scope.teams[player.team.id])) {
$scope.teams[player.team.id] = player.team;
}
});
}]);