[英]calculating json data in angularjs
我正在學習angularjs(不是2),並且試圖弄清楚如何計算客戶訂單總數。
我的數據如下所示:
var customers = [
{
id: 1,
joined: '2000-12-02',
name:'John',
city:'Chandler',
orders: [
{
id: 1,
product: 'Shoes',
total: 9.9956
}
]
},
{
id: 2,
joined: '1965-01-25',
name:'Zed',
city:'Las Vegas',
orders: [
{
id: 2,
product: 'Baseball',
total: 9.995
},
{
id: 3,
product: 'Bat',
total: 9.995
}
]
}
]
我有一個工廠和一個接收數據的控制器,然后...
我的控制器看起來像:
(function() {
var CustomersController = function ($scope, $log, customerFactory) {
$scope.sortBy = 'name';
$scope.reverse = false;
$scope.customers = [];
function init () {
customerFactory.getCustomers()
.success(function(customers) {
$scope.customers = customers;
})
.error(function(data, status, headers, config){
$log.log(data.error + ' ' + status );
});
}
init();
$scope.doSort = function(propName) {
$scope.sortBy = propName;
$scope.reverse = !$scope.reverse;
};
};
CustomersController.$inject = ['$scope','$log' ,'customerFactory'];
angular.module('customersApp')
.controller('CustomersController', CustomersController);
}());
我的看法是這樣的:
<h2>Customers</h2>
Filter: <input type="text" ng-model="customerFilter.name" />
<br /><br />
<table>
<tr>
<th ng-click="doSort(name)">Name</th>
<th ng-click="doSort(city)">City</th>
<th ng-click="doSort(joined)">Joined</th>
<th> </th>
</tr>
<tr ng-repeat="cust in customers | filter:customerFilter | orderBy:sortBy:reverse">
<td>{{ cust.name }}</td>
<td>{{ cust.city }}</td>
<td><a href="#/orders/{{ cust.employeeid }}">View Orders</a></td>
</tr>
</table>
<br />
<span>Total customers: {{ customers.length }} </span>
我了解如何在訂單總計列中添加...但是鑒於返回的json,如何計算呢?
- - - - - - - -編輯 - - - - - - -
我認為這是正確的方向嗎?
customerFactory.getCustomers()
.success(function(customers) {
for(var i = 0; i < customers.length; i++) {
var currCustomer = customers[i];
var aCustomerTotal = 0;
if (currCustomer.hrs) {
for (var j = 0; j < currCustomer.hrs.length; j++) {
aCustomerTotal += parseInt(currCustomer.hrs[j].bllhrs);
}
} else {
aCustomerTotal=0
}
customers[i].orderTotal = aCustomerTotal;
console.log(currCustomer.lastname + " total: " + aCustomerTotal);
}
// after the exeuction of this logic set your $scope's customer to the modified object.
$scope.customers = customers;
})
如果您只想獲得每個客戶的總數->您可以使用簡單的double for循環:
.success(function(customers) {
for(var i = 0; i < customers.length; i++) {
var currCustomer = customers[i];
var aCustomerTotal = 0;
for (var j = 0; j < currCustomer.orders.length; j++) {
aCustomerTotal += currCustomer.orders[j].total;
}
customers[i].orderTotal = aCustomerTotal;
console.log(currCustomer.name + " total: " + aCustomerTotal);
}
// after the exeuction of this logic set your $scope's customer to the modified object.
$scope.customers = customers;
}
您可以為此創建自定義過濾器 (假設您也使用lodash進行簡化):
angular.module('customersApp', [])
.filter('customerTotal', function() {
return function(customer) {
return _.reduce(customer.hrs, function(sum, hrs) {
return sum + parseInt(hrs.bllhrs);
}, 0);
};
});
您可以像這樣使用該過濾器:
<td>{{ curr.name }} total: {{ cust | customerTotal }}</td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.