Ich habe folgende Daten als 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
}
]
}
]
}
}
und ich möchte es als html-Tabelle mit anglejs und ng-repeat . anzeigen, damit ich die folgende Tabelle bekomme:
<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>
für die Tabellenvorschau: http://jsfiddle.net/54pD8/
mein Problem, dass die HTML-Tabelle zeilenbasiert arbeitet . Ich kann mit ng-repeat durch meine Runden und dann durch meine Übungen iterieren jede Übung und so weiter.
Kann mir jemand bei diesem Problem helfen?
ps. Wenn Sie eine Idee für ein besseres Layout für diese Daten in Json haben, sind Ihre Vorschläge willkommen. Ich bin neu bei Json (und Winkeljs).
Die Lösung, die Sie suchen, finden Sie in Angulars offiziellem Tutorial. In diesem Lernprogramm werden Telefone aus einer JSON-Datei mit Angulars $ http service geladen. Im folgenden Code verwenden wir $ http.get, um eine Datei phones.json zu laden, die im Telefonverzeichnis gespeichert ist:
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';
});
Wir durchlaufen dann die Telefone:
<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>
Einfache Methode zum Erstellen dynamischer Kopfzeilen und Zellen in normalen Tabellen:
<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;
})
});
JSON-Daten:
[{
"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"
}]
Um eine HTML-Tabelle mit JSON zu erstellen, verwenden wir die ngRepeat
-Direktive von AngularJS.
Beispiel
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table border="1">
<tr ng-repeat="x in names">
<td>{{x.Name}}</td>
<td>{{x.City}}</td>
<td>{{x.Country}}</td></tr>
</table>
</div>
</body>
</html>
JavaScript
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
$scope.names = [
{ "Name" : "Max Joe", "City" : "Lulea", "Country" : "Sweden" },
{ "Name" : "Manish", "City" : "Delhi", "Country" : "India" },
{ "Name" : "Koniglich", "City" : "Barcelona", "Country" : "Spain" },
{ "Name" : "Wolski", "City" : "Arhus", "Country" : "Denmark" }
];
});
In obigem Beispiel habe ich eine Tabelle von Json erstellt. Ich habe Bezug genommen von http://www.tutsway.com/create-html-table-using-json-in-angular-js.php
Winkel 2 oder 4:
Es gibt kein ng-repeat mehr, es ist * ngFür jetzt in den letzten Angular-Versionen!
<table style="padding: 20px; width: 60%;">
<tr>
<th align="left">id</th>
<th align="left">status</th>
<th align="left">name</th>
</tr>
<tr *ngFor="let item of myJSONArray">
<td>{{item.id}}</td>
<td>{{item.status}}</td>
<td>{{item.name}}</td>
</tr>
</table>
Verwendete diese einfache JSON:
[{"id":1,"status":"active","name":"A"},
{"id":2,"status":"live","name":"B"},
{"id":3,"status":"active","name":"C"},
{"id":6,"status":"deleted","name":"D"},
{"id":4,"status":"live","name":"E"},
{"id":5,"status":"active","name":"F"}]
Json in tabellarischer Form darstellen:
<table>
<thead>
<tr>
<th ng-repeat="(key, value) in vm.records[0]">{{key}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, value) in vm.records">
<td ng-repeat="(key, value) in value">
{{value}}
</td>
</tr>
</tbody>
</table>
Sie können die $http.get()
-Methode verwenden, um Ihre JSON
-Datei abzurufen. Weisen Sie dann einem $scope
-Objekt Antwortdaten zu. In HTML
zum Erstellen einer Tabelle verwenden Sie ng-repeat für das $ scope-Objekt. ng-repeat
wird die Zeilen innerhalb dieser Schleife durchlaufen, und Sie können Daten dynamisch an Spalten binden.
Ich habe Ihren Code überprüft und Sie haben eine statische Tabelle erstellt
<table>
<tr>
<th>Name</th>
<th>Relationship</th>
</tr>
<tr ng-repeat="indivisual in members">
<td>{{ indivisual.Name }}</td>
<td>{{ indivisual.Relation }}</td>
</tr>
</table>
so können Sie besser zu meinem Code gehen, um dynamische Tabelle zu erstellen, je nach den Daten, die Ihre Spalte und Zeile erhöhen oder verringern.