我有一个Angular JS的控制器,它在范围循环的帮助下对值的数量求和,但现在我需要在每次迭代时将总和的前一个值相加。我试过这么多过滤器但没有成功。我需要期望的结果。帮助肯定是赞赏。
var arr = [
{
"unique_id": "CS",
"college": "BSCS",
"arr_length": 1,
"program_section": [
{
"question": "Q1",
"total": 135,
},
{
"question": "Q2",
"total": 142,
},
{
"question": "Q3",
"total": 135,
},
{
"question": "Q4",
"total": 137,
},
]
},
{
"unique_id": "MBA",
"college": "BBA",
"arr_length": 2,
"program_section": [
{
"question": "Q1",
"total": 175,
},
{
"question": "Q2",
"total": 142,
},
{
"question": "Q3",
"total": 165,
},
{
"question": "Q4",
"total": 137,
},
]
},
{
"unique_id": "CA",
"college": "Account",
"arr_length": 1,
"program_section": [
{
"question": "Q1",
"total": 145,
},
{
"question": "Q2",
"total": 162,
},
{
"question": "Q3",
"total": 125,
},
{
"question": "Q4",
"total": 117,
},
]
}
];
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.names = arr;
}).filter('range', function() {
return function(input, total) {
total = parseInt(total);
for (var i=0; i<total; i++) {
input.push(i);
}
return input;
};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<table border="1">
<thead>
<tr>
<td>Question</td>
<td ng-repeat="x in names">{{ x.college }}</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="n in [] | range:4">
<td>
{{ names[0].program_section[n].question }}
</td>
<td width="100px" ng-repeat="x in names">
{{ x.program_section[n].total }}
</td>
</tr>
</tbody>
</table>
</div>
&#13;
期望的结果
<table width="426" border="1">
<tr>
<td width="79">Question</td>
<td >BSCS</td>
<td >BBA</td>
<td >Account</td>
<td >Total</td>
</tr>
<tr>
<td>Q1</td>
<td>135</td>
<td>175</td>
<td>145</td>
<td width="50" >455</td>
</tr>
<tr>
<td>Q2</td>
<td>142</td>
<td>142</td>
<td>162</td>
<td width="50" >446</td>
</tr>
<tr>
<td>Q3</td>
<td>135</td>
<td>165</td>
<td>125</td>
<td width="50" >425</td>
</tr>
<tr>
<td>Q4</td>
<td>137</td>
<td>137</td>
<td>117</td>
<td width="50" >391</td>
</tr>
</table>
&#13;
答案 0 :(得分:1)
您可以只计算控制器中的总数并使用它来生成剩余列:
$scope.totals = {}
arr.map(function(obj){
obj.program_section.map(function(section){
$scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
})
})
此处总计[Qx]将包含Qx的总计,我们在视图中使用它:
var arr = [
{
"unique_id": "CS",
"college": "BSCS",
"arr_length": 1,
"program_section": [
{
"question": "Q1",
"total": 135,
},
{
"question": "Q2",
"total": 142,
},
{
"question": "Q3",
"total": 135,
},
{
"question": "Q4",
"total": 137,
},
]
},
{
"unique_id": "MBA",
"college": "BBA",
"arr_length": 2,
"program_section": [
{
"question": "Q1",
"total": 175,
},
{
"question": "Q2",
"total": 142,
},
{
"question": "Q3",
"total": 165,
},
{
"question": "Q4",
"total": 137,
},
]
},
{
"unique_id": "CA",
"college": "Account",
"arr_length": 1,
"program_section": [
{
"question": "Q1",
"total": 145,
},
{
"question": "Q2",
"total": 162,
},
{
"question": "Q3",
"total": 125,
},
{
"question": "Q4",
"total": 117,
},
]
}
];
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.names = arr;
$scope.totals = {}
arr.map(function(obj){
obj.program_section.map(function(section){
$scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
})
})
}).filter('range', function() {
return function(input, total) {
total = parseInt(total);
for (var i=0; i<total; i++) {
input.push(i);
}
return input;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<table border="1">
<thead>
<tr>
<td>Question</td>
<td ng-repeat="x in names">{{ x.college }}</td>
<td>Totals</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="n in [] | range:4">
<td>
{{ names[0].program_section[n].question }}
</td>
<td width="100px" ng-repeat="x in names" >
{{ x.program_section[n].total }}
</td>
<td width="50%" ng-bind="totals[names[0].program_section[n].question]"></td>
</tr>
</tbody>
</table>
</div>