gpt4 book ai didi

javascript - Angular : total of all country in each row when select option "ALL"

转载 作者:行者123 更新时间:2023-11-28 00:02:05 25 4
gpt4 key购买 nike

我有一个表,其中有 2 个差异。每个国家/地区的值(按月),当我在选择框中选择每个国家/地区时,它工作正常,但是当我选择“全部”选项时,我需要所有国家/地区(按月)包含的所有值的总和。即{{month.A}} 应为 46(JAN)、56(FEB),{{month.S}} 应为 38(JAN) 和 28(FEB)。

另外,请指导我,因为所有选项在选择框中都没有很好地显示。 fiddle 是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"}
];

最佳答案

维护过滤函数中值的聚合。如果提供“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;
});

关于javascript - Angular : total of all country in each row when select option "ALL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com