I can't load json object I created - json

I am using KnockoutJS for a web page. I created a JSON object to mimic values from server. Each time I run the page I get an empty record. Am I missing something?
var data ={ "employee": [{
"SiteId": 9,
"SystemId": 1,
"SystemName": "Glocus",
"ManualLink": "http://www.Glocus.com/Guide",
"ContactNo": "301-489-8989",
"UserAccount": [{
"AccountId": 1,
"AccountName": "Sisij",
"AccountType": "Customer System",
"Username": "Tybeke"
"Password": "*****",
}, {
"AccountId": 2,
"AccountName": "gabid",
"AccountType": "System System",
"Username": "Tybeke"
"Password": "*****",
}]
}, {
"SiteId": "1",
"SystemId": 1,
"SystemName": "TheSecondWorld",
"ManualLink": "http://www.TheSecondWorld.com/Guideplan",
"ContactNo": "301-489-8989",
"UserAccount": [{
"AccountId": 1,
"AccountName": "Uniqex",
"AccountType": "Customer System",
"Username": "TUniqux"
"Password": "*****",
}, {
"AccountId": 2,
"AccountName": "Celind",
"AccountType": "System System",
"Username": "GCElinds"
"Password": "*****",
}]
}]};
var ViewModel = function () {
var self = this;
var hello = ko.mapping.fromJS(data);
self.items = ko.observableArray(hello);
};
ko.applyBindings(new ViewModel());
Here is my knockoutJS Html code:
<div>
<span>Account</span>
<hr/>
<table>
<thead>
<tr>
<th>System Name</th>
<th>Manual link</th>
<th>Contact No</th>
</tr>
</thead>
<tbody data-bind="foreach: items.employee">
<tr>
<td data-bind="text :SystemName"></td>
<td data-bind="text : ManualLink"></td>
<td data-bind="text : ContactNo "></td>
</tr>
</tbody>
</table>
<hr/>
</div>
I have wasted so much time trying to get it right. Any help will be appreciated.

ko.mapping is used to create viewmodels. So you wouldn't actually nest view models in this case; you would just use the JSON to create one:
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
http://jsfiddle.net/32765/1/

Related

How do I display data from JSON into a table in Reactjs

