September 14, 2015 06:39 by
Peter
Today, let me tell you about binding table with JSON String using Angular.This is a really easy Javascript function, that helps to convert Json String to a hypertext mark-up language Table by using AngularJS Frameworks. Define a well structured Json string as shown below, this Json string contains the information of two IT departments with the following code:
var dept1 = {
"data": [{
"Name": "Robert",
"City": "London",
"Country": "United Kingdom"
},
{
"Name": "Scott",
"City": "Manchester",
"Country": "United Kingdom"
},
{
"Name": "Rebecca",
"City": "Liverpool",
"Country": "United Kingdom"
},
{
"Name": "Peter",
"City": "Bristol",
"Country": "United Kingdom"
},
{
"Name": "Thomas",
"City": "Leeds",
"Country": "United Kingdom"
}]
}
var dept2 = {
"data": [
{
"Name": "Ethan",
"City": "Cardiff",
"Country": "United Kingdom"
},
{
"Name": "David",
"City": "Southampton",
"Country": "United Kingdom"
},
{
"Name": "Suzan",
"City": "Norwich",
"Country": "United Kingdom"
}
]
}
JS (Functions)
var app = angular.module('myApp', []);
app.controller('employees', function($scope, $http) {
$scope.names = dept1.data;
$scope.next = function() {
$scope.names = dept2.data;
}
$scope.prev = function() {
$scope.names = dept1.data;
}
});
HTML
<div ng-app="myApp" ng-controller="employees">
<input type="button" value="Development" ng-click="prev();">
<input type="button" value="Testing" ng-click="next();">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
CSS
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 0.25rem;
text-align: left;
border: 1px solid #ccc;
}
tbody tr:hover {
background: yellow;
}
HostForLIFE.eu AngularJS Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.