Can I group by multiple fields using Angular-Filter?
Asked Answered
G

2

11

I'm using Angular-Filter's groupBy filter.

Example from 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>

So, the example groups the players collection by team. Imagine there's an age property for every player object:

$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}
];

I want to group by team and age. How can I do that?

Greaseball answered 25/3, 2016 at 16:14 Comment(2)
Have you tried just adding another groupBy?Threepiece
What are expected results? What have you tried? Seems like a nested groupBy would do what you needTimbre
H
17

Follow the example of multiple fields

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}
    ];
 });
<!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>
Honora answered 12/5, 2016 at 14:39 Comment(2)
How do you access the multiple key's separately in your html? Key seems to become a string of "team,age" do I need to split that? {{key.team}} is undefined. etc.Hallo
To answer my own comment: {{value[0].team}} and {{value[0].age}} will do the trick. With a groupby you should be able to count on at least one row per "team" and "age" (or group) existing, so no need to check count > 0. Far from elegant but it should work and be reliable. Wish the key would become json or something though when using multiple fields for the key.Hallo
T
3

try the following i am assuming you want groups to be created using multiple values as criteria

ng-repeat="(key, value) in players | groupBy: '[team,age]'"
Tritheism answered 12/4, 2016 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.