AngularJS ng-repeat get data from JSON - angularjs

I got a problem on getting data from ng-repeat.
<tr ng-repeat="x in records">
<td>{{x.id}}</td>
<td>{{x.title}}</td>
<td>{{x.year}}</td>
<td>{{x.note}}</td>
</tr>
The {{x.id}} is the tt0013158 I want to get from the JSON.
Here is the JSON:
{"result":{
"tt0013158":{
"note":"",
"title":"The Frozen North",
"year":"1922"
},
"tt1605783":{
"note":"",
"title":"Midnight in Paris",
"year":"2011"
}
}}
I can get the note, title and year correctly. But how can I get the id (such as tt0013158) from the JSON?

There is no notion in javascript (or angular) that tt0013158 in the sample above would be the object's id. It's just a key in a map.
I believe this should do the trick:
<tr ng-repeat="(key, x) in records.result">
<td>{{key}}</td>
<td>{{x.title}}</td>
<td>{{x.year}}</td>
<td>{{x.note}}</td>
</tr>

you need to use (key,value) syntax :
<tr ng-repeat="(key, value) in data in records.result">
<td>{{key}}</td>
<td>{{value.id}}</td>
<td>{{value.title}}</td>
<td>{{value.year}}</td>
<td>{{value.note}}</td>
</tr>

You can use following approach to iterate through ng-repeat
<tr ng-repeat="item in records">
<td>{{item.id}}</td>
<td>{{item.note}}</td>
<td>{{item.title}}</td>
<td>{{item.year}}</td>
</tr>

Related

how to iterate json data using ng-repeat in angularjs

how to iterate json data using ng-repeat in angularjs
{
"CSS Corp":{
"COE":{"win_loss":[6,4]},
"YNOS":{"win_loss":[5,5]},
"ESTEE":{"win_loss":[10,0]},
"ELC":{"win_loss":[8,2]}
},
"SSSPL":{
"PEG":{"win_loss":[0,10]},
"ARUBA":{"win_loss":[2,8]},
"SALES":{"win_loss":[1,9]},
"MARKETING":{"win_loss":[7,3]}
},
}
Your question is very broad. You will first have to attach those JSON data to the scope of your controller and expose it to the template through a variable myData. Assuming you know how to do that, the use of ng-repeat becomes very trivial (add more columns or rows to fit your dataset):
<table>
<thead>
<tr><th>Header 1</th></tr>
</thead>
<tbody>
<tr ng-repeat="item in myData">
<td>{{ item }}</td>
</tr>
</tbody>
</table>
Use ng-repeat
Syntax : <tr ng-repeat="value in container">
Assuming that you have a array of JSON object in your JS as below,
var arrayObject = [
"CSS Corp":{
"COE":{"win_loss":[6,4]},
"YNOS":{"win_loss":[5,5]},
"ESTEE":{"win_loss":[10,0]},
"ELC":{"win_loss":[8,2]}
},
"SSSPL":{
"PEG":{"win_loss":[0,10]},
"ARUBA":{"win_loss":[2,8]},
"SALES":{"win_loss":[1,9]},
"MARKETING":{"win_loss":[7,3]}
}
];
Then your view should iterate as below,
<div ng-repeat="company in arrayObject">
{{company}} // use whatever you want to print
</div>

I am trying to execute multiple ng-repeat so that I can access a third level array in my json and display in a table but can't get this working

I am trying to nest ng-repeat but looks like I am not doing it correctly.
I need all the lineItem in the json to be displayed.
Since, json value I am trying to display is a 3rd level array, I tried nested
ng-repeat but does not work.
<table border="1" width="100%">
<tr>
<th>Id</th>
<th>materialNumber</th>
<th>quantity</th>
</tr>
<tbody ng-repeat="subConfig in values.subConfigs.subConfig">
<tr ng-repeat="lineItem in subConfig.lineItems.lineItem">
<td>{{lineItem.lineItemId}}</td>
<td>{{lineItem.materialNumber}}</td>
<td>{{lineItem.quantity}}</td>
</tr>
</tbody>
</table>
here is jsfiddle I tried:
Your json was not in correct format values should not be array also you need to change ng-repeat="s in values.subConfigs.subConfig"> to ng-repeat="s in values.configBOM.subConfigs.subConfig">
Something like
<tbody ng-repeat="s in values.configBOM.subConfigs.subConfig">
<tr ng-repeat="lineItem in s.lineItems.lineItem">
<td>{{lineItem.lineItemId}}</td>
<td>{{lineItem.materialNumber}}</td>
<td>{{lineItem.quantity}}</td>
</tr>
</tbody>
Here is working fiddle
Its a very minor mistake :
Please change your json to an object instead of an array , the format you have given is wrong .
So it should be :
$scope.values={} //whatever you want to write inside
instead of:
$scope.value=[];
Secondly while you are doing ng-repeat you have to change this line :
<tbody ng-repeat="subConfig in values.subConfigs.subConfig">
to :
<tbody ng-repeat="subConfig in values.configBOM.subConfigs.subConfig">

