I receive the below data from my REST API
[
{
// restaurant details
},
"city": {}, // city details
"location": {}, // location details
"menu_categories": [
{
// menu_categories details
"menu_items": [
{
// menu_items details
"menu_modifier_groups": [
{
// menu_modifier_groups details
"menu_modifier_items": []
}
]
}
]
}
]
}
]
In this data, I'm fetching a single restaurant with its menu_categories
that then has a child menu_items
which then has a child menu_modifier_groups
which then has a child menu_modifier_item
.
As you can see I have arrays nested within each other. I want to use ng-repeat
to group menu_items
under menu_categories
. something like below;
menu_category 1
menu_item 1
menu_item 2
menu_category 2
menu_item 3
menu_item 4
Also how can I use ng-repeat
on just one for the array? I have the data assigned to $scope.restuarant
Any guidance appreciated
You can try this (instead of title you can use whatever property you have set):
<ul>
<li data-ng-repeat="category in restaurant.menu_categories">
{{ category.title }}
<ul data-ng-if="category.menu_items">
<li data-ng-repeat="item in category.menu_items">{{ item.title }}</li>
</ul>
</li>
</ul>
You can use a tree view to display the data
Take a look at this : http://ngmodules.org/modules/angular.treeview or this link https://github.com/eu81273/angular.treeview ( it the same )
it take for example this data :
$scope.treedata =
[
{ "label" : "User", "id" : "role1", "children" : [
{ "label" : "subUser1", "id" : "role11", "children" : [] },
{ "label" : "subUser2", "id" : "role12", "children" : [
{ "label" : "subUser2-1", "id" : "role121", "children" : [
{ "label" : "subUser2-1-1", "id" : "role1211", "children" : [] },
{ "label" : "subUser2-1-2", "id" : "role1212", "children" : [] }
]}
]}
]},
{ "label" : "Admin", "id" : "role2", "children" : [] },
{ "label" : "Guest", "id" : "role3", "children" : [] }
];
You can easly adapt the rest api data to this
Working plunker - http://plnkr.co/edit/fjDsJbmd7DIlBK3xrdyu .
To group menu_items
under menu_categories
using ng-repeat
<ul ng-repeat="(key, value) in restaurant" ng-init="outerIndex = $index">
menu_category {{$index + 1}}
<li ng-init="innerIndex = $index" ng-repeat="menu_cat in value.menu_categories">
menu_item {{($parent.$index*value.menu_categories.length)+($index+1)}}
</li>
</ul>
And the output would be:
menu_category 1
- menu_item 1
- menu_item 2
menu_category 2
- menu_item 3
- menu_item 4
menu_category 3
- menu_item 5
- menu_item 6
Hope this answered your question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.