I have JSON structure like below:
const villages =
{
"lossesOccured":
[
{
"type": "destroyed",
"affectedOn": "humans",
"quantity": 120,
"reliefFund": 100000,
"location": {
"district": "thanjavur",
"villageName": "madukkur",
"pincode": "614903"
}
},
{
"type": "physicalDamage",
"affectedOn": "humans",
"quantity": 250,
"reliefFund": 50000,
"location": {
"district": "thanjavur",
"villageName": "madukkur",
"pincode": "614903"
}
},
]
}
I need help in displaying the JSON data in the form of table like below Using React. Also, need the table row to be added automatically whenever a new element is added in JSON.
<table>
<thead>
<tr>
<th>AffectedOn</th>
<th>Type</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
{
villages.lossesOccured.map(loss => (
<tr>
<td>{loss.affectedOn}</td>
<td>{loss.type}</td>
<td>{loss.quantity}</td>
</tr>
)
}
</tbody>
</table>

How to fill a table with an object including an array of object in Angular2

I'm currently programming in Angular2/Typescript and I'm blocked with a table. I want to fill a table with the information I receive from my service which looks like that :
[
{
"id": 1,
"serviceWindow": "24/7",
"status": "Active",
"customer": "BGL",
"serviceProvidedType": "Call Desk",
"drpContactList": [
{
"id": null,
"name": "Jean Bon",
"timeStart": "08:00",
"timeEnd": "16:00",
"value": "jean.bon#ctg.com",
"mediumType": "Email"
},
{
"id": null,
"name": "Charles Degaule",
"timeStart": "16:00",
"timeEnd": "19:00",
"value": "+352 258 484 494",
"mediumType": "SMS"
}
]
},
{
"id": 2,
"serviceWindow": "Week-end",
"status": "Active",
"customer": "POST",
"serviceProvidedType": "Monitoring",
"drpContactList": [
{
"id": null,
"name": "Michel Palmas",
"timeStart": "10:00",
"timeEnd": "15:00",
"value": "+352 658 194 191",
"mediumType": "Phone"
}
]
},
{
"id": 3,
"serviceWindow": "Business hours",
"status": "Active",
"customer": "ING",
"serviceProvidedType": "Sharepoint",
"drpContactList": [
{
"id": null,
"name": "Alex Lapage",
"timeStart": "05:00",
"timeEnd": "10:00",
"value": "alex.lapage#ctg.com",
"mediumType": "Email"
}
]
},
{
"id": 4,
"serviceWindow": "Manage services",
"status": "Active",
"customer": "DEXIA",
"serviceProvidedType": "System Admin",
"drpContactList": [
{
"id": null,
"name": "RĂ©mi Hirtz",
"timeStart": "19:30",
"timeEnd": "23:00",
"value": "+352 595 945 154",
"mediumType": "Phone"
}
]
},
{
"id": 5,
"serviceWindow": "Enterprise holiday",
"status": "Active",
"customer": "POST",
"serviceProvidedType": "Hands & Eyes",
"drpContactList": [
{
"id": null,
"name": "Thomas Jacobs",
"timeStart": "13:00",
"timeEnd": "18:30",
"value": "thomas.jacbos#ctg.com",
"mediumType": "Email"
}
]
},
{
"id": 6,
"serviceWindow": "24/7",
"status": "Active",
"customer": "BGL",
"serviceProvidedType": "IT Outsourcing",
"drpContactList": [
{
"id": null,
"name": "Adrien Lafonte",
"timeStart": "05:45",
"timeEnd": "10:30",
"value": "+352 151 615 842",
"mediumType": "SMS"
}
]
}
]
So there is an array of object in my principal object. How can I display the information of the second array ? I tried some tricks but I never found a good way. Here is my HTML and Typescript :
<table class="table table-hover table-condensed">
<th style="width:150px" align="center">Customer</th>
<th style="width:150px" align="center">Service provided</th>
<th style="width:150px" align="center">Service window</th>
<th style="width:150px" align="center">DRP contact name</th>
<th style="width:150px" align="center">Time Start</th>
<th style="width:150px" align="center">Time End</th>
<th style="width:150px" align="center">Medium</th>
<th style="width:150px" align="center">DRP contact address</th>
<th style="width:150px" align="center">Status</th>
<tr *ngFor="#drp of drps"
[class.selected]="drp === selectedDrp"
(click)="gotoEdit(drp)">
<td style="width:150px" align="center">{{drp.customer}}</td>
<td style="width:150px" align="center">{{drp.serviceProvidedType}}</td>
<td style="width:150px" align="center">{{drp.serviceWindow}}</td>
<td style="width:150px" align="center">{{drp.drpContactList[0].name}}</td>
<td style="width:150px" align="center">{{drp.drpContactList[0].timeStart}}</td>
<td style="width:150px" align="center">{{drp.drpContactList[0].timeEnd}}</td>
<td style="width:150px" align="center">{{drp.drpContactList[0].mediumType}}</td>
<td style="width:150px" align="center">{{drp.drpContactList[0].value}}</td>
<td style="width:150px" align="center">{{drp.status}}</td>
</tr>
</table>
As you can see, for the moment I only show the first objet in the array. Either I put everything in only one row or I can create as row as object in the array.
Here is the Typescript behind everything :
export class ListDrpsComponent extends BaseComponent implements OnInit {
drps:Drp[];
constructor(private _drpService:DrpService,
private messageService:MessageToUserService,
private _router:Router) {
super(_router, messageService);
}
onAddDrp() {
let link = ['AddDrp'];
this._router.navigate(link);
}
getDrps() {
this._drpService.getDrps().subscribe(
data => {
this.onNext(data)
},
(data)=> this.onError(data),
() => null
);
}
onNext(data:Response) {
super.onNext(data);
this.drps = <Drp[]> data.json();
console.log(this.drps);
}
ngOnInit() {
this.getDrps();
}
onError(data:Response) {
super.onError(data);
}
gotoEdit(drp:Drp) {
let link = ['EditDrp', {id: drp.id}];
this._router.navigate(link);
}
}
I'm sorry for the presentation of the code, I'm new here ! If you need more details, tell me, I will try to respond to your demand. And sorry for my English, it's not my native language, I'm still learning.
Cordially,
Florian
EDIT : Here is a picture of what the table looks like. I wanted to have one row per information if possible (so i should repeat the first object information as many times as there is object in the second array)
Table of DRP
Without modifying your model (plnkr):
<ng-template ngFor let-drp [ngForOf]="drps">
<tr *ngFor="let drpCt of drp.drpContactList"
[class.selected]="drp === selectedDrp"
(click)="gotoEdit(drp)">
<td style="width:150px" align="center">{{drp.customer}}</td>
<td style="width:150px" align="center">{{drp.serviceProvidedType}}</td>
<td style="width:150px" align="center">{{drp.serviceWindow}}</td>
<td style="width:150px" align="center">{{drpCt.name}}</td>
<td style="width:150px" align="center">{{drpCt.timeStart}}</td>
<td style="width:150px" align="center">{{drpCt.timeEnd}}</td>
<td style="width:150px" align="center">{{drpCt.mediumType}}</td>
<td style="width:150px" align="center">{{drpCt.value}}</td>
<td style="width:150px" align="center">{{drp.status}}</td>
</tr>
</ng-template>

Create a table from REST object by setting data in columnar manner

I have a REST service, which returns this object:
[
{
"id": 100,
"value": "2452"
},
{
"id": 100,
"value": "2458"
},
{
"id": 1,
"value": "2457"
},
{
"id": 1,
"value": "2459"
},
{
"id": 4,
"value": "2460"
},
{
"id": 5,
"value": "3458"
}
]
Now, using this GET service, I want the following table to be built in angular and show it in UI.
100 1 4 5
-------------------
2452 2457 2460 3458
2458 2459
i.e. the unique ids should create the header and the list of values associates to each header value will be appended to respective column.
I have tried something with ng-repeat like this:
<table border="1">
<tr>
<th ng-repeat="column in cols">{{column.id}}</th>
</tr>
<tr>
<td ng-repeat="column in cols">
<md-list>
<md-list-item class="md-2-line" ng-repeat="val in column.values">
<md-checkbox ng-model="item.done"></md-checkbox>
<div class="md-list-item-text">
??
</div>
</md-list-item>
</md-list>
</td>
</tr>
But still wondering how to do the same? Please help.
Try use groupBy filter.
var app = angular.module('anApp', ['angular.filter']);
app.controller('aCtrl', function($scope) {
$scope.data = [
{
"id": 100,
"value": "2452"
},
{
"id": 100,
"value": "2458"
},
{
"id": 1,
"value": "2457"
},
{
"id": 1,
"value": "2459"
},
{
"id": 4,
"value": "2460"
},
{
"id": 5,
"value": "3458"
}
];
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<div ng-app="anApp" ng-controller="aCtrl">
<table border="1">
<tr>
<th ng-repeat="col in data | groupBy: 'id'">{{col[0].id}}</th>
</tr>
<tbody>
<tr>
<td ng-repeat="col in data | groupBy: 'id'">
<table>
<tr ng-repeat="c in col">
<td> {{c.value}}</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</div>

how to get the object inside object in json using angular js

Here is my json file.
{
"countries":[
{
"country": "India",
"cities" : [{
"name": "Bangalore",
"rank": "40"
},
{ "name": "Mumbai",
"rank": "32"
},
{ "name": "Kolkata",
"rank": "54"
},
{ "name": "Chennai",
"rank": "42"
}]
},
{ "country": "China",
"cities":[{"name": "Guangzhou",
"rank": "111"
},
{ "name": "Fuzhou",
"rank": "21"
},
{ "name": "Beijing",
"rank": "90"
},
{ "name": "Baotou",
"rank": "23"
}]
}
]}
I want to show all the name and rank of all the cities in the html table,
but I am unable to do this.
Angular js code is:
app.controller('cityCtrl1', function($scope, $http){
$http.get("http://localhost:8080/Angular%20Examples/angularCountries/app/json/countriesToCities1.json").success(function(data){
$scope.cities = data.countries;
});
});
Html code is:
<tr ng-repeat="city in cities | filter: selectName1">
<div ng-repeat="details in city.cities">
<td> {{details.name}} </td>
<td> {{details.rank}}</td>
</div>
</tr>
Might be I could change the code in controller file for getting the data, but not sure that will work or not.
First thing div inside a tr is not allowed(not working also) - Check this <div> into a <tr>: is it correct?
So I have change format -
<ul class="table" ng-repeat="country in cities.countries">
<li class="city-row" ng-repeat="city in country.cities">
<span class="city-name">{{city.name}}</span>
<span class="city-count">{{city.rank}}</span>
</li>
</ul>
I have created working example - http://plnkr.co/edit/1GTqNPcfigd9XRaAy0vg
Apply your own CSS to display it in table format.
what you are trying to do is,to refer city.states but where as states is not an object of json..change it to city.cities
<tr ng-repeat="city in cities | filter: selectName1">
<div ng-repeat="details in city.cities">
<td> {{details.name}} </td>
<td> {{details.rank}}</td>
</div>
</tr>

Angular.js - Nested JSON scope with Angular

{
"imports": {
"imported": [
{
"date": "19/9/2014",
"item": [
{
"sn": "3366698",
"type": "Food",
"weight": "10tn."
},
{
"sn": "3366699",
"type": "Eqipment",
"weight": "20kg."
}
]
},
{
"date": "20/9/2014",
"item": [
{
"sn": "3366700",
"type": "Electronics",
"weight": "100pt."
},
{
"sn": "3366701",
"type": "Food",
"weight": "5tn."
}
]
}
]
}
}
I have this json and I am not sure if it's in the right structure. I am trying to render each item type (duplicates included) as table header by the following $.getJSON method:
$scope.items = data.imports.item;
and HTML as:
<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>
But I couldn't succeed. What am I doing wrong?
EDIT: jsfiddler
I don't see you are using the imported propery at all, try
$scope.items = data.imports.imported;
since imported is your array and not item.
<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>
Your json ist broken, paste your json here:
http://jsonformatter.curiousconcept.com/
You'll see that data.imports.item would be undefined.
Correct JSON to access would look like:
{
"imports": {
"item": [
{
"sn": "3366698",
"type": "Food",
"weight": "10tn."
},
{
"sn": "3366699",
"type": "Eqipment",
"weight": "20kg."
}
]
}
}
Also access your data after:
<th ng-repeat="item in items">
{{item["type"]}}
</th>