我有桌子,其中我有2个差异。每个国家/地区的价值(按月计算),当我在选择框中选择每个国家/地区时它工作正常但是当我选择“全部”选项时,我需要在所有国家/地区(月份)中包含所有值的总和.i.e。 {{month.A}}应该是46(JAN),56(FEB)和{{month.S}}应该是38(JAN)和28(FEB)。
另请指导我选择框中没有显示所有选项。 小提琴是https://jsfiddle.net/tffv2owp/
$scope.months =
[{ "country": "UK", "mon": "JAN", "A": "14", "S": "2"},
{ "country": "AUSTRIA", "mon": "JAN", "A": "24", "S": "12"},
{ "country": "ITALY", "mon": "JAN", "A": "5", "S": "21"},
{ "country": "SWIZ", "mon": "JAN", "A": "3", "S": "3"},
{ "country": "UK", "mon": "FEB", "A": "4", "S": "12"},
{ "country": "AUSTRIA", "mon": "FEB", "A": "24", "S": "12"},
{ "country": "ITALY", "mon": "FEB", "A": "15", "S": "1"},
{ "country": "SWIZ", "mon": "FEB", "A": "13", "S": "3"}
];
答案 0 :(得分:1)
维护过滤器函数中的值的总和。如果" ALL"提供,然后重新计算聚合(或如果这些可能保持相同计算一次和缓存)。 Here是每次应用过滤器时重新计算的示例。而重要的补充:
// Add an "ALL" value to countries
$scope.countries = ["ALL", "UK", "AUSTRIA", "ITALY", "SWIZ"];
// ...
app.filter('filterMultiple',['$filter',function ($filter) {
// maintain singleton aggregate to prevent max $digest cycle
// each month would go here -- start each value (e.g. A, S) at 0
var aggregateItems = [
{mon: 'JAN', A: 0, S: 0},
{mon: 'FEB', A: 0, S: 0}
];
return function (items, keyObj) {
var filterObj = {
/* filterObj remains same */
};
// check for 'ALL' Selection
if (keyObj && keyObj.country === 'ALL') {
var aggs = {};
// 0 out aggregate values to recalculate
angular.forEach(aggregateItems, function (obj) {
obj.A = 0;
obj.S = 0;
// reverse lookup by month
aggs[obj.mon] = obj;
});
// aggregate months / items
angular.forEach(items, function (obj) {
aggs[obj.mon].A = aggs[obj.mon].A + (parseInt(obj.A, 10) || 0);
aggs[obj.mon].S = aggs[obj.mon].S + (parseInt(obj.S, 10) || 0);
});
// totals calculated
return aggregateItems;
} else if(keyObj){
// Your original logic
}
return filterObj.filteredData;
});
答案 1 :(得分:0)