HTML - split the array values dynamically using comma seperated - html

Using ng-repeat am showing array of values which am getting it from backend. But i want to split the specific values using comma seperated and displya it dynamically.
Please find the HTML code below.
<li ng-repeat="history in claimGroupingHistories">
<div class="row">
<div class="col-lg-4 col-md col-sm-4 col-xs-4">
<label>{{'claimgroup.history.oldvalueselected'|translate}}:</label>
</div>
<div id="historyOldValueSelected_{{$index}}" class="col-lg-8 col-md-8 col-sm-8 col-xs-8">{{history.oldValueSelected}}</div>
</div>
</div>
</li>
Here, in the {{history.oldValueSelected}} values will be like ABC1234, XYZ2345, IJK098. Instead of showing the value in single line. I want to split the value using comma seperated and display the value.
For Example :
Now, it showing like,
Old Value Selected : ABC1234, XYZ2345, IJK098
Expecting output like,
Old Value Selected : ABC1234
XYZ2345
IJK098
Splitting the array values dynamically using comma seperated.
Testdata :
[
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":null,
"newValueSelected":"Group by minimum claims, Customer Invoice or Repair Date",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-22T18:42:59.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims, Customer Invoice or Repair Date",
"newValueSelected":"Group by minimum claims",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-23T06:13:07.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims",
"newValueSelected":"Group by minimum claims, Customer Invoice No",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-23T06:14:54.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims, Customer Invoice No",
"newValueSelected":null,
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-24T06:23:33.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":null,
"newValueSelected":"Customer Invoice No, Group by minimum claims",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-24T06:23:58.000Z"
}
]

I am assuming history.oldValueSelected is a string, because if oldValueSelected was an array you could use ng-repeat like this:
<div layout="column">
<span ng-repeat="oldValue in history.oldValueSelected" ng-bind="oldValue"></span>
</div>
Now to oldValueSelected being a string. I can think of two options: You can either
split the values in the controller after you got your data
split it in ng-repeat
If you choose the second option, you can do the following:
<div layout="column">
<span ng-repeat="oldValue in history.oldValueSelected.split(', ')" ng-bind="oldValue"></span>
</div>
EDIT: I didn't get the expected output when I used class="row"
<li ng-repeat="history in $ctrl.histories">
<!-- class="row" didn't deliver the expected output-->
<div layout="row">
<div class="col-lg-4 col-md col-sm-4 col-xs-4">
<!-- For testing purposes - replace with your code-->
<label>{{history.name}}:</label>
</div>
<div layout="column">
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8" ng-repeat="oldValue in history.oldValueSelected.split(', ')">{{oldValue}}</div>
</div>
</div>
</li>
In the controller for testing purposes:
this.histories = [
{name: "number 1", oldValueSelected:"ABC1234, XYZ2345, IJK098"},
{name: "number 2", oldValueSelected:"ABC1234, IJK098"},
{name: "number 3", oldValueSelected:"IJK098"}
];

app.component.html
<div *ngFor="let hero of heroes.split(', ')">
{{ hero }}
</div>
app.component.ts
private heroes: string = "123123, 123123, 345354, 5675757";
Output:
123123
123123
345354
5675757

Related

How to bind JSON object key value pair separately to angular template

And i have a dynamic json coming from an API as below:
{123: "Mumbai", 456: "Bangalore", 789: "Chennai", 101: "Andhra",...}
I have below HTML code written in my template in which I am getting image-1, image-2 logos from my assets folder
<div class="row">
<div class="col-6" (click)="cityClick('Bangalore')">
<div class="img-1">
// my image-1 logo goes here
</div>
<div class="img-text">
Bangalore
</div>
</div>
<div class="col-6" col-6 (click)="cityClick('Mumbai')">
<div id="image_block" class="img-2">
// my image-2 logo goes here
</div>
<div id="text_block" class="img-text">
Mumbai
</div>
</div>
</div>
How to get the key of the json when i click on image and also display the text below the image from the corresponding key-value.
And when i click i should be able to pass the key and text inside the click event. Please help as i am new to Angular!
First convert this JSON into an JavaScript/TypeScript array like below:
var json = {123: "Mumbai", 456: "Bangalore", 789: "Chennai", 101: "Andhra" };
var jsonToBeUsed = [];
for (var type in json) {
item = {};
item.key = type;
item.value = json[type];
jsonToBeUsed.push(item);
}
This will result in data like below:
Now use NgFor in the template to bind array. (Refer this for NgFor)
<div class="row">
<div *ngFor="let item of array">
<div class="col-6" (click)="cityClick(item)">
<div class="img-1">
// my image-1 logo goes here
</div>
<div class="img-text">
{{item.value}}
</div>
</div>
</div>
</div>
We have passed the whole object in the click event. You can read any of the desired property from the object in click event handler which you will write in the component.
For your special requirement, you can use below markup:
<div class='row' *ngFor='let index of array; let i = index; let even = even'>
<div *ngIf="even" class='col-6' (click)="cityClick(array[i])">
<div class="img-1">
// my image-1 logo goes here
</div>
<div class="img-text">
{{array[i].value}}
</div>
</div>
<div *ngIf="!even" class='col-6' (click)="cityClick(array[i])">
<div class="img-1">
// my image-1 logo goes here
</div>
<div class="img-text">
{{array[i].value}}
</div>
</div>
</div>
Use this below function in your code:
getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
And use as
var dynamicJson = {123: "Mumbai", 456: "Bangalore", 789: "Chennai", 101: "Andhra"}
cityClick(value){
var key = this.getKeyByValue(this.dynamicJson, value);
console.log(key);
}
{123: "Mumbai", 456: "Bangalore", 789: "Chennai", 101: "Andhra",...}
Do you have influence on that JSON? This highly looks like a design issue for me. I assume those numbers are id's. I believe somethings like this should be better:
[{id: "123", name: "Mumbai"}, {id: "456", name: "Bangalore"}, {id: "789", name: "Chennai"}, {id: "101", name: "Andhra"},...}]
In that case you receive an array of cities, which could be an interface to parse to.
export interface City {
id: string;
name: string;
}
And you can easily render it in html by using *ngFor
<div *ngFor="let city of cities">
<!--do something with city.id and city.name-->
</div>
<div *ngFor let value of json |keyvalue > </div>

