How to iterate nested nested json array? - vue.js

I am trying to iterate nested json array, but I can't figure out how to do it. Here is my code:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<div v-for="agreg in mydata.aggregated">
<p>
<!-- I need to iterate over nested i.e: s_inn, s_kpp -->
</p>
</div>
</div>
App code:
new Vue({
el: '#app',
data: {
mydata: [{
"t_registration_number": "0177200000918001354",
"aggregated": [{
"aaa": "8414440",
"bbb": "95101",
"nested": [{
"s_inn": "1111111",
"s_kpp": "2222222"
}
]
}, {
"aaa": "45770520",
"bbb": "04641",
"nested": [{
"s_inn": "3333333",
"s_kpp": "4444444"
}
]
}
]
}
]
}
})
https://jsfiddle.net/k37e28sp/1/

mydata itself is an array, it doesn't have a property called aggregated, it has a list of objects which contain aggregated. So you have to first loop over mydata.
Js Fiddle: https://jsfiddle.net/3r9aefqu/
Template
<script src="https://unpkg.com/vue"></script>
<div id="app">
<!-- For each object in my mydata -->
<div v-for="data in mydata">
{{ data.t_registration_number }}
<!-- For each aggregated inside each of mydata -->
<div v-for="agg in data.aggregated">
aaa :{{ agg.aaa }} bbb: {{ agg.bbb }}
<div v-for="n in agg.nested">
s_inn : {{n.s_inn}} s_kpp: {{ n.s_kpp }}
</div>
=======================
</div>
</div>
</div>
Vue
new Vue({
el: '#app',
data: {
mydata: [{
"t_registration_number": "0177200000918001354",
"aggregated": [{
"aaa": "8414440",
"bbb": "95101",
"nested": [{
"s_inn": "1111111",
"s_kpp": "2222222"
}]
}, {
"aaa": "45770520",
"bbb": "04641",
"nested": [{
"s_inn": "3333333",
"s_kpp": "4444444"
}]
}]
}]
}
})
Output
0177200000918001354
aaa :8414440 bbb: 95101
s_inn : 1111111 s_kpp: 2222222
=======================
aaa :45770520 bbb: 04641
s_inn : 3333333 s_kpp: 4444444
=======================

Related

How to render block conditionally using vue.js?

