对象的角度ng重复排序对象

时间:2015-07-05 10:09:34

标签: angularjs sorting sql-order-by

$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'">

但它不起作用。 Doues有谁知道怎么让它像数字一样?非常感谢你。

1 个答案:

答案 0 :(得分:2)

我终于找到了解决方案,这就是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">

感谢您的帮助!