AngularJs 动态 orderBy 表达式

AngularJs dynamic orderBy expression

下面是代码。我嵌套了 ng-repeat 和一个包含所有其他字段总和的字段。

<div class="animate margin-top" >
        <div layout="row" layout-align="center"  ng-repeat="data in leaderBoardData | orderBy: 'getTotal($index)'" style = "padding: 10px;" class= "md-padding">
            <section class="text-center width-20"><a>{{data.handleName}}</a></section>
            <section class="text-center width-20" ng-repeat="score in data.score track by $index">{{score}}</section>
            <section class="text-center width-20">{{getTotal($index)}}</section>
        </div>
</div>

所以想按照动态字段排序getTotal($index)。我应该怎么做?上述 orderBy 无效。

下面是 getTotal() 函数的代码

$scope.getTotal = function (index) {
    var total = 0
    $scope.leaderBoardData[index].score.forEach(function (score) {
        total = total + score
    })
    return total
}

下面是排行榜数据

var leaderBoardData = [ { handleName: 'xyz', score: [1,2,3] },{ handleName: 'acc', score: [4,5,6] } ]

我猜 $scope.leaderBoardData 不是数组(它是对象的对象),而且 orderBy 过滤器只适用于数组。如果你把 $scope.leaderBoardData 做成一个数组,它就可以工作

我猜你没有正确使用 forEach。

    $scope.getTotal = function (index) {
    var total = 0;
    angular.forEach($scope.leaderBoardData,function(score,index){
        total = total + score
    })
    return total;
}

试试这个,它应该有效。

  1. 删除 orderBy 表达式的引号,所以 angular 知道它是一个函数,你不需要显式传递 $index 作为参数。

  2. 您需要将此 {{getTotal($index)}} 更改为 {{getTotal(data)}},因为数据已在此处迭代。

所以,基本上:

function Ctrl($scope) {

  $scope.leaderBoardData = [{
    handleName: 'xyz',
    score: [4, 5, 6]
  }, {
    handleName: 'acc',
    score: [1, 2, 3]
  }, {
    handleName: 'acFc',
    score: [1, 2, 4]
  }];

  $scope.getTotal = function(index) {
    var total = 0;
    // also change to index as array already passed to function
    index.score.forEach(function(score) {
      total = total + score;
    })
    return total;
  };
}
<!DOCTYPE html>
<html ng-app>

<head></head>

<body>
  <div>
    <div ng-controller="Ctrl" class="animate margin-top">
      <div layout="row" layout-align="center" ng-repeat="data in leaderBoardData | orderBy: getTotal" style="padding: 10px;" class="md-padding">
        <section class="text-center width-20">
          <a>{{data.handleName}}</a>
        </section>
        <section class="text-center width-20" ng-repeat="score in data.score track by $index">{{score}}</section>
        <section class="text-center width-20">
          <!-- change is here -->
          {{getTotal(data)}}
        </section>
      </div>
    </div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</body>

</html>

你可以这样做。 HTML

<div layout="row" layout-align="center"  ng-repeat="data in leaderBoardData | orderBy: 'totalScore'" style = "padding: 10px;" class= "md-padding">
            <section class="text-center width-20"><a>{{data.handleName}}</a></section>
            <section class="text-center width-20" ng-repeat="score in data.score track by $index">{{score}}</section>
            <section class="text-center width-20">total score={{data.totalScore}}</section>
        </div>

JS :

$scope.leaderBoardData =  [ { handleName: 'xyz', score: [111,21,3] },{ handleName: 'acc', score: [14,5,16] } ];

     $scope.leaderBoardData.forEach(function(oneRecord,key){
        var total=0;
        oneRecord.score.forEach(function(oneEle,key){
            total = total + oneEle

        })
        oneRecord.totalScore = total;

     })