2014-09-25 29 views
0

我嘗試它包含以下JSON響應排序依據單JSON對象NG重複

{ 
    "Title": "Harry Potter and the Sorcerer's Stone", 
    "Year": "2001", 
    "Rated": "PG", 
    "Released": "16 Nov 2001", 
    "Runtime": "152 min", 
    "Genre": "Adventure, Family, Fantasy", 
    "Director": "Chris Columbus", 
    "Writer": "J.K. Rowling (novel), Steve Kloves (screenplay)", 
    "Actors": "Richard Harris, Maggie Smith, Robbie Coltrane, Saunders Triplets", 
    "Plot": "Rescued from the outrageous neglect of his aunt and uncle, a young boy with a great destiny proves his worth while attending Hogwarts School of Witchcraft and Wizardry.", 
    "Language": "English", 
    "Country": "UK, USA", 
    "Awards": "Nominated for 3 Oscars. Another 15 wins & 58 nominations.", 
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNTM5NDkzNV5BMl5BanBnXkFtZTYwODQ4MzY5._V1_SX300.jpg", 
    "Metascore": "64", 
    "imdbRating": "7.4", 
    "imdbVotes": "344,737", 
    "imdbID": "tt0241527", 
    "Type": "movie", 
    "Response": "True" 
} 

我遍歷鍵和下方值碼:

<div class="card" ng-repeat="(key,value) in movie"> 
    <div class="item item-divider positive"> 
     {{ key }} 
    </div> 
    <div class="item item-text-wrap"> 
     {{ value }} 
    </div> 
</div> 

這是導致項目以按鍵[演員,獎項...]的升序顯示。如何在不更改訂單的情況下顯示它?

+0

所以這將是你的標準進行排序? – meriadec 2014-09-25 13:41:33

+2

[ng-repeat指令在使用(key,value)時對數據進行排序的可能的重複](http://stackoverflow.com/questions/19676694/ng-repeat-directive-sort-the-data-when-using-key - 值) – sma 2014-09-25 13:44:56

+0

@meriadec按鍵在json對象的響應中設置的順序:「標題」,「年」,「額定」等。這是一個有趣的問題,我從來沒有遇到過像這之前 – Josep 2014-09-25 13:44:58

回答

0

你可以嘗試這樣的:

function demo ($scope) { 
 
    
 
    var data = { 
 
    "Title": "Harry Potter and the Sorcerer's Stone", 
 
    "Year": "2001", 
 
    "Rated": "PG", 
 
    "Released": "16 Nov 2001", 
 
    "Runtime": "152 min", 
 
    "Genre": "Adventure, Family, Fantasy", 
 
    "Director": "Chris Columbus", 
 
    "Writer": "J.K. Rowling (novel), Steve Kloves (screenplay)", 
 
    "Actors": "Richard Harris, Maggie Smith, Robbie Coltrane, Saunders Triplets", 
 
    }; 
 
    
 
    var order = ['Title', 'Year', 'Rated', 'Released', 'Runtime', 'Genre', 'Director', 'Writer', 'Actors']; 
 
    
 
    function setOrder (obj) { 
 
    var out = []; 
 
    order.forEach(function (key) { 
 
     out.push({ title: key, value: obj[key] }); 
 
    }); 
 
    return out; 
 
    } 
 
    
 
    $scope.items = setOrder(data); 
 
    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app ng-controller="demo"> 
 

 
    <table> 
 
    <tr ng-repeat="item in items"> 
 
     <td>{{ item.title }}</td> 
 
     <td>{{ item.value }}</td> 
 
    </tr> 
 
    </table> 
 

 
</div>

+0

哇!謝謝。這是一個奇怪的問題。 – Tejas 2014-09-25 13:51:43

+1

np :)寧願總是使用'ngRepeat'的tableau,它對排序,過濾等更友好。 – meriadec 2014-09-25 14:22:53