2015-10-18 66 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

完美!奇迹般有效。谢谢 :) –