iterate elements of a json using *ngFor only if they met a condition

I'm working on an angular 4 project calling a json through a service, everything works very well exept for a single thing, my json has the following simplified structure for understanding the problem:
{
"animals" [
{
"name" : "dog"
"subgroup": "vertebrate"
"class" : "mammal"
},
{
"name" : "pig"
"subgroup": "vertebrate"
"class" : "mammal"
},
{
"name" : "cat"
"subgroup": "vertebrate"
"class" : "mammal"
},
{
"name" : "snake"
"subgroup": "vertebrate"
"class" : "reptile"
},
{
"name" : "lizzard"
"subgroup": "vertebrate"
"class" : "reptile"
},
{
"name" : "crocodile"
"subgroup": "vertebrate"
"class" : "reptile"
},
]
}
and i want to iterate ONLY the objects with the "class" : "reptile"
i made this structure:
<div class="col-12 mb-3" *ngFor="let reptiles of animals">
<div *ngIf = "reptiles.class == reptile">
<div class="row">
<div class="col-12">
<h5 class="py-3 bg-dark text-white pl-3 mx-0 mb-3">{{reptiles.name}}</h5>
<p class="py-3 bg-dark text-white font-weight-light pl-3 m-0">{{reptiles.class}}</p>
</div>
</div>
</div>
</div>
but what happens is that it iterates three empty
<div class="col-12 mb-3" *ngFor="let reptiles of animals">
</div>
corresponding to the mammals, and i want that objects not to iterate at all, i want to iterate only the objects with the class "reptile".
how can i achieve that?
The easy fix is to use ng-container instead of a div to iterate:
<ng-container *ngFor="let reptiles of animals">
<div class="col-12 mb-3" *ngIf="reptiles.class == reptile">
<div>
<!-- ... -->
</div>
</div>
</ng-container>
Of course the template still iterates over these entries now, but it will not create any DOM node for it whatsoever (the magic of ng-container).
Possibly a better fix would be to instead filter in your component and only pass the data you want to display to the template:
// In your controller after receiving the animals data:
this.reptiles = this.animals.filter(a => a.class === "reptile");
// Template
<div *ngFor="let reptile of reptiles">...</div>
You can also write a filterBy pipe or take one from an existing library such as ngx-pipes. But beware that Angular discourages that as it easily becomes a performance pitfall.
I think that you can use this solution
Just filter by class property:
filterItemsOfType(type){
return this.items.filter(x => x.class == type);
}
Cheers,
#carlosrojas_o
you just need to filter your data in component like this:
this.filteredAnimals = this.animals.filter(animal => animal.class === "reptile"); // now use filteredAnimals in html template
Hope it will help

Not able to fetch key and value from nested JSON data in IONIC

I face a problem now that I am not able to fetch key and value from nested JSON data.Please help me I am doing any wrong.
<ion-item class="item item-thumbnail-left item-text-wrap" type="item-text-wrap" nav-transition="android" ng-repeat="data in hrINPROCandidateList | filter:search">
<p class="small-text blue-text ">{{data.cfname}}</p>
<div class="row" ng-repeat="(key,value) in data.note_detail_fields track by $index">
<div class="col field">{{key}}</div>
<div class="col field-info">{{value}}</div>
</div>
</ion-item>
JSON Data
{
"responseToken": 1,
"list": [
{
"candidate_id": "4",
"note_detail_fields": "{\"link\":\"View Interview Details\",\"job_title\":\"Sopra Executive\",\"Candidate\":\"Mark Ashton\",\"Interview_Type\":\"First Interview\",\"\":\"\",\"Duration\":\"30 minutes\",\"Date\":\"Wednesday, 23<sup>rd<\\/sup> November 2016\",\"Time\":\"09:00 to 09:30\",\"Location\":\"london\"}",
"cfname": "Idris",
"clname": "Alba"
},
{
"candidate_id": "506",
"note_detail_fields": null,
"cfname": null,
"clname": null
},
{
"candidate_id": "32",
"note_detail_fields": "{\"link\":\"View Shared Job\",\"job_title\":\"Manager\",\"Location\":\"London, United Kingdom\",\"Package\":\"800 - 850 per day GBP\"}",
"cfname": "Sajal",
"clname": "Agarwal"
}
],
"totalCount": "4",
"success": 1,
"status": null
}
View data in app
You re trying to loop through a string instead of an array, create a filter that parse the json contained in data.note_detail_fields
<div class="row" ng-repeat="(key,value) in data.note_detail_fields | fromJson track by $index">
<div class="col field">{{key}}</div>
<div class="col field-info">{{value}}</div>
</div>
app.filter('fromJson',function(){
return function(input){ return angular.fromJson(input); }
})

