Angular orderBy子值

Angular orderBy sub value

我正在重复以下对象,我想按 '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 对输出进行排序但没有成功。

我该怎么做?甚至有可能实现对子值的排序吗?

干杯,

$scope.by_pricing_total = 函数(它){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>

orderBy - filter in module ng

Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers. Note: if you notice numbers are not being sorted as expected, make sure they are actually being saved as numbers and not strings.

Order by 仅适用于数组。你的不是数组,它的对象。

将您的数据结构更改为

"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"
  },
]