New to angularjs and looking for some advice. We have hooked our angular front-end to a REST API that is spitting json back such as:
[ { "category_id":"1", "category_name":"dog", "subcategories":[ { "category_id":"4", "parent_id":"1", "category_name":"Sporting", }, { "category_id":"5", "parent_id":"1", "category_name":"Hunter", }, { "category_id":"6", "parent_id":"1", "category_name":"Lap", } ] }, { "category_id":"2", "category_name":"feline", "subcategories":[ { "category_id":"8", "parent_id":"2", "category_name":"Black", }, { "category_id":"9", "parent_id":"2", "category_name":"White", }, { "category_id":"10", "parent_id":"2", "category_name":"Domestic", } ] } ]
We are trying display each category and their sub-categories as rows in a table. We have been trying to us ng-repeat - but it definately doesn't like nested arrays when using tables.
We have hacked something like this together:
<tr ng-repeat-start="cat in category"> <td>{{cat.category_id}}</td> <td>{{cat.category_name}}</td> </tr> <tr ng-repeat-start="subcat in cat.subcategories"> <td>{{subcat.category_id}}</td> <td>{{subcat.category_name}}</td> </tr> <tr ng-repeat-end></tr> <tr ng-repeat-end></tr>
What we would like to be able to do is use datatable (angular-datatables.min.js) functionality for our tables which means that we need to flatten our our json such that subcategories are on the same "level" as categories and are all in one array instead of subarrays.
Does anyone know of a 'nice' way to accomplish this?
Thanks in advance for any assistance.