Angular ng-show won't eval correctly

So if my JSON array object returns this:
"Tools": [
{
"name": "Submit a Claim",
"position": 1,
"isOn": true,
"alert": null
},
{
"name": "My Recurring Claims",
"position": 2,
"isOn": true,
"alert": null
},
{
"name": "Online Enrollment",
"position": 3,
"isOn": false,
"alert": "Online enrollment is available for the upcoming plan year. Click here to enroll!"
},
And my ng-show html has this:
<div class="toolTile col-md-3" ng-show="Tools.name = 'Online Enrollment' && Tools.isOn==true ">
<a href="#/claimEnter">
<img class="toolIcon" src="ppt/assets/toolIcons/oe.svg">
<p>Online Enrollment</p>
</a>
</div>
<div class="toolTile col-md-3" ng-show="Tools.name = 'Submit a Claim' && Tools.isOn==true ">
<a ng-click="transId()" ng-href="{{ ppt.Globals.hasDebitCard ? '#/claimEnter' : '#/claimSub' }}" >
<img src="ppt/assets/toolIcons/submitclaim.svg" >
<p>Submit a Claim</p>
</a>
</div>
Why does it keep evaluationing to false? I have tried quite a few variations and the "Online Enrollments" should hide while the "Submit a Claim" should show.
Any ideas of what I may be doing wrong here?
Thanks much.
Hi Man You could not Post Code with Out Head and Tail. There were Some
Syntax Errors Which caused the trouble .
You Need to Provide a Plunker with Buggy Code at least for some help :-) .
When you got an array in object you need to iterate over it.
I have provided you a running Plunker :-
<div ng-repeat="tool in data.Tools">
<div ng-show="tool.name == 'Online Enrollment' && tool.isOn==false ">
Online Enrollment
</div>
<div ng-show="tool.name == 'Submit a Claim' && tool.isOn == true ">
Submit a Claim
</div>
</div>
Plunker :-
http://plnkr.co/edit/B01pKWLsUtA2JlTAPOd5?p=preview
Good Luck !! Let me Know Your Queries if any !!

Nested ngrepeats with inconsistent data structures

I am creating a set of divs from a JSON object using ng-repeat that construct "note" cards. The number of cards is determined by the number of JSON objects in the array. "Type" designates the header of the card, and "Content" designates the body. However, the contents of "Content" is not consistent. In some examples, the content is merely a sentence or paragraph. In others, the content is an array. When the content is a sentence, the formatting is handled fine. When the content is an array, I end up with the actual array in text format [{"year":"2009", "operation":"caesarean"},{"year":"2010", "operation":"torn meniscus"}] as the body of the card. Any ideas of how to implement this without scripting each card individually?
HTML
<section name="patientinfo">
<div align="center" ng-controller="HistoryCtrl">
<div class="grid-container" ng-repeat="record in historyItems">
<div class="row" row>
<div class="col-2" ng-repeat="history in record.History">
<div class="note" draggable="true">
<div class="row" id="rowhead3">
<div class="col-6" >
<span class="note-header">{{history.type}}</span>
</div>
</div>
<div class="row">
<div class="col-6" style="overflow: auto; height:250px;">
<div>
<span class="note-content">
{{history.content}}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
JSON Example (the actual JSON is huge). There are many other entries besides "history". Also, these notes are not from a real person, so don't worry.
"History": [
{
"type": "medical",
"content": [
{
"condition": "IBS"
},
{
"condition": "Torn Meniscus Right Knee"
},
{
"condition": "Seasonal Allergies"
}
]
},
{
"type": "SOCIAL HISTORY",
"content": "Lives with husband and 3 children. No history of violence, domestic violence, or rape. Feels safe at home and in relationship. ETOH on weekends (socially 4 drinks in one weekend occasionally) and occasionally smokes cigarettes and marijuana. Admits to very rare marijuana on special occasions."
}]
Example of what I'm ending up with:
http://i.stack.imgur.com/MtuBN.png
You should transform or augment the data into a new format where you have the following structure for each note:
{
type: "string",
contentType: "(text|list)",
content: "string" // OR array of strings
}
To do this, you will need to have custom logic to transform each object in the array to a single string.
Then just use an ng-switch on the contentType attribute to decide which markup to use (e.g. a <ul> if contentType is list).