2017-08-24 79 views
-2

我创建一个web应用程序中,在我的名单我得到一个名为TLIST列表,获取从列表中特定数据的总和angularjs

这是我的数据看起来像

0 
: 
{id: "135", tid: "1", date: "Aug 24 2017 12:00AM", amount: "1700", empname: "Andrew Charles", …} 
1 
: 
{id: "136", tid: "1", date: "Aug 25 2017 12:00AM", amount: "1200", empname: "Andrew Charles", …} 

现在我想获得我的列的金额总和

我需要在这里做什么?

$scope.Tlist 

这是我的名单

我想进去$scope.gettotalamount 数据我需要做什么?

回答

0

可以遍历你的对象来获得总和

var app = angular.module("sub", [], function() {}); 
 
app.controller("my_ctrl", function($scope) { 
 
    $scope.Tlist={0 
 
: 
 
{id: "135", tid: "1", date: "Aug 24 2017 12:00AM", amount: "1700", empname: "Andrew Charles"}, 
 
1 
 
: 
 
{id: "136", tid: "1", date: "Aug 25 2017 12:00AM", amount: "1200", empname: "Andrew Charles"}}; 
 
$scope.gettotalamount=0; 
 
angular.forEach($scope.Tlist,function(value,key){ 
 
    $scope.gettotalamount+=parseFloat(value.amount); 
 
}); 
 

 
});
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
 
</head> 
 

 
<body ng-app="sub" ng-controller="my_ctrl"> 
 
    <div>{{gettotalamount}}</div> 
 
</body>

+0

我有动态列表,这是不与动态列表 –

+0

如何为您的动态列表的工作?它如何得到更新? – Vivz

+0

我从我的数据库中获取数据 –

0

这应该给你你需要的东西:

function addItUp(input) { 
    return input.map(item => +item.amount).reduce((a, b) => a + b, 0); 
} 

$scope.gettotalamount = addItUp($scope.TList); 

这应该添加它,甚至假设你的'金额'是一个字符串。如果它是一个格式不正确的数字,它会有问题,但我假设你在这部分的错误检查是彻底的。

0

看来你刚才复制并粘贴了你的控制台输出,这不是你的数据的真实反映。如果它是一个数组,然后遍历这些项目并从每个项目中获取金额,如果它是一个对象,则遍历每个键值对并从每个值中获取金额(如Vivz指出的那样)。

这里有两个例子:

// app.js 
 
(function() { 
 

 
    'use strict'; 
 

 
    angular.module("app", []); 
 

 
})(); 
 

 
// main.controller.js 
 
(function() { 
 

 
    'use strict'; 
 

 
    angular.module("app").controller("MainController", MainController); 
 

 
    MainController.$inject = ['$scope']; 
 

 
    function MainController($scope) { 
 

 
    $scope.myArray = [{ 
 
     id: "135", 
 
     tid: "1", 
 
     date: "Aug 24 2017 12:00AM", 
 
     amount: "1700", 
 
     empname: "Andrew Charles" 
 
     }, 
 
     { 
 
     id: "136", 
 
     tid: "1", 
 
     date: "Aug 25 2017 12:00AM", 
 
     amount: "1200", 
 
     empname: "Andrew Charles" 
 
     } 
 
    ]; 
 

 
    $scope.myObject = { 
 
     0: { 
 
     id: "135", 
 
     tid: "1", 
 
     date: "Aug 24 2017 12:00AM", 
 
     amount: "1700", 
 
     empname: "Andrew Charles" 
 
     }, 
 
     1: { 
 
     id: "136", 
 
     tid: "1", 
 
     date: "Aug 25 2017 12:00AM", 
 
     amount: "1200", 
 
     empname: "Andrew Charles" 
 
     } 
 
    }; 
 

 
    $scope.arrayTotal = getTotal($scope.myArray); 
 
    $scope.objectTotal = getTotal($scope.myObject); 
 

 
    function getTotal(data) { 
 

 
     var total = 0; 
 

 
     if (angular.isArray(data)) { 
 

 
     // loop through each item in the array and add the amount to the total 
 
     angular.forEach(data, function(item) { 
 
      total += parseFloat(item.amount); 
 
     }); 
 

 
     } else if (angular.isObject(data)) { 
 
     // it's an object so loop through each key/val pair and sum the 
 
     // amount property of the value 
 
     angular.forEach(data, function(val, key) { 
 
      total += parseFloat(val.amount); 
 
     }); 
 
     } 
 

 
     return total; 
 
    } 
 

 
    } 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
 

 
<body ng-app="app"> 
 
    <div ng-controller="MainController as MainCtrl"> 
 
    <p>arrayTotal: {{arrayTotal}}</p> 
 
    <p>objectTotal: {{objectTotal}}</p> 
 
    </div> 
 
</body>

相关问题