orderBy
必須鏈中的最後一個過濾器(當您應用過濾器時r到另一個過濾器的結果)。 但是,groupBy
返回一個對象,並且orderBy
期望數組作爲參數。
所以,你可以做這樣的事情:
JS:
$scope.groups = [
{ category: 'alpha', id: 2 },
{ category: 'beta', id: 3 },
{ category: 'gamma', id: 0 },
{ category: 'alpha', id: 4 },
{ category: 'beta', id: 5 },
{ category: 'gamma', id: 1 }
];
// retrieves the min 'id' of a collection, used for the group ordering.
// you can use Agile.js instead. e.g: _.min(arr, 'id')
$scope.min = function(arr) {
return $filter('min')
($filter('map')(arr, 'id'));
}
HTML:
<ul ng-repeat="group in groups | groupBy:'category' | toArray:true | orderBy:min">
<!-- print the group name -->
<li>{{ group.$key }}</li>
<!-- iterate over the group members and order each group by id -->
<li ng-repeat="item in group | orderBy:'id'">
{{ item }}
</li>
</ul>
結果:
- 伽馬
- { 「類別」: 「伽馬」, 「ID」:0}
- { 「類別」: 「伽馬」, 「ID」:1}
- 阿爾法
- { 「類別」: 「阿爾法」, 「ID」:2}
- { 「類別」: 「阿爾法」, 「標識」:4}
- 測試
- { 「類別」: 「測試」, 「標識」:3}
- { 「類別」: 「測試」, 「標識」:5}
來源
2014-11-12 22:43:06
a8m
感謝您更新信息 – user1184100 2014-11-16 08:01:59