【发布时间】:2015-04-30 10:51:04
【问题描述】:
我在应用程序中使用 Angular。
我正在从我的数据库中获取包含以下项目的数组中的数据:
$ordersall[$count] = array(
"orderID" => $looporders["orderID"],
"customerID" => $looporders["customerID"],
"customerName" => $customerName,
"orderDate" => $looporders["orderDate"],
"orderDeliveryDate" => $looporders["orderDeliveryDate"],
"orderStatus" => $looporders["orderStatus"],
"orderdetailID" => $row["orderdetailID"],
"productTitle" => $productTitle,
"productPrijs" => $productPrijs,
"aantal" => $row["orderdetailAantal"],
"extras" => "",
"extrasprice" => ""
);
现在我想为每个 ORDERID 打印这些数据,每个我想显示的 ORDERID:customerName、orderDate 和订单详细信息,如产品、价格、额外费用等。但是订单 ID(例如:5)可以有多个订单详细信息,因此有多个产品名称、价格、附加功能等。
当我只有 1 个 orderID 时,我这样解决:
角度
app = angular.module('app', []);
app.controller("NavCtrl",function($scope,$http){
var serviceBase = 'api/';
$http.get(serviceBase + 'orderoverview/58').then(function (results) {
$scope.categories = results.data;
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.categories.length; i++){
var categories = $scope.categories[i];
total += (categories.productPrice * categories.orderdetailAantal);
if(categories.extra1 != "")
{
total += categories.extrasPrice;
}
}
return total;
}
});
});
HTML
<div class="col-md-3" ng-controller="NavCtrl">
<div class="sm-st clearfix">
<!--<span class="sm-st-icon st-red"><i class="fa fa-check-square-o"></i></span>-->
<div class="sm-st-info">
<i class="fa fa-square"></i>
<span>Sander Hofman</span>
<p>1 minutes</p>
<ul>
<li ng-repeat= "p in categories" ng-hide="categories.extra1.length">
{{p.orderdetailAantal}} {{p.productTitle}} <br>
+ {{p.extra1}} {{p.extra2}} {{p.extra3}} {{p.extra4}}
</li>
<li class="divider"></li>
</ul>
<p class="totalprice">{{ getTotal() }} euro</p>
</div>
</div>
</div>
但是现在如何在 html 中实现这一点,我为每个 orderID 及其所有 orderdetails 都有一个列表项?
示例:
订单ID:5
orderdetail1:产品名称、价格、数量
orderdetail2:产品名称、价格、数量
orderdetail2:产品名称、价格、数量
orderID6:
orderdetail1:产品名称、价格、数量
JSON 示例
[{"orderID":4,"customerID":1,"customerName":"Sander Hofman","orderDate":"2015-04-20 09:49:06","orderDeliveryDate":"2015-04-20","orderStatus":"done","orderdetailID":2,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":1,"extras":"","extrasprice":""},
{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":3,"productTitle":"The Coach","productPrijs":3,"aantal":1,"extras":"","extrasprice":""},
{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":4,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""},
{"orderID":5,"customerID":2,"customerName":"Jelle Hofman","orderDate":"2015-04-20 12:05:09","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":5,"productTitle":"Sexy Teacher","productPrijs":4,"aantal":3,"extras":"","extrasprice":""},
{"orderID":22,"customerID":11,"customerName":"Tom Welslau","orderDate":"2015-04-20 14:15:12","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":22,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""},
{"orderID":22,"customerID":11,"customerName":"Tom Welslau","orderDate":"2015-04-20 14:15:12","orderDeliveryDate":"2015-04-20","orderStatus":"prepare","orderdetailID":23,"productTitle":"The Virgin","productPrijs":3.2,"aantal":1,"extras":"","extrasprice":""}
【问题讨论】:
-
你能添加一些示例 json 吗?你想使用
angular.forEach
标签: javascript angularjs