I'm calling an API and want to print my JSON result in tabular format using anugularjs. So I've declared a hardcoded array(train) in a function(myfunc). Now I want to print the array in the table format. Here's the code i tried:
function myfunc() {
var train = [{
"TrainNo": "11043",
"TrainName": "MADURAI EXPRESS",
"Source": "LTT",
"ArrivalTime": "03:50",
"Destination": "PUNE",
"DepartureTime": "00:15",
"TravelTime": "03:35H",
"TrainType": "EXP"
},
{
"TrainNo": "16533",
"TrainName": "BGKT SBC EXPRES",
"Source": "KYN",
"ArrivalTime": "04:10",
"Destination": "PUNE",
"DepartureTime": "01:05",
"TravelTime": "03:05H",
"TrainType": "EXP"
}
];
}
myfunc();
var app = angular.module('myApp', []);
app.controller('Ctrl', function($scope) {
$scope.names = train;
});
<!DOCTYPE html>
<html>
<style>
table,
th,
td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<script src="https://code.jquery.com/jquery-3.0.0.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<div ng-app="myApp" ng-controller="Ctrl">
<table>
<tr>
<th>Name</th>
<th>Class</th>
<th>Department</th>
</tr>
<tr ng-repeat="x in names">
<td>{{x.TrainName}}</td>
<td>{{x.TrainName}}</td>
<td>{{x.Source}}</td>
<td>{{x.Class}}</td>
<td>{{ x.Department}}</td>
</tr>
</table>
</div>
</body>
</html>
I've tried this code but still I'm still unable to insert data in the table.