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