2017-04-04 32 views
1

我需要使用兩個具有Angular ng-repeat的API生成如下所示的表格?如何做到這一點如何使用JSON映射響應生成Angular ng表格

+---------------+---------------------+------------+------------+ 
| Continent | Continent Shortcode | City | Population | 
+---------------+---------------------+------------+------------+ 
| Asia   | AS     | Beijing | 21.5  | 
|    |      | Manila  | 1.6  | 
| Europe  | EU     | London  | 8.6  | 
|    |      | Paris  | 2.2  | 
| North America | NA     | Washington | 7   | 
| South America | SA     | Brasilia | 2.5  | 
| Australia  | AU     | Canberra | 0.3  | 
+---------------+---------------------+------------+------------+ 

REST端點getCityStatsByContinent(continentId)? - > /API /統計/城市/列表大陸= 100,200,300,400,500

{ 
    "100" : [{ 
      "city" : "Beijing", 
      "population" : 21.5m 
     }, { 
      "city" : "Manila", 
      "population" : 1.6m 
     } 
    ], 
    "200" : [{ 
      "city" : "London", 
      "population" : 8.6m 
     }, { 
      "city" : "Paris", 
      "population" : 2.2m 
     } 
    ], 
    "300" : [{ 
      "city" : "Washington", 
      "population" : 7m 
     } 
    ], 
    "400" : [{ 
      "city" : "Brasilia", 
      "population" : 2.5m 
     } 
    ], 
    "500" : [{ 
      "city" : "Canberra", 
      "population" : 0.3m 
     } 
    ], 
} 

getContinetDetails() REST端點 - >/API /大洲

[ 
    { 
     "id" : 100, 
     "code" : "AS", 
     "name" : "Asia" 
    }, { 
     "id" : 200, 
     "code" : "EU", 
     "name" : "Europe" 
    }, { 
     "id" : 300, 
     "code" : "NA", 
     "name" : "North America" 
    }, { 
     "id" : 400 
     "code" : "SA", 
     "name" : "South America" 
    }, { 
     "id" : 500, 
     "code" : "AU", 
     "name" : "Australia" 
    } 
] 

我只能想到從下面的代碼開始(假設第一個API響應存儲在citiesByContinentId中)。但我堅持的東西應該是解決從/ API的continentId正確的方法/大洲

<tr ng-repeat ="(continentId, cityStats) in citiesByContinentId"> 
    <td>{{ continentId }}</td> 
<tr> 

回答

1

angular.module('app', []) 
 
    .controller('sampleCtrl', function($scope) { 
 
    $scope.ctiyPopulation = { 
 
     "100": [{ 
 
     "city": "Beijing", 
 
     "population": "21.5 m" 
 
     }, { 
 
     "city": "Manila", 
 
     "population": "1.6 m" 
 
     }], 
 
     "200": [{ 
 
     "city": "London", 
 
     "population": "8.6 m" 
 
     }, { 
 
     "city": "Paris", 
 
     "population": "2.2 m" 
 
     }], 
 
     "300": [{ 
 
     "city": "Washington", 
 
     "population": "7 m" 
 
     }], 
 
     "400": [{ 
 
     "city": "Brasilia", 
 
     "population": "2.5 m" 
 
     }], 
 
     "500": [{ 
 
     "city": "Canberra", 
 
     "population": "0.3 m" 
 
     }] 
 
    }; 
 

 
    $scope.continentDetails = [{ 
 
     "id": 100, 
 
     "code": "AS", 
 
     "name": "Asia" 
 
    }, { 
 
     "id": 200, 
 
     "code": "EU", 
 
     "name": "Europe" 
 
    }, { 
 
     "id": 300, 
 
     "code": "NA", 
 
     "name": "North America" 
 
    }, { 
 
     "id": 400, 
 
     "code": "SA", 
 
     "name": "South America" 
 
    }, { 
 
     "id": 500, 
 
     "code": "AU", 
 
     "name": "Australia" 
 
    }]; 
 
    });
table { 
 
    width: 100% 
 
} 
 

 
td, 
 
th { 
 
    border: 1px solid #eee; 
 
    padding:5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="sampleCtrl"> 
 
    <table cellpadding="0" cellspacing="0"> 
 
    <thead> 
 
     <tr> 
 
     <th>Continent</th> 
 
     <th>Continent Shortcode</th> 
 
     <th>City</th> 
 
     <th>Population</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody ng-repeat="continent in continentDetails"> 
 
     <tr ng-repeat="city in ctiyPopulation[continent.id]"> 
 
     <td><span ng-if="$index===0">{{continent.name}}</span></td> 
 
     <td><span ng-if="$index===0">{{continent.code}}</span></td> 
 
     <td>{{city.city}}</td> 
 
     <td>{{city.population}}</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

假設$ scope.continentDetails返回100K記錄,將在不影響性能在100K記錄上重複執行。這是唯一的解決方案嗎?還是應該解決這個問題? – FakirTrappedInCode

+0

在這種情況下,您需要爲記錄實現**分頁**和**單個綁定('{{:: city.city}}')**。 –

相關問題