Trouble getting data out of API using Vue JS - json

I'm having trouble iterating over data from a json file in Vue JS. I've got everything set up and have access to the json. What am I missing here:
JSON
{
"test": "example",
"data": [
{
"name": "My Name",
"address": "My Address"
}
]
}
Vue JS html
{{ someData['name'] }}
<ul>
<li v-for="item in someData" v-bind:key="item.id">
{{ item }}
</li>
</ul>
and my script:
created() {
this.$http.get('http://localhost:3000/properties.json').then(response => {
// get body data
this.someData = response.body
}, response => {
// error callback
});
}
The result I'm getting is:
Result
As you can see, in my v-for simply specifying item grabs each of the items here, but I want to be able to do something like {{ item['name'] }} and {{ item['address'] }}

The main problem is in your JSON file. VueJS v-for is iterating over the keys in your JSON file, in this case {{ item }} will be getting name and address. That is the reason of your output.
To solve this...
Format your JSON file as
[{name: 'John', address: 'Jane'}]
Doing this your VueJS v-for will be iterating over JSON array and {{ item }} will get the value { name: 'John', address: 'Jane' } and now you can do {{ item.name }} {{ item.address }}
An example here
EDIT: Update your API request code
created() {
this.$http.get('http://localhost:3000/properties.json').then(response => {
// get body data
this.someData = [response.body]
}, response => {
// error callback
});
}

You can simply do the following:
<template v-for="(item, key) in someData">
<span v-if="key == 'address'">{{item}}</span>
</template>

