AngularJS-使用'true'属性

AngularJS - filter data with 'true' attribute

本文关键字:属性 true 使用 AngularJS-      更新时间:2023-09-26

我使用的是AngularJS,我已经连接了子文档(JSON下面的"订单"),并得到了它的JSON数据量的总和,在orderfood下我有confirm属性。我希望仅过滤在confirm属性中具有值placed的数据。我的plunker演示

我得到的结果

3 quantities of V 4 Vanilla should be prepared
3 quantities of Power cut should be prepared

预期结果

2 quantities of V 4 Vanilla should be prepared
3 quantities of Power cut should be prepared

JSON

[{
"_id": "56e3bff0e9932ca6425e6f65",
"orderfood": [
  {
  "qty": "2",
  "confirm": "placed",
  "name": "V 4 Vanilla"
  }
],
"name": "Rohit",
"created": "2016-03-12T07:06:24.424Z"
},
{
"_id": "56e3bd5bc3791b973c048804",
"user": null,
"__v": 10,
"orderfood": [
  {
  "qty": "1",
  "confirm": "cancelled",
  "name": "V 4 Vanilla"
  },
  {
  "qty": "3",
  "confirm": "placed",
  "name": "Power cut"
  }
],
"name": "Rohit",
"created": "2016-03-12T06:55:23.244Z"
}];

控制器

$scope.getOrderFoods = function() {
var orderfood = [];
$scope.reduce= function(data){
   return data.reduce(function(previousValue,
   currentValue, currentIndex, array) {
  return previousValue + parseInt(currentValue.qty);
}, 0);
}
angular.forEach($scope.orders, function(order) {
  angular.forEach(order.orderfood, function(orderfoo) {
    if (orderfood.indexOf(orderfoo) == -1) {
      orderfood.push(orderfoo);
    }
  })
});
return orderfood;
}

HTML

<div ng-repeat="(key,data) in getOrderFoods() | groupBy:'name'">
  <p><span>{{reduce(data)}}</span> quantities of <span>{{key}}</span> should be prepared </p>
</div>

我的plunker演示

只需在ng-repeat:中添加条件作为筛选器

<div ng-repeat="(key,data) in getOrderFoods() | filter: {confirm: 'placed'} | groupBy:'name'">

Plnkr