Javascript 对所有ID重复AngularJS ng
我在应用程序中使用Angular 我正在从数据库中获取数组中的数据,其中包含以下项:Javascript 对所有ID重复AngularJS ng,javascript,angularjs,Javascript,Angularjs,我在应用程序中使用Angular 我正在从数据库中获取数组中的数据,其中包含以下项: $ordersall[$count] = array( "orderID" => $looporders["orderID"], "customerID" => $looporders["customerID"], "customerName" => $customerName, "orderDate" =&g
$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和orderdetails,如产品、价格、附加等等,。。。但是一个订单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;
}
});
});
问题可能在于服务器返回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是嵌套数组(每个订单都有一个产品数组),那么您可以在两次ng重复中轻松完成它
<li ng-repeat= "p in categories" ng-hide="categories.extra1.length">
{{p.orderID}}
<ul>
<li ng-prepeat="product in p.products">
{{product.title}}
</li>
</ul>
</li>
{{p.orderID}}
-
{{product.title}}
所以,也许解决方案是改变服务器返回JSON数据的方式?(如果可能的话)一个更干净的解决方案是首先提供订单列表。当用户选择特定订单时,您可以导航到其他视图以显示订单详细信息。这对于可伸缩性很重要。但是,如果您想继续您的方法,您可以通过使用嵌套的ng重复来实现,如下所示
<div ng-repeat="order in orders">
<span>{{order.orderID}}</span>
<span>{{order.customerID}}</span>
<span>{{order.customerName}}</span>
<span>{{order.orderDate}}</span>
<span>{{order.orderDeliveryDate}}</span>
<span>{{order.orderStatus}}</span>
<div ng-repeat="orderDetail in order">
<span>{{orderDetail.orderdetailID}}</span>
<span>{{orderDetail.productTitle}}</span>
<span>{{orderDetail.productPrijs}}</span>
<span>{{orderDetail.aantal}}</span>
<span>{{orderDetail.extras}}</span>
</div>
{{order.orderID}
{{order.customerID}}
{{order.customerName}
{{order.orderDate}
{order.orderDeliveryDate}
{{order.orderStatus}
{{orderDetail.orderdetailID}
{{orderDetail.productTitle}
{{orderDetail.productPrijs}}
{{orderDetail.aantal}
{{orderDetail.extras}
如果您的数据是这样的,那么JSON数据中的每个订单都附带了该订单的所有订单详细信息,那么这将起作用
下面是一个示例JSON数据来说明这一点
[{“orderID”:4,“customerID”:1,“customerName”:“Sander Hofman”,“orderDate”:“2015-04-20 09:49:06”,“orderDeliveryDate”:“2015-04-20”,“orderStatus”:“完成”,
订单详情:[
{“orderdetailID”:10,“productTitle”:“性感老师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:“},
{“orderdetailID”:11,“productTitle”:“Sexy2教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”},
{“orderdetailID”:12,“productTitle”:“Sexy3教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”},
{“orderdetailID”:13,“productTitle”:“Sexy4教师”,“productPrijs”:4,“aantal”:1,“extras”:“,”extrasprice”:“},
{“orderdetailID”:14,“productTitle”:“Sexy5教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”
]},
{“orderID”:5,“customerID”:2,“customerName”:“Francis Ezengage”,“orderDate”:“2015-04-20 09:49:06”,“orderDeliveryDate”:“2015-04-20”,“orderStatus”:“完成”,
订单详情:[
{“orderdetailID”:20,“productTitle”:“性感老师”,“productPrijs”:4,“安塔尔”:1,“附加”:“附加”:“附加精神”:”},
{“orderdetailID”:21,“productTitle”:“Sexy2教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”},
{“orderdetailID”:22,“productTitle”:“Sexy3教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”},
{“orderdetailID”:23,“productTitle”:“Sexy4教师”,“productPrijs”:4,“aantal”:1,“extras”:“,”extrasprice”:“},
{“orderdetailID”:24,“productTitle”:“Sexy5教师”,“productPrijs”:4,“aantal”:1,“extras”:“extrasprice”:”
]}]
如果仔细研究上述内容,您会发现,在主订单表中的字段之后,会出现一个名为orderDetails的字段。此字段的值是一个数组,其元素是order detail类型的对象。因此,我在示例中有一个2个订单的数组,每个订单在orderDetails中有5个项目。因此,ng repeat会重复所有订单,但在每个订单中也会重复orderDetails中的每个项目
至于示例代码,它只涉及以上述格式返回JSON数据。具体的实现取决于您使用的服务器类型。能否添加一些示例json?你想使用角度。请在这里稍作修改。第二个ng repeat应该是ng repeat=“orderDetail in order.orderDetails”,其中每个订单都有一个名为orderDetails的订单详细信息数组。我试图理解您的方法,但如果您也能提供一个角度代码示例,会容易得多。嗯,我得到一个错误:JSON.parse:第1行的JSON数据后出现意外的非空白字符
<div ng-repeat="order in orders">
<span>{{order.orderID}}</span>
<span>{{order.customerID}}</span>
<span>{{order.customerName}}</span>
<span>{{order.orderDate}}</span>
<span>{{order.orderDeliveryDate}}</span>
<span>{{order.orderStatus}}</span>
<div ng-repeat="orderDetail in order">
<span>{{orderDetail.orderdetailID}}</span>
<span>{{orderDetail.productTitle}}</span>
<span>{{orderDetail.productPrijs}}</span>
<span>{{orderDetail.aantal}}</span>
<span>{{orderDetail.extras}}</span>
</div>