var app = new Vue({
el: '#app',
data: {
products:[
{
product_id:'21221312',
product:[
{variation:'paper', price:'12'}
]
},
{
product_id:'2122131212',
product:[
{variation:'ebook', price:'122'}
]
},
{
product_id:'21221312212',
product:[
{variation:'aduio', price:'1322'}
]
},
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.15/vue.js"></script>
<div id="app">
<div v-for="(product, index) in products">
<div v-for="variant in product.product">
<p v-if="variant.variation === 'paper'">
{{variant.price}}
</p>
<p v-if="variant.variation==='ebook'">
{{variant.price}}
</p>
<p v-if="variant.variation==='aduio'">
{{variant.price}}
</p>
</div>
</div>
<div>
I am just start learning Vue.js, I have this problem how i can to render variation price conditionally, for example if variation contains paper i want to render only paper not ebook and audio, is this possible with v-if satement?
products:[
{
product_id:'21221312'
variations: [
{
variation:'paper',
price:'44'
},
{
variation:'ebook',
price:'41'
},
{
variation:'audio',
price:'40'
}
],
},
{
product_id:'212213132'
variations: [
{
variation:'paper',
price:'44'
},
{
variation:'ebook',
price:'41'
},
{
variation:'audio',
price:'40'
}
],
},
]
The way you are writing your markup is redundant, whether it be audio, ebook, or paper, it will still just render the price. might as well remove all those v-ifs since they all end up doing the same thing. You can display everything inside the variant and it will display correctly what the product has.
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.15/vue.js"></script>
<div id="app">
<div v-for="(product, index) in products">
<div v-for="variant in product.product">
<p>
Type: {{variant.variation}} | Price: {{variant.price}}
</p>
<p>
</p>
</div>
</div>
<div>
You can check out this JSFiddle of your issue to see how it works and play around with it if you would like

Why v-text is not working to show a String

I tried to show particular string using v-text. But it shows nothing.
Tried code
ccc.handlebars
<div id="app">
<div class="input-group mb-3">
<input type="text" class="form-control" v-text="getValues[0].value">
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
'bulk': {{{bulks}}}
},
computed: {
getValues: function() {
return this.bulk;
}
},
methods: {},
});
</script>
getValues retuens JSON
[
{ name: "Dauth", age: "18", value: "TGFR123" }
]
I want to show TGFR123 in the text box
Above getValues returns the correct JSON. But v-text="getValues[0].value" not shows the string. Help me to solve this.
v-text does not work on input.
If you want to set the value of an input use :value or v-model
var app = new Vue({
el: '#app',
data: {
bulk: [{
name: "Dauth",
age: "18",
value: "TGFR123"
}]
},
computed: {
getValues: function() {
return this.bulk;
}
},
})
<script src="https://cdn.jsdelivr.net/npm/vue#2.6.0"></script>
<div id="app">
Bind : <input type="text" :value="getValues[0].value">
v-model : <input type="text" v-model="getValues[0].value">
<div>Result : {{getValues[0].value}}</div>
</div>
With v-model the data will be changed with the user input
v-text
value binding
You can use v-model or v-bind:value to insert this value in your input
var app = new Vue({
el: '#app',
data: {
bulk: [{id: 123, name: "Test", value: "NerF21_346"}]
},
computed: {
getValues() {
return this.bulk[0].value
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<input type="text" v-model="getValues">
</div>
v-model documentation

How should I define a concrete object name in vue components

Vue.component("blog-post", {
props: ["post"],
template: `
<div>
<h3>{{ post.title }}</h3>
<p> #####: {{ post.content }} </p>
</div>
`
});
new Vue({
el: "#blog-post-demo",
data: {
posts: [
{ id: 1, title: "My journey with Vue" },
{ id: 2, title: "Blogging with Vue" },
{ id: 3, title: "Why Vue is so fun" }
]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<div id="blog-post-demo" class="demo">
<blog-post
v-for="post in posts"
v-bind:key="post.id"
v-bind:title="post.title"
v-bind:content="post.id"
></blog-post>
</div>
The above example is not working. But I am able to make the below one work.
Vue.component("blog-post", {
props: ["content", "title"],
template: `
<div>
<h3>{{ title }}</h3>
<p> #####: {{ content }} </p>
</div>
`
});
new Vue({
el: "#blog-post-demo",
data: {
posts: [
{ id: 1, title: "My journey with Vue" },
{ id: 2, title: "Blogging with Vue" },
{ id: 3, title: "Why Vue is so fun" }
]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<div id="blog-post-demo" class="demo">
<blog-post
v-for="post in posts"
v-bind:key="post.id"
v-bind:title="post.title"
v-bind:content="post.id"
></blog-post>
</div>
Could someone explain what am I missing here?
In the first case, your component accept post as the prop, which is what you should pass from the parent component.
Vue.component("blog-post", {
props: ["post"],
template: `
<div>
<h3>{{ post.title }}</h3>
<p> #####: {{ post.content }} </p>
</div>
`
});
new Vue({
el: "#blog-post-demo",
data: {
posts: [
{ id: 1, title: "My journey with Vue" },
{ id: 2, title: "Blogging with Vue" },
{ id: 3, title: "Why Vue is so fun" }
]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<div id="blog-post-demo" class="demo">
<blog-post
v-for="post in posts"
v-bind:post="post"
v-bind:key="post.id"
></blog-post>
</div>

Vue.js and Multiselect - The name 'input' does not exist in the current context

I'm trying to start a method after the value of the component Vue Multiselect has been changed using #input, but I'm getting the following compilation error:
CS0103: The name 'input' does not exist in the current context
Here's my multiselect:
<multiselect v-model="Instalacao.value" label="Serie" track-by="Serie" placeholder="Nº de série" :options="Instalacoes"
:multiple="false" :searchable="true" :allow-empty="false" :disabled="Editando" #input="getTecnicosByRepresentante">
<span slot="noResult">Nenhum técnico encontrado</span>
</multiselect>
This example works as expected: both the watch and the #input handler execute when you select a value. Your problem is probably not in the code that you included here.
new Vue({
el: '#app',
components: {
Multiselect: window.VueMultiselect.default
},
data: {
Instalacao: {
value: null
},
Instalacoes: [{
Serie: 'one',
value: 'Vue.js'
},
{
Serie: 'two',
value: 'Vue-Multiselect'
},
{
Serie: 'three',
value: 'Vuelidate'
}
]
},
watch: {
'Instalacao.value': function(newValue) {
console.log('Updated', newValue);
}
},
methods: {
getTecnicosByRepresentante() {
console.log("Input detected, too");
}
}
})
<script src="https://unpkg.com/vue#latest/dist/vue.js"></script>
<link href="https://unpkg.com/vue-multiselect#latest/dist/vue-multiselect.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vue-multiselect#latest/dist/vue-multiselect.min.js"></script>
<div id="app">
<multiselect v-model="Instalacao.value" label="Serie" track-by="Serie" placeholder="Nº de série" :options="Instalacoes" :multiple="false" :searchable="true" :allow-empty="false" #input="getTecnicosByRepresentante">
<span slot="noResult">Nenhum técnico encontrado</span>
</multiselect>
<pre>{{ Instalacao.value }}</pre>
</div>

Not able to access data variable in script but can in html

I have populated a data variable with an array, and can access its contents by using a v-for in the html, but I can't access any of the data in the variable within the script, and I don't know why.
var result = [{
"CatalogName": "Retro Doors",
"ItemName": "French Doors",
"ItemListPrice": "$461.00",
"ItemType": "Oak",
"ItemFeatures": [{
"Features": "Door Quantity",
"QTY": 2
},
{
"Features": "Door Hinges",
"QTY": 4
},
{
"Features": "Door Knobs",
"QTY": 1
},
{
"Features": "Door Looks",
"QTY": 1
},
{
"Features": "Glass Panes",
"QTY": 2
}
]
}];
new Vue({
el: '#app',
beforeCreate: function() {
console.log("Before Created");
},
created: function() {
console.log("Created");
this.GetItemsList();
},
beforeMount: function() {
console.log("Before Mount");
},
data: {
itemPriceList: []
},
methods: {
GetItemsList() {
this.itemPriceList = result;
}
},
mounted: function() {
console.log("Mounted");
console.log(this.ItemPriceList);
}
});
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.17/dist/vue.js"></script>
<div id="app">
{{ itemPriceList[0].CatalogName }}
<div v-for="item in itemPriceList">
{{ item.ItemName }}
<div v-for="items in item.ItemFeatures">
{{ items.Features }} : {{ items.QTY }}
</div>
</div>
</div>
You have a typo in a variable name inside mounted hook - used with capital I.
In data: itemPriceList
In mounted hook: this.ItemPriceList
Should be the same as defined inside data property.