gpt4 book ai didi

javascript - AngularJS:angular-filter:如何使用 2 列进行分组?

转载 作者:行者123 更新时间:2023-11-29 19:11:53 25 4
gpt4 key购买 nike

我知道我可能听起来有点困惑所以我请求你让我详细说明

注意:此帖子已附上有效的 POC(代码段)。

我有一个具有 3 个属性的对象数组姓名

  1. 姓名
  2. 团队
  3. team_rank
 $scope.players = [
{name: 'Gene', team: 'alpha', team_rank: 1},
{name: 'George', team: 'beta', team_rank: 2},
{name: 'Steve', team: 'gamma', team_rank: 3},
{name: 'Paula', team: 'beta', team_rank: 2},
{name: 'Scruath', team: 'gamma', team_rank: 3}
];

我通过运行 POC 得到了如下图所示的结果。

enter image description here

问题

我需要用团队名称来显示团队的排名。请建议我该怎么做?

<!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'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }}
</li>
</ul>
</div>

<footer>
<script>
angular.module('app',['angular.filter'])
.controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene', team: 'alpha', team_rank: 1},
{name: 'George', team: 'beta', team_rank: 2},
{name: 'Steve', team: 'gamma', team_rank: 3},
{name: 'Paula', team: 'beta', team_rank: 2},
{name: 'Scruath', team: 'gamma', team_rank: 3}
];
});
</script>
</footer>
</body>
</html>

最佳答案

如果你确定每个玩家都有 team_rank 属性,你可以这样做:

Group name: {{ key }} Rank: {{value[0].team_rank}}

<!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'">
Group name: {{ key }} Rank: {{value[0].team_rank}}
<li ng-repeat="player in value">
player: {{ player.name }}
</li>
</ul>
</div>

<footer>
<script>
angular.module('app',['angular.filter'])
.controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene', team: 'alpha', team_rank: 1},
{name: 'George', team: 'beta', team_rank: 2},
{name: 'Steve', team: 'gamma', team_rank: 3},
{name: 'Paula', team: 'beta', team_rank: 2},
{name: 'Scruath', team: 'gamma', team_rank: 3}
];
});
</script>
</footer>
</body>
</html>

关于javascript - AngularJS:angular-filter:如何使用 2 列进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38280464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com