I've got a REST API for my web app which returns one JSON which joins several tables together from my database. I'm able to pull these into my AngularJS app without any issues, however when I want to group the output by one of the fields, I can't find a clear way to do this.
As an example of how I've currently got my services:
angular.module('myapp.services', ['ngResource'])
.factory('Item', function($resource) {
return $resource('api/items/:itemId', {}, {
query: {method: 'GET', isArray: true}
});
})
Which I then pull in via:
<div class="items">
<div class="item" ng-repeat="item in items">
<div ng-include="'/partials/item.html'"></div>
</div>
</div>
What I would like to do is display collections of these items in a list with them grouped according to group name. So a call to:
/api/collections/:collection_id/items
Would return JSON output like so (below is for one item):
[ { item_id: 1,
item_name: 'Monkey',
item_action: 'Eats a banana',
group_id: 1,
group_name: 'Primates',
collection_id: 1,
collection_name: 'Animals'
} ]
So I'd want to be able to output one collection of them like so:
<h1>Animals</h1>
<div class="group">
<h2>Primates</h2>
<div class="items">
<div class="item">
<p>Monkey - eats a banana</p>
<p>Gorilla - bangs his chest</p>
</div>
</div>
</div>
<div class="group">
<h2>Fish</h2>
<div class="items">
<div class="item">
<p>Salmon - sings Sinatra songs</p>
</div>
</div>
</div>
<div class="group">
<h2>Insects</h2>
<div class="items">
<div class="item">
<p>Ant - Forages for food</p>
</div>
</div>
</div>
Is this possible using some sort of ng-repeat structure like in my example above? I'm almost certain it is, but I'm new to AngularJS and have been having difficulty finding any examples like this online.
I'd like to keep everything being read from that one JSON file to keep requests to the database to a minimum (one request on page load and then the JS deals with the rest).
Any help is greatly appreciated!