ng repeat dynamic with key objet json AngularJS

I have a small problem I would like to set up a ng-repeat dynamic with the values I receive from my JSON object,
First, i made my th from my table with the keys
Secondly, i would like to do my td in dynamic (without putting the name of the key ex: obj.NOM, obj.TYPE ...)
I managed to do something with an Object.keys but logic is not good so I need some help
this is my JSON object ( i show you just the little piece of code that I have a problem )
"HEADER":[
{"NOM":"API-APP","TYPE":"string","DESCRIPTION":"Code application"},
{"NOM":"API-SIGNATURE","TYPE":"string","DESCRIPTION":"Signature de la requete API"},
{"NOM":"API-TIMESTAMP","TYPE":"integer","DESCRIPTION":"Timestamp en microseconde"}]
and this is my ng repeat
<span><b>HEADER</b></span>
<br>
<br>
<table class="table">
<th ng-repeat ="(key, itemHeader) in itemHead.HEADER[0] track by $index">{{key}}</th>
<tr ng-repeat ="(key, itemHeader) in itemHead.HEADER track by $index" >
<td>{{getFirstPropertyValue(itemHeader,$index)}}</td>
</tr>
</table>
I explain i made first a ng-repeat for th with the keys and I would like put my data (3 data per row) in td without put ( .NOM .TYPE .DESCRIPTION)
So I took the function object.key which works very well but that makes me just one element per row but I need 3 elements per row
this is my scope for the function object.key
$scope.getFirstPropertyValue = function(obj,index){
return obj[Object.keys(obj)[index]];
}
and this my result
thanks in advance for your help
<table class="table">
<thead>
<tr>
<th ng-repeat="(key, itemHeader) in itemHead.HEADER[0]">
{{key}}
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in itemHead.HEADER">
<td ng-repeat="column in row">
{{column}}
</td>
</tr>
</tbody>
</table>

What does reverse=!reverse means in angular orderby?

I am sorting data with AngularJS. I see in AngularJS documentation https://docs.angularjs.org/api/ng/filter/orderBy an example. I don't understand what does reverse=!reverse means in index.html. Here is the code:
div ng-controller="ExampleController">
<table class="friend">
<tr>
<th>Name
(^)</th>
<th>Phone Number</th>
<th>Age</th>
</tr>
<tr ng-repeat="friend in friends">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
</tr>
</table>
</div>
I did as in the example. It works but I don't understand what does the expression reverse=!reverse means?
When you click the link for Phone Number or Age, the sorting of the list is put in reverse order by those columns. reverse=!reverse; just toggles the value of that variable from true to false or from false to true before it's passed to the order function.

angularJS - Repeating tr's in a table, but dynamically adding more rows while looping

Since examples always tell more then just words here is what I would like to do in another language
<tr>
<c:forEach items="${items}" var="item">
<td>...</td>
<td>...</td>
<td>...</td>
<c:if test="${item.showWarning}">
</tr><tr><td colspan="3">${item.warning}</td>
</c:if>
</tr>
So this will loop over a set of items and show some properties of these items. If there is a warning, a new row will be added underneath the current row in which the warning will be shown. However, how can I do this in angularJs? If I put a ng-repeat on the tr, it will stop at the first end tag of tr. I have read on some other threads that this is not very easily done, but how can it be done? And yes, I really do want to use a table. Here is my contrived example with angularjs which is obviously not working as I would like it to. Any pointers how this can be done?
JSBin example with tr-ng-repeat
Currently (1.0.7/1.1.5) you can't output data outside the ng-repeat, but version 1.2(see youtube video AngularJS 1.2 and Beyond at 17:30) will bring the following syntax(adapted to your example):
<tr ng-repeat-start="item in items">
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr ng-repeat-end ng-show="item.showWarning">
<td colspan="3">{{item.warning}}</td>
</tr>
The idea is that whatever is between -start and -end will be repeated including the -end element.
One solution that I can think of is having multiple tbody tags within the same table. Here is a discussion on the use of multiple tbody tags within the same table.
So, for your issue, you could have the following setup:
<table ng-controller="ItemController">
<thead>
<th>Name</th>
<th>Description</th>
<th>warning?</th>
</thead>
<tbody ng-repeat="item in items">
<tr>
<td>{{item.name}}</td>
<td>{{item.description}}</td>
<td>{{item.warning}}</td>
</tr>
<tr ng-show="item.warning">
<td colspan="3" style="text-align: center">Warning !!!</td>
</tr>
</tbody>
</table>
Repeat the table body as many times as there are entries for the table and within it have two rows - one to actually display the row entry and one to be displayed conditionally.
You can repeat over the tbody
<tbody ng-repeat="item in items">
<tr>
<td>{{item.name}}</td>
<td>{{item.description}}</td>
<td>{{item.warning}}</td>
</tr>
<tr ng-show="item.warning">
<td colspan="3">Warning !!!</td>
</tr>
</tbody>
Also you do not need to wrap the ng-show expression in {{}}, you can just use item.warning

Resources