Angular orderBy子值

时间:2015-10-19 06:06:04

标签: angularjs angularjs-ng-repeat angularjs-orderby

我有以下对象,我正在重复,我想按' pricing.total'排序。

 "data":{  
  "12654fcd":{  
     "sequenceNumber":"12654fcd",
     "directionInd":"OneWay",
     "journey":[  ],
     "pricing":{
          "total":"1200.79"
     },
     "breakdown":{  },
     "validatingCarrier":"DL"
  },
  "1eb562ab":{  
     "sequenceNumber":"1eb562ab",
     "directionInd":"OneWay",
     "journey":[  ],
     "pricing":{
          "total":"1400.80"
     },
     "breakdown":{  },
     "validatingCarrier":"DL"
  },
}

这是输出:

 <div class="row" data-ng-repeat="itinerary in results.data.data |   orderBy:'pricing.total'">

我的重复工作正常,但是我试图通过pricing.total对输出进行排序而没有任何成功。

我该怎么做呢?甚至可以对子值进行排序吗?

干杯,

2 个答案:

答案 0 :(得分:1)

$ scope.by_pricing_total = function(it){return it.pricing.total} 然后 ORDERBY:by_pricing_total

angular.module('orderByExample', [])
  .controller('ExampleController', ['$scope',
    function($scope) {
      $scope.friends = [{
        name: 'John',
        phone: '555-1212',
        age: 10,
        data: {
          a: 57
        }
      }, {
        name: 'Mary',
        phone: '555-9876',
        age: 19,
        data: {
          a: 53
        }
      }, {
        name: 'Mike',
        phone: '555-4321',
        age: 21,
        data: {
          a: 51
        }
      }, {
        name: 'Adam',
        phone: '555-5678',
        age: 35,
        data: {
          a: 53
        }
      }, {
        name: 'Julie',
        phone: '555-8765',
        age: 29,
        data: {
          a: 52
        }
      }];

      $scope.getDataA = function(it) {
        console.log(it.data);
        return it.data.a;
      }
    }
  ]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h1>Hello Plunker!</h1>
<div ng-app="orderByExample" ng-controller="ExampleController">
  <table class="friend">
    <tbody>
      <tr>
        <th>Name</th>
        <th>Phone Number</th>
        <th>Age</th>
      </tr>
      <tr ng-repeat="friend in friends | orderBy:getDataA">
        <td>{{friend.name}}</td>
        <td>{{friend.phone}}</td>
        <td>{{friend.age}}</td>
      </tr>
    </tbody>
  </table>
</div>

答案 1 :(得分:1)

  

<强> ORDERBY    - 过滤模块ng

     

通过表达式谓词对指定的数组进行排序。这是订购   字母表中的字符串和数字上的数字。注意:如果你   通知编号未按预期排序,请确保它们是   实际上被保存为数字而不是字符串。

仅按数组排序。你的不是数组,它的对象。

将您的数据结构更改为

"data":[  
  {  
     "sequenceNumber":"12654fcd",
     "directionInd":"OneWay",
     "journey":[  ],
     "pricing":{
          "total":"1200.79"
     },
     "breakdown":{  },
     "validatingCarrier":"DL"
  },
  {  
     "sequenceNumber":"1eb562ab",
     "directionInd":"OneWay",
     "journey":[  ],
     "pricing":{
          "total":"1400.80"
     },
     "breakdown":{  },
     "validatingCarrier":"DL"
  },
]