2017-02-17 5 views
1

У меня есть многомерный массив, который я обрабатываю JSON, и я хотел бы «заказать» в AngularJS. и я получаю эту ошибку: Ошибка: [OrderBy: notarray] Ожидаемый массив, но получил: 13AngularJS Многомерный массив Order By

<div ng-repeat="x in thread track by x.Id | orderBy: x.Id"> 

Вот мой JSON:

{ 
    "13": { 
    "Id": 16, 
    "LINK_Id": 16, 
    "Attachments": [ 
     { 
     "AttachmentName": "Attachment.jpeg", 
     "AttachmentLinkId": 6, 
     "LinkType": "Thread", 
     "ThreadId": 20 
     }, 
     { 
     "AttachmentName": "Attachment.txt", 
     "AttachmentLinkId": 7, 
     "LinkType": "Thread", 
     "ThreadId": 20 
     } 
    ] 
    }, 
    "16": { 
    "Id": 16, 
    "LINK_Id": 169 
    }, 
    "19": { 
    "Id": 19, 
    "LINK_Id": 112 

} }

Если кто-то может помочь Это будет высоко ценится.

Большое спасибо,

ответ

0

Есть некоторые ограничения и правила при использовании OrderBy Go through this. Iterating an Object of Objects (Acting as an Associative Array or Hash) Это название - это то, чего вы пытаетесь достичь.

0

Заменить:

<div ng-repeat="x in thread track by x.Id | orderBy: x.Id"> 

С:

<div ng-repeat="x in thread | orderBy: 'Id' track by x.Id"> 
0

Я бы не сортирует массив в пределах $ объема (хотя бы потому, насколько я могу судить, вам не нужно сортировать он динамически), я бы сортировал его в контроллере с помощью метода сортировки встроенного Javascript (быстрее).

Например после того, как JSON, возвращается,

angular.module('sample-app', []) 
 

 
.controller('SampleController', ['$http', '$scope', function($http, $scope) { 
 

 
    $scope.test = 'Array Sort'; 
 

 
    $http.get('https://jsonplaceholder.typicode.com/posts/') 
 
    .then(function(result) { 
 
    
 
    // Order id DESC (change a and b position for ASC) 
 
    result.data.sort(function(a, b){ 
 
     return b.id - a.id 
 
    }); 
 
    
 
    // inject into scope 
 
    $scope.items = result.data; 
 
    }); 
 

 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="sample-app"> 
 
    <section ng-controller="SampleController"> 
 
    <h3>{{ test }}</h3> 
 
    <ul> 
 
     <li ng-repeat="item in items"> 
 
     {{ item.id }} 
 
     </li> 
 
    </ul> 
 
    </section> 
 
</div>

Смежные вопросы