计算angularjs中的json数据

时间:2016-10-12 18:58:09

标签: javascript angularjs json

我正在通过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>&nbsp;</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回来的话,你如何计算呢?

--------------- EDIT --------------

我认为这是正确的道路?

    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;
        })

2 个答案:

答案 0 :(得分:1)

如果您坚持不懈就是每个客户获得一个总数 - &gt;你可以使用一个简单的双循环:

.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;
}

答案 1 :(得分:1)

您可以为此创建custom filter(假设您还使用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>