The crucial point is that the v-for="item in someData" part in the component's view iterates over values of the someData object, namely "John" and "Jane".
Depending on what exactly is your goal, you should either reformat the JSON to be an array of objects (as suggested by #F.bernal), or change the logic of v-for directive.

Related

Django download return

simple question please, my POST in the views returns a json format dictionary
nested_data = {
'name': cleaned_data3['theme_name'],
'visualStyles': {
'barChart': {
'*': {
'general': [{
'responsive': cleaned_data2['responsive'],
'keepLayerOrder': cleaned_data2['maintain_layer_order']
}],
'legend': [{
'show': cleaned_data['show'],
'position': cleaned_data['position'],
'showTitle': cleaned_data['show_title'],
'labelColor': {
'solid': {
'color': '#666666'
}
},
'fontFamily': cleaned_data['font'],
'fontSize': cleaned_data['font_size']
}],
}
}
}
}
then I am returning the code formatted into json using:
return JsonResponse(nested_data)
This shows me the json rendered in the browser, but how do I download this return? In my index.html the submit button is rendering the return from the view, but I need to submit the forms and to download the content into .json file, something needs to be put into the href?
<input type="submit" value="Submit">
<a href="{{ xxx }}" download>DOWNLOAD</a>
You need change the response content type to application/force-download.
response = JsonResponse(nested_data)
response['Content-Type'] = 'application/force-download'
return response
# or ...
return HttpResponse(
simplejson.dumps(nested_data),
content_type='application/force-download'
)
You should define a function to write the json in views.py or index.html.
with open(file_name, "wb") as f:
f.write(data)
if you gonna write it in views.py, you could write the bellow lines in the js code section.
csrfmiddlewaretoken: "{{ csrf_token }}"

Angular #Input array- how to reference different array object field names?

I want to setup a reusable dropdown menu whereby I pass an array of objecs to the #Input of my dropdown component and then update some labels in it.
The array fields will be different each time. One array might have a displayName string field, while another array will have a string called id as the field I want to reference, for example.
I'm not sure how to reference these on a per instance basis.
Dropdown HTML:
<ng-container *ngFor="let item of content">
<span class="button-label">{{item.whatever-the-field-is}}</span>
</ng-container>
Dropdown Component:
#Input() content: Array<any> = [];
Dropdown Instance:
<multi-button-dropdown [content]="(myArrayObservable$ | async)"></multi-button-dropdown>
Example Arrays:
users = [
{
id: 'afvnf102841-251',
username: 'Joe Bloggs'
}
...
]
members = [
{
displayName: 'Han Solo',
location: 'Space'
}
...
]
Question:
How do i setup the dropdown.html/component so that the <span> {{item....}} reference will be displayName for the members[] instance, id for the users[] instance and so on, passed in by another #Input value?
One quick way would be to use the keyvalue pipe inside the *ngFor to iterate over the object properties without knowing it's names.
<ng-container *ngFor="let item of content">
<span class="button-label" *ngFor="let prop of item | keyvalue">
{{ prop.key }} - {{ prop.value }}
</span>
</ng-container>
I figured out that I can make a new interface which sets out the label value, and map the Observable array object fields to this value before passing it into the component:
this.dropdownUsers = users.map(user => (
{
label: user.displayName
}
));
<multi-button-dropdown [content]="dropdownUsers"></multi-button-dropdown>

Angular: Show list in view with different object keys

I have the following JSON object:
header: {
first: {
title: {
name: "Test
}
},
second: {
title: {
name: "Test 2"
},
desc: {
name: "Description"
}
}
}
Now I want to show it in view like that:
How can I manage to do that in the view? Since the keys will vary every time. For example once you have header, another time main and so on... Something like JSONEditor, where you can edit JSON objects. But I want to create something like that with my own design. I tried with ngIf but it seems really difficult. I'd be really thankful for any suggestions or help.
You can use the KeyValuePipe to iterate over objects using ngFor.
<!-- app-your-node component -->
<div *ngFor="let item of object | keyvalue">
{{ title }}
<app-your-leaf *ngIf="yourLogicThatThisIsALeaf(item); else node"
[title]="item.key" [object]="item.value"><app-your-leaf>
<ng-template #node>
<app-your-node [title]="item.key" [object]="item.value"></app-your-node>
</ng-template>
</div>

Process Json using VUE

The following Json is recieved by the Vue Script, from a Php script which encodes an Array of Objects into JSON.
[{"title":"Clean Drinking Water","content":"Some Content","link":"abd.html","img":"<img src="kkk.jpg" />"
},
{"title":"Clean AAAAAter Provided","content":"Lore Lipsum
Test","link":"abc.html","img":"<img src="kkk.jpg" width="320" />"
}
]
How do i access title, content, link and image data from this Json using Vue. My Vue Code is
const url="lkl.php?get=json";
const vm=new Vue({
el:'#app',
data:{
results:[]
},
mounted(){
axios.get(url).then(response=>{
this.results=response.data
})
}
});
I have tried {{ results.title }} {{ results[0].title }} but that doesn't seem to work. I am fairly new to Vue. Any help would be much appreciated.

Angular ng-repeat with nested json objects?

I have a JSON object, represented as such:
{
"orders" : [
{
"ordernum" : "PRAAA000000177800601",
"buyer" : "Donna Heywood"
"parcels" : [
{
"upid" : "UPID567890123456",
"tpid" : "TPID789456789485"
},
{
"upid" : "UPID586905486090",
"tpid" : "TPID343454645455"
}
]
},
{
"ordernum" : "ORAAA000000367567345",
"buyer" : "Melanie Daniels"
"parcels" : [
{
"upid" : "UPID456547347776",
"tpid" : "TPID645896579688"
},
{
"upid" : "UPID768577673366",
"tpid" : "TPID784574333345"
}
]
}
]
}
I need to do a repeater on the second level of this, a list of the "upid" numbers.
I know already how to get the top level
<li ng-repeat="o in orders">{{o.ordernum}}</li>
But I am unclear on the sequence to loop a level down. For example, this is wrong:
<li ng-repeat="p in orders.parcels">{{p.upid}}</li>
I also know how to nest repeaters to get this, but in this case i don't need to display the top level at all.
CLARIFICATION
The goal here is to have one list with the 4 "upid" numbers (there are 2 for each parcel, and there are 2 parcels in the order).
Actually its same answer of #sylwester. The better way to put it in filter. And you can reuse it by passing propertyName parameter.
In your case we passed parcels
JS
myApp.filter('createarray', function () {
return function (value, propertyName) {
var arrayList = [];
angular.forEach(value, function (val) {
angular.forEach(val[propertyName], function (v) {
arrayList.push(v)
});
});
return arrayList;
}
});
HTML
<ul>
<li ng-repeat="o in ordersList.orders | createarray: 'parcels'">{{o.upid}}</li>
</ul>
Here is working Fiddle
You can just create new array 'parcels' like in demo below:
var app = angular.module('app', []);
app.controller('homeCtrl', function($scope) {
$scope.data = {
"orders": [{
"ordernum": "PRAAA000000177800601",
"buyer": "Donna Heywood",
"parcels": [{
"upid": "UPID567890123456",
"tpid": "TPID789456789485"
}, {
"upid": "UPID586905486090",
"tpid": "TPID343454645455"
}]
}, {
"ordernum": "ORAAA000000367567345",
"buyer": "Melanie Daniels",
"parcels": [{
"upid": "UPID456547347776",
"tpid": "TPID645896579688"
}, {
"upid": "UPID768577673366",
"tpid": "TPID784574333345"
}]
}]
};
$scope.parcels = [];
angular.forEach($scope.data.orders, function(order) {
angular.forEach(order.parcels, function(parcel) {
$scope.parcels.push(parcel)
})
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="homeCtrl">
<ul>
<li ng-repeat="o in parcels">{{o.upid}}</li>
</ul>
</div>
</div>
Seems like you just need a double-nested for loop -
<ul>
<div ng-repeat="o in orders">
<li ng-repeat="p in o.parcels">{{p.upid}}</li>
</div>
</ul>
The HTML might be a little ugly here, but I'm not sure what exactly you are going for. Alternatively you could just create a new array of the parcels via mapping.
Searching a lot for nice and simple solution for iterating dynamically. I came up with this
JAVASCRIPT (angular): a person is an example of nested object. the is_object function will be use in the HTML view.
$scope.person = {
"name": "john",
"properties": {
"age": 25,
"sex": "m"
},
"salary": 1000
}
// helper method to check if a field is a nested object
$scope.is_object = function (something) {
return typeof (something) == 'object' ? true : false;
};
HTML: define a template for simple table. the 1st TD is the key which is displayed. another TD (2 or 3, but never both) will be show the value if its not an object (number / string), OR loop again if its an object.
<table border="1">
<tr ng-repeat="(k,v) in person">
<td> {{ k }} </td>
<td ng-if="is_object(v) == false"> {{ v }} </td>
<td ng-if="is_object(v)">
<table border="1">
<tr ng-repeat="(k2,v2) in v">
<td> {{ k2 }} </td>
<td> {{ v2 }} </td>
</tr>
</table>
</td>
</tr>
</table>
The reason that <li ng-repeat="p in orders.parcels">{{p.upid}}</li> does not work the way you expect is because the parcels array is an object inside each individual order in your order array, i.e. it is not an object of the orders array itself.
If your orders array is defined on the $scope of a controller, then you create the array on the $scope variable:
$scope.allParcels = $scope.orders
.map(function (elem) {
return elem.parcels;
}) // get an array where each element is an array of parcels.
.reduce(function (previousValue, currentValue) {
return previousValue.concat(currentValue);
}); // concat each array of parcels into a single array of parcels
then on the template, you can use <li ng-repeat='p in allParcels'>{{p.upid}}</li>
If, however, you do not want to place the array on the $scope, I believe you can do something similar to this:
<li ng-repeat="p in orders
.map(function (elem) {
return elem.parcels;
})
.reduce(function (previousValue, currentValue) {
return previousValue.concat(currentValue);
})">{{p.upid}}</li>
although I'm not 100% sure that Angular will evaluate the .map/.reduce in the ng-repeat expression (also having an array generated this way in an ng-repeat is ill-advised since angular would have to constantly generate a new array via map/reduce on each $digest cycle).