140

I have a list of players which belong to a group each. How can I use a filter to list the users per group?

[{name: 'Gene', team: 'team alpha'},
 {name: 'George', team: 'team beta'},
 {name: 'Steve', team: 'team gamma'},
 {name: 'Paula', team: 'team beta'},
 {name: 'Scruath of the 5th sector', team: 'team gamma'}];

I'm looking for this result:

  • team alpha
    • Gene
  • team beta
    • George
    • Paula
  • team gamma
    • Steve
    • Scruath of the 5th sector
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96

8 Answers8

183

You can use groupBy of angular.filter module.
so you can do something like this:

JS:

$scope.players = [
  {name: 'Gene', team: 'alpha'},
  {name: 'George', team: 'beta'},
  {name: 'Steve', team: 'gamma'},
  {name: 'Paula', team: 'beta'},
  {name: 'Scruath', team: 'gamma'}
];

HTML:

<ul ng-repeat="(key, value) in players | groupBy: 'team'">
  Group name: {{ key }}
  <li ng-repeat="player in value">
    player: {{ player.name }} 
  </li>
</ul>

RESULT:
Group name: alpha
* player: Gene
Group name: beta
* player: George
* player: Paula
Group name: gamma
* player: Steve
* player: Scruath

UPDATE: jsbin Remember the basic requirements to use angular.filter, specifically note you must add it to your module's dependencies:

(1) You can install angular-filter using 4 different methods:

  1. clone & build this repository
  2. via Bower: by running $ bower install angular-filter from your terminal
  3. via npm: by running $ npm install angular-filter from your terminal
  4. via cdnjs http://www.cdnjs.com/libraries/angular-filter

(2) Include angular-filter.js (or angular-filter.min.js) in your index.html, after including Angular itself.

(3) Add 'angular.filter' to your main module's list of dependencies.

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
a8m
  • 9,334
  • 4
  • 37
  • 40
  • Great example. However, the key returns the group name and not the actual key... how can we solve that? – WJA Mar 18 '15 at 11:20
  • What is the "actual key" for you ? can you provide please some fiddle/jsbin example ? – a8m Mar 18 '15 at 12:12
  • I'm trying to figure out how add checkbox for group and items, if groupd name checkbox is clicked all item under it should be selected. – tomalex Apr 16 '15 at 22:45
  • here's some quick example - http://jsbin.com/mazeye/1/edit?html,js,output @tomalex – a8m Apr 17 '15 at 06:56
  • I have created sample demo with checkbox feature, its kind of complex http://plnkr.co/edit/1fNicDw4W7hidKB6DgPa?p=preview @ArielM. – tomalex Apr 20 '15 at 23:28
  • I have updated my demo with a button to dynamically update model value http://plnkr.co/edit/1fNicDw4W7hidKB6DgPa?p=preview, but model change does not trigger ng change event of checkbox. How can we fix this issue? – tomalex May 08 '15 at 02:33
  • @lcrespofalco `val.length` ? see jsbin: http://jsbin.com/bixoqe/3/edit?html,js,output – a8m May 13 '15 at 04:13
  • 8
    Don't forget to include the `angular.filter` module. – Puce Jun 02 '15 at 13:49
  • @ArielM. can i use multiple values like 'team,age' if age is avalable with 'team' and 'name' in: `
      `
    – Asmita Oct 07 '15 at 13:10
  • You can pass an "angular-expression" as an argument. for example: `groupBy: 'team || age'`. here's some [fiddle](http://jsbin.com/xecata/edit?html,js,output) to demonstrate it. Thanks. – a8m Oct 07 '15 at 13:25
  • So I'd like to add the very sad caveat I just learned about, which is that when you loop through object keys with ng-repeat, the object keys are output in alphabetical order, and you also can't use orderBy. Watch for this issue when using the groupBy filter, which returns the array you want to loop over as an object, with the specified property as its keys. https://github.com/angular/angular.js/issues/6210 – erfling Jan 13 '16 at 23:37
  • 1
    you can use order-by with group-by @erfling, PTAL on: https://github.com/a8m/angular-filter/wiki/Common-Questions#order-groupby-result – a8m Jan 14 '16 at 15:55
  • 1
    Oh wow. Thanks. I didn't expect ordering the nested loop to affect the outer one in that way. That's really useful. +1 – erfling Jan 14 '16 at 16:07
  • @ArielM. how can I group using multiple property like this http://stackoverflow.com/q/36223686/1230188 ? Suppose I have `groupId` and `groupName` then I want to include those properties in "`key`", so I can access `key.groupId` and `key.groupName` – Farhan Ghumra May 12 '16 at 11:52
  • Use this lib that has a groupBy method: https://github.com/a8m/angular-filter. The custom method created here has problems chaining filters on the diggest loop – Julio Marins Jul 08 '16 at 00:49
  • 1
    @Xyroid even i'm looking for the same i want to make `key` as object . any luck from you side – super cool Aug 06 '16 at 07:35
  • hi adding to your fiddle, imagine you have filter textbox to filter out list of category group. My requirment is that i couldn't able to hide groupName from UI when corresponding filtered list is empty. **

    Group name: {{ key }}

  • player: {{ player.name }}
  • ** . I want to hide groupName when value is empty. i tried ngHide no luck – Saravanan Jan 02 '17 at 07:06
  • How can I get the real index of player in this case? – Ankitkumar Tandel May 02 '18 at 08:25
  • How we can use this in angular 2+? – Ankur Jun 20 '18 at 13:00