AngularJs动态orderBy表达式

时间:2015-07-09 10:40:56

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

以下是代码。我有嵌套的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)对其进行排序。我该怎么办?上面的订单不起作用。

以下是getTotal()功能

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

以下是leaderboardData

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

4 个答案:

答案 0 :(得分:1)

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

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

  3. 所以,基本上:

    &#13;
    &#13;
    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;
      };
    }
    &#13;
    <!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>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

我猜测$ scope.leaderBoardData不是一个数组(它是一个对象的对象),而orderBy过滤器只适用于数组。如果你使$ scope.leaderBoardData成为一个数组,它将起作用

答案 2 :(得分:0)

我猜你没有正确使用forEach。

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

试试这个,它应该有效。

答案 3 :(得分:0)

你可以这样做。 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;

     })