Angular ng-重复排序对象的对象
Angular ng-repeat sorting object of objects
$scope.openingtimes= {
"1": {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"5": {"id": 5, "day": 1, "name_of_day": "Pondělíadas", "open_from": "13:00", "open_to": "16:02"},
"7": {"id": 7, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"9": {"id": 9, "day": 1, "name_of_day": "Pondělísf", "open_from": "13:00", "open_to": "16:00"},
"10": {"id": 10, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"11": {"id": 11, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"12": {"id": 12, "day": 1, "name_of_day": "Pondělí", "open_from": "13:02", "open_to": "16:00"},
"13": {"id": 13, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"}
};
排序有问题,因为在视图中:
<div ng-repeat="time in openingTimes >
{{ time.id }}
</div>
我得到的顺序是:1、10、11、12、13、5、7、9,这当然是错误的。我不能更改对象,它必须是相同的我知道解决方案是这样的:
$scope.openingtimes= {
1: {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
5: {"id": .....
但是我做不到。我试过类似的东西:
<div ng-repeat="time in openingTimes | orderBy:'id*1'">
但它不起作用。有谁知道如何让它像数字一样排序吗?非常感谢你。
我终于找到了解决方案,这就是 pankajparkar 所说的过滤器:
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
和VIEW:
<div ng-repeat="time in openingTimes | orderObjectBy:'id':false">
感谢帮助!
$scope.openingtimes= {
"1": {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"5": {"id": 5, "day": 1, "name_of_day": "Pondělíadas", "open_from": "13:00", "open_to": "16:02"},
"7": {"id": 7, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"9": {"id": 9, "day": 1, "name_of_day": "Pondělísf", "open_from": "13:00", "open_to": "16:00"},
"10": {"id": 10, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"11": {"id": 11, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
"12": {"id": 12, "day": 1, "name_of_day": "Pondělí", "open_from": "13:02", "open_to": "16:00"},
"13": {"id": 13, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"}
};
排序有问题,因为在视图中:
<div ng-repeat="time in openingTimes >
{{ time.id }}
</div>
我得到的顺序是:1、10、11、12、13、5、7、9,这当然是错误的。我不能更改对象,它必须是相同的我知道解决方案是这样的:
$scope.openingtimes= {
1: {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
5: {"id": .....
但是我做不到。我试过类似的东西:
<div ng-repeat="time in openingTimes | orderBy:'id*1'">
但它不起作用。有谁知道如何让它像数字一样排序吗?非常感谢你。
我终于找到了解决方案,这就是 pankajparkar 所说的过滤器:
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
和VIEW:
<div ng-repeat="time in openingTimes | orderObjectBy:'id':false">
感谢帮助!