I am trying to display json data in html form using ng-repeat, I was able to grab data but some I could not due to objects in objects structure. Ex : "Obj => Obj => Obj" of a kind.
In the json object below, I have accessed the objs in the "Color" but where I need help is to access the objs in the "seat" and "engine"!. I think whatever ng-repeat that grabs "seat" objs would likely do the same for the "engine".
I did like this: ng-repeat = "c in datasource" first step. Next in a td, I did ng-repeat =" allcolor in c.Color" to get data of that "Color".
Now in another td, I want to grab the data of objs in "seat", how do I go about it?
Ex: ng-repeat = "seatobjs in allcolor.seat" not working.
<tr ng-repeat = "c in datasource">
<td>{{c.Name}}</td>
<td>{{c.From}}</td>
<td ><p ng-repeat="allcolor in c.Color>
{{allcolor.Name}}</p></td>
<td><p ng-repeat="seatobjs in allcolor.seat"> //need help here
{{seatobjs.Name}}</p></td>
<td><button type="button" ng-disabled="!(check)">Edit</button></td>
</tr>
Thank
[
{
"Name": "Car",
"From": "2019-06-26",
"Tom": "2019-12-31",
"Color": [
{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31",
"seat": [
{
"Name": "Yello",
"From": "2019-06-26",
"Tom": "2019-12-31"
},
{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
],
"engine": [
{
"Name": "Brown",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
]
}
]
},
{
"Name": "Car",
"From": "2019-06-26",
"Tom": "2019-12-31",
"Color": [
{
"Name": "Red",
"From": "2019-06-26",
"Tom": "2019-12-31",
"tyre": [
{
"Name": "Yello",
"From": "2019-06-26",
"Tom": "2019-12-31"
},
{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
],
"engine": [
{
"Name": "Black",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
]
}
]
},
]
Try this
ng-repeat = "seatobjs in allcolor['seat']"
allcolor cannot be accessed by elements that are outside of the ng-repeat defined element.
With your given HTML markup, you might want to use ng-repeat again on the new td and access seat as follows.
<tr ng-repeat = "c in datasource">
<td>{{c.Name}}</td>
<td>{{c.From}}</td>
<td ><p ng-repeat="allcolor in c.Color>
{{allcolor.Name}}</p>
</td>
<td ng-repeat =" allcolor2 in c.Color">
<p ng-repeat="seatobjs in allcolor2.seat">{{seatobjs.Name}}</p>
</td>
<td><button type="button" ng-disabled="!(check)">Edit</button></td>
</tr>
With the ngRepeat directive you can iterate over the properties of an object. This is the syntax:
<div ng-repeat="(key, value) in myObj">…</div>
The following code iterates some properties of your JSON and binds them inside a table:
var carApp = angular.module('carApp', []);
carApp.controller('CarController', function CarController($scope) {
$scope.cars = [{
"Name": "Car",
"From": "2019-06-26",
"Tom": "2019-12-31",
"Color": [{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31",
"seat": [{
"Name": "Yello",
"From": "2019-06-26",
"Tom": "2019-12-31"
},
{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
],
"engine": [{
"Name": "Brown",
"From": "2019-06-26",
"Tom": "2019-12-31"
}]
}]
},
{
"Name": "Car",
"From": "2019-06-26",
"Tom": "2019-12-31",
"Color": [{
"Name": "Red",
"From": "2019-06-26",
"Tom": "2019-12-31",
"tyre": [{
"Name": "Yello",
"From": "2019-06-26",
"Tom": "2019-12-31"
},
{
"Name": "Green",
"From": "2019-06-26",
"Tom": "2019-12-31"
}
],
"engine": [{
"Name": "Black",
"From": "2019-06-26",
"Tom": "2019-12-31"
}]
}]
},
];
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="carApp">
<div ng-controller="CarController">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Seat</th>
<th>Tyre</th>
<th>Engine</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="car in cars">
<td>{{car.Name}}</td>
<td>
<span>{{car.Color[0].Name}}</span>
</td>
<td>
<ul>
<li ng-repeat="seat in car.Color[0].seat">{{seat.Name}}</li>
</ul>
</td>
<td>
<ul>
<li ng-repeat="tyre in car.Color[0].tyre">{{tyre.Name}}</li>
</ul>
</td>
<td>{{car.Color[0].engine[0].Name}}</td>
</tr>
</tbody>
</table>
</div>
</div>
Related
I have a lot of data I need to parse though.
I need to pull all pid's and price's.
`
[
{
"id": 159817,
"price": "10.69",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 24549,
"pid": "12079",
"labels": [
{
"type": "default",
"value": "Chicken Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Chicken",
"locale": "en"
},
{
"type": "product_json",
"value": "Chicken",
"locale": "en"
}
],
"calMin": 600,
"calMax": 600,
"lastUpdated": "2021-12-31T13:49:22.794Z"
}
},
{
"id": 159818,
"price": "9.29",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 25,
"pid": "1",
"labels": [
{
"type": "default",
"value": "Ham Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Ham",
"locale": "en"
}
],
"calMin": 540,
"calMax": 540,
"lastUpdated": "2021-07-09T19:30:00.326Z"
}
}
]
`
and I need to place them into a string like this, but on a scale of 150 products. I'd also need to change "pid" to "productId"
[{ "productId": "46238", "price": 6.09 }, { "productId": "40240", "price": 1.49 }]
I need to add a string before this data, but I'm pretty confident I can figure that part out.
I am pretty open to the easiest suggestion, whether that be VBS, Excel macro, etc.
Am using angular 6, but i don't know how to use *ngFor in html table looping. i need code for below attached screenshot. Also am given API response
{
"result": [
[
{
"title": "Title 1",
"sno": "1",
"name": "abc",
"phone": "123456",
"email": "abc#gmail.com"
},
{
"title": "Title 1",
"sno": "2",
"name": "def",
"phone": "789456",
"email": "def#gmail.com"
},
{
"title": "Title 1",
"sno": "3",
"name": "ghi",
"phone": "4561230",
"email": "ghi#gmail.com"
}
],
[
{
"title": "Title 2",
"sno": "1",
"name": "john",
"phone": "4561230",
"email": "john#gmail.com"
}
]
]
}
Use nested ngFor Loop check the example below:
<table id="spreadsheet">
<tr *ngFor="let data of result">
<td class="row-number-column">{{data[0].title}}</td>
<td *ngFor="let item of data">
<tr>
<td class="row-number-column">{{item.sno}}</td>
<td class="row-name-column">{{item.name}}</td>
<td class="row-name-column">{{item.phone}}</td>
</tr></td>
</tr>
I have a table which shows the below data in the grid.Each value have a column in the table.I displayed all data by calling the api. I dont know how to display the currencies code,name,symbol,languages in the table.Can anyone help me to sort this out?
[
{
"name": "Afghanistan",
"topLevelDomain": [
".af"
],
"currencies": [
{
"code": "AFN",
"name": "Afghan afghani",
"symbol": "؋"
}
],
"languages": [
{
"iso639_1": "ps",
"iso639_2": "pus",
"name": "Pashto",
"nativeName": "پښتو"
},
{
"iso639_1": "uz",
"iso639_2": "uzb",
"name": "Uzbek",
"nativeName": "Oʻzbek"
},
{
"iso639_1": "tk",
"iso639_2": "tuk",
"name": "Turkmen",
"nativeName": "Türkmen"
}
],
"translations": {
"de": "Afghanistan",
"es": "Afganistán",
"fr": "Afghanistan"
}
}
}]
You can just do like:
<table>
<tr>
<td>{{data.name}}</td>
<td>
<span *ngFor="let item of data.currencies">
Code: {{item.code}}
Name: {{item.name}}
Symbol: {{item.symbol}}
</span>
</td>
</tr>
data is the item of your json.
I have Following Json:
{
"FunctionIs": [{
"id": 1,
"name": "Test",
"Type": "A"
}, {
"id": 2,
"name": "Test2",
"Type": "A"
},{
"id": 3,
"name": "Test3",
"Type": "A"
},{
"id": 4,
"name": "Test4",
"Type": "A"
},{
"id": 5,
"name": "Test5",
"Type": "B"
},{
"id": 6,
"name": "Test6",
"Type": "B"
},{
"id": 7,
"name": "Test7",
"Type": "C"
},{
"id": 8,
"name": "Test8",
"Type": "C"
},]
}
I am populating these values through ng-repeat in my html:
<tr ng-repeat="businessdata in userObj.businessJson">
<td id="idIs">{{businessdata.id}}</td>
<td>{{businessdata.name}}</td>
</tr>
Whenever there is change in "Type" (From A to B and B to C and C to D and so on), I want to insert a new <tr> for displaying the heading(Type) only for the first time. Can anyone help in this?
Thanks!
You could create create 3 different ng-repeat each for different type.
e.g.
<tr><th>A</th></tr>
<tr ng-repeat="item in data | filter: {Type:'A'}">
<td>{{item.name}}</td>
<td>{{item.id}}</td>
</tr>
<tr><th>B</th></tr>
<tr ng-repeat="item in data | filter: {Type:'B'}">
<td>{{item.name}}</td>
<td>{{item.id}}</td>
</tr>
<tr><th>C</th></tr>
<tr ng-repeat="item in data | filter: {Type:'C'}">
<td>{{item.name}}</td>
<td>{{item.id}}</td>
</tr>
Here's a working plunker
I have a table which will have collapsed rows that have 2nd level info, but not all the rows will have this 2nd level data.
How can i write a controller to only show a 2nd level collapse row if the JSON script has a 2nd level?
With the following json defined in your controller:
$scope.cakes = [{ "id": "0001", "type": "donut", "name": "Cake",
"ppu": 0.55, "batters": "whatever",
"topping": [ { "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" } ] },
{ "id": "0002", "type": "cupcake", "name": "Cupcake",
"ppu": 0.60, "batters": "whatever",
"topping": [] },
{ "id": "0003", "type": "muffin", "name": "Muffin",
"ppu": 0.25, "batters": "whatever",
"topping": [] }
];
you can selectively show a second row in a table where cake.topping.length is greater than 0:
<table>
<tr ng-repeat-start="cake in cakes">
<td>{{cake.type}}</td><td>{{cake.name}}</td>
</tr>
<tr ng-repeat-end ng-show="cake.topping.length>0">
<td ng-repeat="t in cake.topping">{{t.type}}</td>
</tr>
</table>