Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:按分组列表之和排序_Javascript_Angularjs_Angular Filters - Fatal编程技术网

Javascript AngularJS:按分组列表之和排序

Javascript AngularJS:按分组列表之和排序,javascript,angularjs,angular-filters,Javascript,Angularjs,Angular Filters,我有一份不同球队的单场比赛结果列表,格式如下: [{team: "A", w: 1, l: 0, t: 0, date: '2018-10-01'}, {team: "B", w: 1, l: 0, t: 0, date: '2018-10-01'}, {team: "C", w: 0, l: 1, t: 0, date: '2018-10-01'}, {team: "D", w: 0, l: 1, t: 0, date: '2018-10-01'},

我有一份不同球队的单场比赛结果列表,格式如下:

    [{team: "A", w: 1, l: 0, t: 0, date: '2018-10-01'},
     {team: "B", w: 1, l: 0, t: 0, date: '2018-10-01'},
     {team: "C", w: 0, l: 1, t: 0, date: '2018-10-01'},
     {team: "D", w: 0, l: 1, t: 0, date: '2018-10-01'},
     {team: "A", w: 1, l: 0, t: 0, date: '2018-10-08'},
     {team: "B", w: 0, l: 1, t: 0, date: '2018-10-08'},
     {team: "C", w: 0, l: 0, t: 1, date: '2018-10-08'},
     {team: "D", w: 0, l: 0, t: 1, date: '2018-10-08'},
     {team: "A", w: 1, l: 0, t: 0, date: '2018-10-15'},
     {team: "D", w: 0, l: 0, t: 0, date: '2018-10-15'},
     {team: "B", w: 1, l: 0, t: 0, date: '2018-10-15'},
     {team: "B", w: 1, l: 0, t: 0, date: '2018-10-17'},
     {team: "B", w: 1, l: 0, t: 0, date: '2018-10-20'},
     {team: "C", w: 0, l: 1, t: 0, date: '2018-10-20'},
     {team: "C", w: 0, l: 1, t: 0, date: '2018-10-22'}]
使用
ng repeat
angular.filter
我现在能够在按团队分组的表格中显示该数据。使用我还能够汇总比赛结果,为每支球队创造记录:

Rank    Team    Games   Wins    Losses  Ties
1       A       3       3       0       0
2       D       3       0       1       1
3       C       4       0       3       1
4       B       5       4       1       0
到目前为止我所得到的

此外,我在视图中放置了一个按钮,用户可以从中选择日期范围。更改这些日期后,将更新聚合。这也已经在起作用

我现在的问题是,我无法按显示的任何类别对表进行排序。我尝试了不同的方法,比如使用任何可以想象的
orderBy:
子句。我还尝试将聚合结果放入变量(如
{{wins=reduce(group,'w')}}
)中,以便在
orderBy:
中引用,但没有效果


我想实现的可能吗?

一个解决方案是利用这样一个事实:您可以将函数作为表达式参数传递到
orderBy
。在该函数中,您可以为每个属性计算组中所有项的总和,然后将结果用作排序依据的值

对HTML的调整:

ng-repeat="group in data | groupBy: 'team' | toArray:true | orderBy:category"
以及实现这一目标的相关JS:

// $scope.order is a user option to specify the property to order by
$scope.category = function (value) {
    // The games column is not a data item property so it is treated specially
    if ($scope.order === 'games') {
        return value.length;
    }
    return value.reduce(function (total, dataItem) {
        return total + dataItem[$scope.order];
    }, 0);
};

这里有许多过时的做法,我觉得这让你更难接受。您应该在控制器内启动数据,这样您就不必依赖管道指令,然后通过单击列标题进行排序也同样简单:只需按赢或输或您所做的任何事情对数组进行排序。同意。在发送到视图之前,先映射/减少数据。感谢您的评论。这是我正在做的事情的缩小版,也许这就是它看起来有点尴尬的原因。你在控制器内启动数据是什么意思?我更新了小提琴来向你展示。它还不完整,但基本上你需要做的就是计算等级,然后把它放在它自己的属性上,也许“r”再次感谢,我会看一看。