2015-10-19 79 views
0

我有下面的对象,我正在重复,我想按'pricing.total'进行排序。角度orderBy子值

"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输出排序没有任何成功。

我该怎么做呢?是否有可能实现一个子值的排序?

干杯,

+0

可能的问题是该行程不包裹在一个阵列?如[{},{},{}]中所示?我现在无法更改输出格式,因为它来自API。 – Dbrandt

回答

1

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.

订单仅适用于数组。你的不是数组,它的对象。

改变你的数据结构来

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

这是我刚才的想法,谢谢:) – Dbrandt

1

$ scope.by_pricing_total =功能(它){返回it.pricing.total} 然后 排序依据: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>

+0

看起来像它可以工作,但抛出“语法错误:令牌'{'是一个意外的标记” – Dbrandt

+0

修改我的答案,这应该工作 –

+0

$ scope.byPricingTotal =函数(行程){ console.log(itinerary.pricing 。总); return itinerary.pricing.total; } 然后orderBy:byPricingTotal没有任何console.logs显示,所以我猜测它没有被调用。 – Dbrandt