Having problems with groupBy filter of angular-filter module in a nested array. The 'location' is undefined and 'color' isn't outputting anything. Took this example and modified it from someone else's code.
Thanks in advance!
<!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>
<script>
angular.module('app',['angular.filter']).controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene', team: 'alpha'[
{location: 'texas',
color: 'blue'}]},
{name: 'George', team: 'beta'[
{location: 'texas',
color: 'red'}]},
{name: 'Steve', team: 'gamma'[
{location: 'kansas',
color: 'purple'}]},
{name: 'Paula', team: 'beta'[
{location: 'kansas',
color: 'green'}]},
{name: 'Scruath', team: 'gamma'[
{location: 'kansas',
color: 'orange'}]}
];
});
</script>
<title>JS Bin</title>
</head>
<body>
<div ng-controller="MainController">
<ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }} <br/>
favorite color: {{ player.color }}
</li>
</ul>
</div>
</body>
</html>
Your data structure doesn't correspond to your code logic. This works fine:
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<script>
angular.module('app',['angular.filter']).controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene',
team: {location: 'texas',
color: 'blue'}
},
{name: 'George', team: {location: 'texas',
color: 'red'}},
{name: 'Steve', team:
{location: 'kansas',
color: 'purple'}},
{name: 'Paula', team:
{location: 'kansas',
color: 'green'}},
{name: 'Scruath', team:
{location: 'kansas',
color: 'orange'}}
];
});
</script>
<title>JS Bin</title>
</head>
<body>
<div ng-controller="MainController">
<ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }} <br/>
favorite color: {{ player.color }}
</li>
</ul>
</div>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With