0
我已将自己编码到一个角落,并且无法计算出如何计算小计(每种作物)和总计(所有作物)。我现在有硬编码的预期值,但需要弄清楚如何计算它们。AngularJS:小计和总计
我正在使用budget.json模拟对工厂数据库的调用(在budget.js中定义)。 BudgetsController也在budget.js中定义。
硬编码总数从budgets.js的第35行开始。我尝试了几种LoDash方法来计算总数,但似乎无法找到我可以为每种作物复制的模式,并且我知道“总计”总数将遵循相同的模式,但仅使用小计。
任何帮助表示赞赏!
budget.js代码:
(function(){
'use strict';
angular
.module('ARM')
.factory('ExpensesFactory', function ExpensesFactory(
$http, $q
) {
return {
getBudget: getBudget
};
function getBudget(id){
return $http.get('budget.json');
}
})
.controller('BudgetsController', BudgetsController);
BudgetsController.$inject = ['$scope', 'ExpensesFactory'];
function BudgetsController(
$scope, ExpensesFactory
){
ExpensesFactory.getBudget('1')
.then(function success(rsp){
var arr = rsp.data;
var flattened = _.flatten(arr);
var grped = _.groupBy(flattened, function(item) {
return item.crop;
});
$scope.uses = grped;
//TODO: Crop and Loan Budget Totals
$scope.uses.totals = [
//CORN
[
{
"arm": 178,
"dist": 197.91,
"other": 115,
"peracre": 490.91,
"calc_arm": 61837.2,
"calc_dist": 68753.934,
"calc_other": 39951,
"calc_total": 170542.134
}
],
//SOYBEANS
[
{
"arm": 145,
"dist": 69.73,
"other": 74.35,
"peracre": 289.08,
"calc_arm": 84143.5,
"calc_dist": 40464.319,
"calc_other": 43145.305,
"calc_total": 167753.124
}
],
//SORGHUM
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//WHEAT
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//COTTON
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//RICE
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//PEANUTS
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//SUGAR CANE
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 0,
"calc_dist": 0,
"calc_other": 0,
"calc_total": 0
}
],
//TOTALS
[
{
"arm": 0,
"dist": 0,
"other": 0,
"peracre": 0,
"calc_arm": 155999,
"calc_dist": 36530,
"calc_other": 87223,
"calc_total": 279752
}
]
];
var uniqExp = _.uniq(_.pluck(flattened, 'expense'));
$scope.exp = uniqExp;
});
} // end BudgetsController fn
})();
你能解释一下你的过去和目前使用的? – jgravois 2015-02-08 23:35:53
GREAT for SubTotals(http://plnkr.co/edit/fSZ7wUzySDNm1yqhxb2x?p=preview),但我不知道如何获得TOTALS现在我有SubTotals。我以为我可以做同样的事情,但传递$ scope.uses.total但这不适合我。 – jgravois 2015-02-08 23:58:26
你几乎到了那里,但是你应该在你的脑海中建模(或逐步调试)为什么在每个reduce()周围使用map()。在总数的情况下,你只需要减少步骤。最后一点,避免将总数设置为数组的属性,直接将其保存到$ scope。 http://plnkr.co/edit/GeYpw7c0d6I3fgXy7lFF?p=preview – 2015-02-09 00:21:56