I have json object and I need to iterate that object in angular. I will tell you my problem. I have one button. On click of that button user can select multiple elements. I take example ('a','b','c'...so on). When user select 'a' and close the pop up I need to show this result.
Expected result when 'a' is selected
A // header
A S //names
A p
When user select 'A' its search from showTableData and show the names below header:
"A": [
{ "name":"A S"},
{ "name":"A p"}
],
When user select 'A' and 'B' then expected result:
A B // headers
A S B BS
A P B Bp
so on..
Actually I am able to print header if user select any thing 'A' .'B','C'..so on. I don't know how to print it corresponding names below header.
Here is my code: http://codepen.io/anon/pen/zGNLdR
<div class="row">
<div ng-repeat="d in data">
<div class="col" ng-show="d.checked">{{d.name}}</div>
</div>
</div>
is there any other way to take json object to show the expected result ..
can I map different way data with showTabledata so that i will get expected result ?
$scope.showTableData={
"A": [
{ "name":"A S"},
{ "name":"A p"}
],
"B": [
{ "name":"B BS"},
{ "name":"B Bp"}
],
"c": [
{ "name":"c c"},
{ "name":"c c"}
],
"d": [
{ "name":"d dS"},
{ "name":"d dp"}
],
"E":[
{ "name":"E ES"},
{ "name":"E Ep"}
]
};
I think that the simplest solution is just adding another div and iterate in it over your showTableData variable, but only by keys which were selected.
<div ng-repeat="d in data">
<div class="col" ng-show="d.checked">{{d.name}}</div>
<div class="col" ng-show="d.checked"
ng-repeat="nameObject in showTableData[d.name]">
{{nameObject.name}}
</div>
</div>
Example on codepen.
There are some things which can be refactored, but I hope, that you'll get the idea.
There is a little bit cleaner solution with using of filter. With this approach you can remove your ng-show inside ng-repeat.
<div ng-repeat="d in data | filter:{checked: true}">
<div class="col">{{d.name}}</div>
<div class="col" ng-repeat="nameObject in showTableData[d.name]">
{{nameObject.name}}
</div>
</div>
Example.
Related
I am trying to iterate over an object that's in an array of an array, however, I'm not able to display the data in the front-end.
I tried this method:
<div *ngFor="let item of events of event | keyvalue">
{{item.key}}:{{item.value}}
</div>
and this one:
<div *ngFor="let item of events of event | json">
Test: {{item}}
</div>
The structure of the JSON data is given in this format.
[
[
{
"age": "age3",
"gender": "gender3"
},
{
"age": "age3",
"gender": "gender3"
}
]
]
You'll have to iterate through each internal array as well, so you'll need a nested *ngFor. Make sure to reference the correct item array in your nested *ngFor.
<div *ngFor = "let item of events">
<div *ngFor = "let x of item">
{{x.age}}:{{x.gender}}
</div>
</div>
It looks likes a nested array, try using multiple ngFor:
<div *ngFor="let event of events">
<div *ngFor="let item of event">
Test: {{item | json}}
</div>
</div>
Otherwise you could try using Array.prototype.flat to flatten the nested array:
this.flattened = this.events.flat();
// ...
<div *ngFor="let item of flattened">
Test: {{item | json}}
</div>
<ng-container> is fine when you want structural logic without polluting the DOM with useless elements.
<ng-container *ngFor="let subArray of array"> <!-- This does not appear in the DOM -->
<div *ngFor="let item of subArray"> <!-- This appears in the DOM -->
{{item.key}}:{{item.value}}
</div>
</ng-container>
I'm using Angular 4 and I want to loop through an array of objects and display the information in Angular Material grids tiles. My html (app.component.html) looks a bit like this
<div class="list" *ngIf="listContacts == true">
<mat-grid-list cols="3" rowHeight="2:1" *ngFor="let i of contacts">
<mat-grid-tile>
<div class="card">
<img src={{contacts[i].image}} alt="picture">
<h2>{{contacts[i].name}}</h2>
</div>
</mat-grid-tile>
</mat-grid-list>
</div>
And contacts is an array of objects inside app.component.ts. There are NINE objects within contacts but for simplicity I've only put two here.
export class AppComponent {
// array of contact objects
contacts = [
{
name: "Robbie Peck",
image: "src/assets/robbie.jpg",
},
{
name: "Jenny Sweets",
image: "src/assets/jenny.jpg",
},
...
]
}
So rather than have nine different 's appear, each with their own information (looping i through contacts), I only have one and the console shows an error where it doesn't recognize contacts[i]. What have I done wrong? How can I get the to have 9 's, each with name and image i within the contacts object in the typescript (app.component.ts) file?
You don't have to pass the index, you can just use the variable i and access i.image and i.name
<mat-grid-list cols="3" rowHeight="2:1" *ngFor="let i of contacts" >
<mat-grid-tile>
<div class="card">
<img src={{i.image}} alt="picture">
<h2>{{i.name}}</h2>
</div>
</mat-grid-tile>
I'm using the angular-isotope plugin to repeat multiple quotes in a masonry grid. My goal is to add multiple unique tags to each quote with each tag having its own unique filter class so that when you click the tag, only tags from that filter appear. Currently, the functionality works fine with just a single tag:
HTML:
<div class="quotes js-masonry" isotope-container="isotope-container">
<div class="quote-col {{item.class}}" ng-repeat="item in quotes" isotope-item="isotope-item">
<div class="quote-single">
<div class="quote-quote">"{{item.quote}}"</div>
<div class="quote-author">
<a class="filter-click" data-filter="{{item.dataFilter}}">- {{item.author}}</a>
</div>
<div class="quote-bottom clearfix">
<span class="quote-share">
<img src="img/icons/facebook.png">
<img src="img/icons/twitter.png">
</span>
<span class="quote-tags" ng-repeat="?">
<a class="filter-click" data-filter="{{item.tagFilter}}">{{item.tag}}</a>
</span>
</div>
</div>
</div>
Angular Controller:
var demo = angular.module('angular-isotope-demo', []);
demo.controller('QuoteController', function QuoteController($scope) {
$scope.quotes = [
{
"quote":"It is better to lead from behind and to put others in front, especially when you celebrate victory when nice things occur. You take the front line when there is danger. Then people will appreciate your leadership.",
"author":"Nelson Mandela",
"tag": [
"Leadership",
"Second Class"
],
"class":"mandela leadership",
"dataFilter": ".mandela",
"tagFilter": [
".leadership",
".second-class"
]
}
]
});
My question is, how do I repeat that item tag (i.e. "Leadership", "Second Class") and attach it to that ".second-class" in the tagFilter so it looks like this?:
I hope this is clear to understand.
You will likely have a nested ng-repeat. So turn your quote.tag element into an array and do something like this:
<div ng-repeat="tag in item.tags">
<a class="filter-click" data-filter="{{tag.filter}}">{{tag.tag}}</a>
{{ $last ? '' : ( $index < item.tags.length-2 ) ? ', ' : '' }}
</div>
If you wish to have different classes for each item, then I would have another element within item.tags, say item.tags.classes and then do this:
<a class="filter-click" ng-class="tag.classes" data-filter="{{tag.filter}}">{{tag.tag}}</a>
Your data will need to be restructured for this to work:
{
"quote":"It is better to lead from behind and to put others in front, especially when you celebrate victory when nice things occur. You take the front line when there is danger. Then people will appreciate your leadership.",
"author":"Nelson Mandela",
"tags": [
{
"tag":"Leadership",
"filter":".leadership",
"classes": "second-class"
}
],
"class":"mandela leadership",
"dataFilter": ".mandela",
}
Angular newb here.
I'm trying to set up templates to display a collection of 'questionnaire' items. The questionnaire items may be a choice list or check box. Each questionnaire choice has an associated cost with it.
EG:
{ "Name" : "Product Packaging", "Template" : "TemplateList",
"Choices" : [ { "Box", 20 }, { "Tube", 30 } ] },
{ "Name" : "Custom Printing" , "Template" : "TemplateCheckbox",
"Choices" : [ { true, 10 }, { false, 0 } ] }
Templates:
<script type="text/ng-template" id="TemplateList.html">
<div>{{configitem.Name}}</div>
<div class="choiceValue">
<select ng-model="configitem.CurrentChoice" ng-options="choice.Value for choice in configitem.Choices" ng-change="update()">
</select>
</div>
</script>
<script type="text/ng-template" id="TemplateCheckbox.html">
<div>{{configitem.Name}}</div>
<div>
<input type="checkbox" ng-model="configitem.CurrentChoice.Value" ng-change="update()"/>
</div>
</script>
HTML:
<div ng-controller="ConfiguratorCtrl">
<div ng-repeat="configitem in model.ConfigurableItems">
<div ng-include="configitem.Template + '.html'"></div>
</div>
<div>Price: {{model.CurrentPrice | currency}}</div>
</div>
I'm using templates to display these objects based on the 'Template' type. This works well. However, I'd like to figure out a way to indirectly bind to my controller for the checkbox items. I need the on/off state for the checkbox to bind to one of the true/false 'choice' objects. Right now, it's binding to the first choice element. Any suggestions?
Fiddle
I have an array called groupUsers and another array within an array called tasks.users which look like this:
this.groupUsers = [
{$id: "simpleLogin:1", name: "bob"},
{$id: "simpleLogin:2", name: "joe"}];
this.tasks = [
{title: "do something", users:["simpleLogin:1","simpleLogin:2"]},
{title: "do something else", users:["simpleLogin:2"]}];
What I am trying to do is to get the user's name from groupUsers to display when I show the tasks rather than their id, but I am unsure in how to go about getting it. I assume it involves a filter?
With the following code I can display the tasks and the user id's assigned to each task, just not the names.
<div class="taskContainer" ng-repeat="task in todo.tasks | orderBy:'-when'">
<div><span ng-repeat="users in task.users">{{users}} </span></div>
<span> {{task.title}} </span>
</div>
You can expand to this function to fit your needs, right now it will return one name of the passed in id, $filter is an angular-service, remember to include it in your controller
$scope.getName = function(id){
return $filter('filter')(groupUsers, { name: id})[0].name;
};
here is your html
<div class="taskContainer" ng-repeat="task in todo.tasks | orderBy:'-when'">
<div><span ng-repeat="users in task.users">{{getName(users[0])}} </span></div>
<span> {{task.title}} </span>
</div>