use*_*332 11 html javascript json angularjs
我有以下数据作为JSON:
{
"Workout1": {
"Name": "First",
"Rounds": [
{
"Exercises": [
{
"Name": "Exercise1",
"Repeat": 10
},
{
"Name": "Exercise2",
"Repeat": 10
},
{
"Name": "Exercise3",
"Repeat": 10
}
]
},
{
"Exercises": [
{
"Name": "Exercise1",
"Repeat": 20
},
{
"Name": "Exercise2",
"Repeat": 20
},
{
"Name": "Exercise3",
"Repeat": 20
}
]
},
{
"Exercises": [
{
"Name": "Exercise1",
"Repeat": 30
},
{
"Name": "Exercise2",
"Repeat": 30
},
{
"Name": "Exercise3",
"Repeat": 30
}
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我想将它显示为带有angularjs和ng-repeat的html表.这样我得到下表:
<table class="table">
<tr>
<th>Round1</th>
<th>Round2</th>
<th>Round3</th>
</tr>
<tr>
<td>10 Exercise1</td>
<td>20 Exercise1</td>
<td>30 Exercise1</td>
</tr>
<tr>
<td>10 Exercise2</td>
<td>20 Exercise2</td>
<td>30 Exercise2</td>
</tr>
<tr>
<td>10 Exercise3</td>
<td>20 Exercise3</td>
<td>30 Exercise3</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
用于表格预览:http: //jsfiddle.net/54pD8/
我的问题是html表是基于行的.我可以通过我的轮次进行ng-repeat迭代,然后通过我的练习进行迭代但是为了创建一个表格,我需要每个练习的第一个,然后是每个练习的第二个,依此类推.
有人可以帮我解决这个问题吗?
PS.如果您对json中的这些数据有更好的布局,我们欢迎您的建议,我是json(和angularjs)的新手.
Ton*_*nin 29
您正在寻找的解决方案是在Angular的官方教程中.在本教程中,使用Angulars $ http服务从JSON文件加载电话.在下面的代码中,我们使用$ http.get来加载保存在phones目录中的phones.json文件:
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});
$scope.orderProp = 'age';
});
Run Code Online (Sandbox Code Playgroud)
然后我们遍历手机:
<table>
<tbody ng-repeat="i in phones">
<tr><td>{{i.name}}</td><td>{{$index}}</td></tr>
<tr ng-repeat="e in i.details">
<td>{{$index}}</td>
<td>{{e.foo}}</td>
<td>{{e.bar}}</td></tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
在普通表中使用简单的方法创建动态标题和单元格:
<table width="100%" class="table">
<thead>
<tr>
<th ng-repeat="(header, value) in MyRecCollection[0]">{{header}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in MyRecCollection | filter:searchText">
<td ng-repeat="cell in row">{{cell}}</td>
</tr>
</tbody>
</table>
MyApp.controller('dataShow', function ($scope, $http) {
//$scope.gridheader = ['Name','City','Country']
$http.get('http://www.w3schools.com/website/Customers_MYSQL.php').success(function (data) {
$scope.MyRecCollection = data;
})
});
Run Code Online (Sandbox Code Playgroud)
JSON数据:
[{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
}, {
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
}, {
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Ernst Handel",
"City": "Graz",
"Country": "Austria"
}, {
"Name": "FISSA Fabrica Inter. Salchichas S.A.",
"City": "Madrid",
"Country": "Spain"
}, {
"Name": "Galería del gastrónomo",
"City": "Barcelona",
"Country": "Spain"
}, {
"Name": "Island Trading",
"City": "Cowes",
"Country": "UK"
}, {
"Name": "Königlich Essen",
"City": "Brandenburg",
"Country": "Germany"
}, {
"Name": "Laughing Bacchus Wine Cellars",
"City": "Vancouver",
"Country": "Canada"
}, {
"Name": "Magazzini Alimentari Riuniti",
"City": "Bergamo",
"Country": "Italy"
}, {
"Name": "North/South",
"City": "London",
"Country": "UK"
}, {
"Name": "Paris spécialités",
"City": "Paris",
"Country": "France"
}, {
"Name": "Rattlesnake Canyon Grocery",
"City": "Albuquerque",
"Country": "USA"
}, {
"Name": "Simons bistro",
"City": "København",
"Country": "Denmark"
}, {
"Name": "The Big Cheese",
"City": "Portland",
"Country": "USA"
}, {
"Name": "Vaffeljernet",
"City": "Århus",
"Country": "Denmark"
}, {
"Name": "Wolski Zajazd",
"City": "Warszawa",
"Country": "Poland"
}]
Run Code Online (Sandbox Code Playgroud)