group子文档 - angularjs

时间:2016-02-18 04:39:27

标签: arrays angularjs group-by

我在JSON中有对象值,其中包含array.i我正在尝试对子文档进行分组但不起作用。我的plunk

以下是我的控制器代码。 的控制器

app.controller('MainCtrl', function($scope) {
  $scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}
});

以下是我的HTML代码 的 HTML

<p ng-repeat="(key,value) in list.orderfood | groupBy:'name'">
  {{key}}
</p>

我的plunk

1 个答案:

答案 0 :(得分:1)

不知道您想要的产品是什么,但您需要使用自定义功能来实现这一目标:

<div ng-app ng-controller="Main">
    <div ng-repeat="list in itemsToFilter() | filter:filterNames">
        <b>{{list.name}}</b>
        <li ng-repeat="item in itemsToFilter() | filter: {name: list.name}">NAME: {{item.name}}- PRICE: {{item.price}}, etc...</li>        
    </div>
</div>

然后在你的控制器中:

function Main($scope) {
$scope.list = {
"_id": "56c4758af801160e00d176e0",
"orderfood": [
{
"_id": "569d84f04834c10e003dff36",
"qty": "1",
"confirm": "placed",
"price": 125,
"name": "Paneer Tikka"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
},
{
"_id": "569d869fff1fe20e00f8ba9b",
"qty": "1",
"confirm": "placed",
"price": 85,
"name": "Falooda"
}
],
"title": "Status",
"created": "2016-02-17T13:28:42.226Z"
}

var indexedTeams = [];

$scope.itemsToFilter = function() {
    indexedTeams = [];
    return $scope.list.orderfood;
}

$scope.filterNames = function(item) {
    var nameIsNew = indexedTeams.indexOf(item.name) == -1;
    if (nameIsNew) {
        indexedTeams.push(item.name);
    }

    return nameIsNew;
}
}

这是一个链接...... js-fiddle exmple: http://jsfiddle.net/L6cQN/305/