2015-10-18 64 views
1

我有一個名爲World.json的大文件,其中包含世界各地所有國家的大量數據。我一直在使用一個列表來顯示錶中的所有數據。我想按順序對所有值進行排序。但問題是,我所有的int值都是world.json中的字符串。我知道,我需要使用parseInt函數,使其工作,但 嘗試一切沒有任何成功將字符串錶轉換爲int範圍

只是和示例代碼World.js

"Country": [ 
{ 
    "Code": "AFG", 
    "Name": "Afghanistan", 
    "Continent": "Asia", 
    "Region": "Southern and Central Asia", 
    "SurfaceArea": "652090.00", 
    "IndepYear": "1919", 
    "Population": "22720000", 
    "LifeExpectancy": "45.9", 
    "GNP": "5976.00", 
    "GNPOld": "NULL", 
    "LocalName": "Afganistan/Afqanestan", 
    "GovernmentForm": "Islamic Emirate", 
    "HeadOfState": "Mohammad Omar", 
    "Capital": "1", 
    "Code2": "AF" 
}, 
{ 

controller.js

countryApp.controller('ctrlMain', function($scope, $http) { 
    $http.get('world.json').success(function(data){ 
    $scope.world = data; 
    }); 

    }); 

countryApp.controller('detailsCtrl', function($scope, $routeParams){ 
$scope.name = $routeParams.countryName; 
}); 

all.html

<td class="heading"><a href="" ng-click="reverse = predicate == 'LifeExpectancy' && !reverse; predicate = 'LifeExpectancy'">Befolkningsm&aumlngd</a></td> 

<tr ng-repeat="country in world.Country | filter: { Continent: vDel } | filter:search | orderBy: predicate:reverse"> 
    <td>{{ country.Population }}</td> 
</tr> 
+0

如果它的選項我會更改JSON文件。如果您的數據是按邏輯鍵入的,則應將其存儲爲鍵入的數據。 –

+0

至少在將數據附加到範圍之前鍵入數據。 –

+0

我寧願不改變json文件,因爲它太龐大了:D –

回答

0

有幾種方法可以做你正在尋找的東西。最好的辦法是在服務器上更新你的JSON。如果這不是一個選項,這個更新你的ng-repeat將會有所斬獲。

<tr ng-repeat="country in world.Country | filter:search | orderBy:'Population*1':reverse"> 
    <td>{{ country.Population | number}}</td> 
</tr> 

我已經設置了顯示這個工作一plunker在orderBy

http://plnkr.co/edit/ocD0TskHAqULHUddDPku?p=preview

編輯:更新plunker所以反向工程。

+0

完美!奇蹟般有效。謝謝 :) –