Create Table from JSON Data with angularjs and ng-repeat
Solution 1
The solution you are looking for is in Angular's official tutorial. In this tutorial Phones are loaded from a JSON file using Angulars $http service . In the code below we use $http.get to load a phones.json file saved in the phones directory:
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';
});
We then iterate over the phones:
<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>
Solution 2
Easy way to use for create dynamic header and cell in normal table :
<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 Data :
[{
"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"
}]
Solution 3
To create HTML table using JSON, we will use ngRepeat
directive of AngularJS.
Example
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 above example I have created table from json. I have taken reference from http://www.tutsway.com/create-html-table-using-json-in-angular-js.php
Solution 4
To render any json in tabular format:
<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>
See complete code in detail with other features
Solution 5
Angular 2 or 4:
There's no more ng-repeat, it's *ngFor now in recent Angular versions!
<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>
Used this simple 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"}]
user1616332
Updated on January 17, 2020Comments
-
user1616332 over 4 years
I have the following data as 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 } ] } ] } }
and I want to display it as a html table with angularjs and ng-repeat. so that I get the following table:
<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>
for table preview: http://jsfiddle.net/54pD8/
my problem that the html table is working row-based. I can iterate with ng-repeat through my Rounds and then through my excercises but for creating a table I need always the first of each exercises then the second of each exercises and so on.
Can someone help me with this problem?
ps. if you have an idea for better layout for this data in json, your suggestions are welcome, I'm new to json (and angularjs).
-
user1616332 about 10 yearsI have tried it with the same code and it worked with a list but i neeed a table!
-
user1616332 about 10 yearsThank you, this is exactly what i needed!
-
Slopes about 8 yearsI'm not sure if this is due to an update on Angular or if it was a typo, but I believe the ng:repeat should be ng-repeat. I'm getting an error when I use ng:repeat but it works with ng-repeat. I'm using the method that includes the external google script: <script src="ajax.googleapis.com/ajax/libs/angularjs/1.4.5/…> Thank you!
-
SantoshK over 7 yearsTh is static in your code.. it should be dynamic as per data increase the column