Set and retreive routerLink parameters via json in Angular2 - json

I've got a list of items generated by *ngFor in Angular2.
I want each of the items to work as a link to the full page with description of that item.
The generated data is retrieved via json.
As I want to use the items as links, I guess they should have router parameters like that [routerLink]="['/items', item.description]" in order to redirect users to the description page.
The question is how to pass such parameters via json (I can't bind them directly to the link in my html) ?
My item structure (div itemBlock supposed to work as a link):
<div class="itemBlock" *ngFor="let item of items">
<img src="{{item.image}}" alt="Item image" class="item--image">
<h3 class="item--name">{{item.name}}</h3>
<span class="item--number">{{item.number}}</span>
</div>
Json structure:
[
{
"id":"Item 1",
"name": "Item 1 Name",
"image": "./item1.jpg",
"number": "Number 1"
},
{
"id":"Item 2",
"name": "Item 2 Name",
"image": "./item2.jpg",
"number": "Number 2"
},
...,
...
]

Related

Displaying RESTApi information in html

I am creating my own simple rest api. I would like to be able to display information from it onto a html page. For example, if you called a get request to https://ip:3000/products you would get a response like:
"product": [{
"type": "Type 1",
"price": "price 1"
},
{
"type": "Type 2",
"price": "price 2",
}]
So how could i call this request and make a website that lists all the types in one column and all the prices in another?
you can use the fetch function of JavaScript to display your API data nicely on the HTML page.
Here (https://scotch.io/tutorials/how-to-use-the-javascript-fetch-api-to-get-data) you can find more information about it.
Or you can also retrieve the data via Ajax, and then display.
(https://hackersandslackers.com/making-ajax-calls-with-jquery/)

How to iterate through multi-nested JSON Object for dropdown function in Angular 6?

I am working in Angular 6.0 and I have ran into the issue of using HttpClient to iterate through my JSON data I am reading in from a local file in my assets folder.
Here is my JSON Data
[{
"configKey": [{
"user1": [{
"subconfig": [{
"name": "program Id",
"type": "String"
}, {
"name": "brand ID",
"type": "String"
}]
}],
"user2": {
"subconfig": [{
"name": "program Id",
"type": "String"
}, {
"name": "brand ID",
"type": "String"
}]
},
"user3": {
"subconfig": [{
"name": "program Id",
"type": "String"
}, {
"name": "brand ID",
"type": "String"
}]
}
}]
}]
Here is my current code in the component ts file
rule:Array<any> =[];
loadinfo(){
this.http.get<any[]>("assets/test.json")
.subscribe(data=>{
this.rule = data;
})
}
Here's what I have in the HTML File
<label>
<select>
<option *ngFor="let og of rule">
{{og.configKey}}
</option>
</select>
</label>
In my expected end goal, I should be able to populate a dropdown list based on a previous selection. (eg. I select user1, the list should populate with the 'names' in it's user tree in the JSON). However, I have not gotten to this part and would just like help with simply populating a dropdown list with names from my JSON tree.
From my previous research, I understand that HttpClient only returns Objects and that *ngFor only works on arrays. I have tried to change the object to an array, but my result is only another error that states that ngFor can only iterate through objects. Is there a way to change my entire nested JSON object to Array?
For what it's worth, here is my JSON Data in console
Console JSON
How would I go about making my data iterable by *ngFor and how would I navigate to the names? I would assume in the html it would be something like this? :
//TEST CODE????
<label>
<select>
<option *ngFor="let og of rule">
{{og.configKey.user1.subconfig.name}}
</option>
</select>
</label>
Any sort of help would be appreciated! Thanks in advance
You can iterate objects by keyvalue pipe. (https://angular.io/api/common/KeyValuePipe)
You can do it like this:
<div *ngFor="let config of res | keyvalue">
<div>{{config.key}}</div>
<div *ngFor="let users of config.value">
<div *ngFor="let user of users | keyvalue">
<div>{{user.key}}</div>
<div *ngFor="let subcfgs of user.value | keyvalue">
<div>{{subcfgs.key}}</div>
<div *ngFor="let subcfg of subcfgs.value">
<div>name: {{subcfg.name}}</div>
<div>type: {{subcfg.type}}</div>
</div>
</div>
</div>
</div>
</div>
stackblitz : https://stackblitz.com/edit/angular-ph76ya
I have edited your JSON.

JSON Path - size of root array element (NeoLoad)

I'm using NeoLoad 6.3.1 at the moment and am trying to get the length of an array where the array itself is the root element.
Given the following sample JSON:
[
{ "id": 1, "title": "Item 1" },
{ "id": 2, "title": "Item 2" },
{ "id": 3, "title": "Item 3" },
{ "id": 4, "title": "Item 4" },
{ "id": 5, "title": "Item 5" }
]
I want to just get back the answer of "5".
If I use the JSON Path Online Evaluator, I can use $.length and it returns:
[ 5 ]
In NeoLoad 6.3.1, that returns an error.
As NeoLoad is Java-based, I am assuming that they're using the com.jayway.jsonpath's json-path library (or something similar). Based on the documentation there I updated the query to be $.length() but did not have any luck.
Any suggestions?
In Neoload, there is "Variable Extractor" action where you can provide left boundary, right boundary for any one of the subnode in your array. e.g.
LB:"title": "
RB: " }
and select "extract call occurrences" option. This variables can be accessed via "variablename_matchNr" which gives count of all occurrences of given extraction.
Better explained here:
http://answers.neotys.com/questions/590268-created-variable-extractor-last-occurrence-extracted-values
Neload also provides JSON path expression in variable extractor where user can select any one node and select "extract call occurrences".

Radio button groups in ng-repeat

I have a list of items that contains organizations and each organization has two properties TargetIsSelected and InfoIsSelected which could be either true or false and are mutually exclusive. When the form loads I want the appropriate option to be selected for each organization row and when the user changes option I would like the changes updated in the underlying model for the respective organization.
My data looks like
$scope.Orgs = [{ "ID": "1", "Description": "Org 1", "TargetIsSelected": false, "InfoIsSelected": true },
{ "ID": "2", "Description": "Org 2", "TargetIsSelected": false, "InfoIsSelected": false },
{ "ID": "3", "Description": "Org 3", "TargetIsSelected": false, "InfoIsSelected": false }];
});
and the HTML
<body ng-controller="MainCtrl">
Target | Info
<div data-ng-repeat="org in Orgs">
<input type="radio" name="org{{$parent.$index}}" ng-model="$parent.TargetIsSelected" data-ng-value={{org.TargetIsSelected}}>
<input type="radio" name="org{{$parent.$index}}" ng-model="$parent.InfoIsSelected" data-ng-value={{org.InfoIsSelected}}>
{{org.Description}}
<br />
</div>
<br />
{{ Orgs | json }}
The problem is the default option from data (TargetIsSelected and InfoIsSelected) is not selected when the screen loads and changed option is not propagated to the model for each row. Currently I can select only one option from the entire list of options. I understand ng-repeat creates a child scope so I'm using $parent to reference the model but it doesn't seem to be working. Any help would be appreciated.
I have setup a Plunker

ElasticSearch Nested Array Partial Update

I have this particular object which contains the my_array:
"description": "My Object Description",
"my_array": [
{
"id": 1000,
"name": "abc",
"url" : "abc.html",
"content": "somebig content"
},
{
"id": 1001,
"name": "def",
"url" : "def.html",
"content": "somebig content"
},
{
"id": 1002,
"name": "xyz",
"url" : "xyz.html",
"content": "somebig content"
} ]
Each element in array contains a url. Now whenever this object changes, i have a task which hits the url for each element of the array, gets the html content for that element, and creates request document which can be indexed into elasticsearch.
Lets say, the url for id = 1001 is not accessible, and content for this element cannot be accessed. I still want to go ahead and process changes for elements 1000, and 1002. In that case my update would look like this:
"description": "My New Object Description",
"my_array": [
{
"id": 1000,
"name": "abc",
"url" : "abc-new-url.html",
"content": "some modified content"
},
{
"id": 1002,
"name": "xyz",
"url" : "xyz-new-url.html",
"content": "some modified content"
} ]
If i send this partial update to elasticsearch, the collection gets updated but element 1001 is removed from the collection.
My problem is how can i selectively update elements 1000, and 1002 without touching 1001. Index being stale with 1001 here is ok for me. One obvious choice is to fetch the existing doc from elasticsearch, and do the merging manually before doing the update. Is there any other way this partial update can be performed?
Another question, is there any way to send just the url to elasticsearch, and write a plugin to fetch the html content at index time, rather then doing it beforehand?
I think you could solve this using scripting in a update query, see these answers here:
remove objects from array elastic search
You can't do such an update using Elasticsearch native APIs. However, if you don't want to merge the updated content manually on your application level, a possible solution is to store each element of the array in a document with the same index as your original document, but different type.
Then do the update for each one of these elements (which in this case becomes documents) separately