Reputation: 21
I have a form where the user select a number between 1 and 4 with a select tag, with the property of v-model="screens"
.
According to the number selected, with v-for="screen in screens"
new select tags with options between 1 and 3 are showed to the user. For example if the user select 3, then 3 select tags are showed.
Then if in the second select the user select the number 3, three inputs are showed to the user also with v-for
.
The problem is that I don't know how to change the id of the inputs so I can save the user info to the database.
Vue.component('select-square', {
template:'#select-square',
components:{
'square-template' : {
template: '#square-template'
}
}
});
new Vue({
el: 'body',
data: {
screens:'',
}
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.min.js"></script>
<select class="form-control" v-model="screens" number>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<div v-for="screen in screens">
<select-square></select-square>
</div>
<template id="select-square">
<select class="form-control" v-model="squares" number>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<square-template v-for="square in squares"></square-template>
</template>
<template id="square-template">
<input>
</template>
Upvotes: 0
Views: 3844
Reputation: 21
Ok. I already figure it out. v-bind="{id: square}"
make the trick
Vue.component('select-square', {
template:'#select-square',
data: function (){
return {squares:''}
},
components:{
'square-template' : {
template: '#square-template'
}
}
});
new Vue({
el: 'body',
data: {
screens:'',
}
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.min.js"></script>
<select class="form-control" v-model="screens" number>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<div v-for="screen in screens">
<select-square></select-square>
</div>
<template id="select-square">
<select class="form-control" v-model="squares" number>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<square-template v-bind="{id: square}" v-for="square in squares"></square-template>
</template>
<template id="square-template">
<input>
</template>
Upvotes: 2
Reputation: 1324
Well, in your ready method you can watch for the screens changes like
data: function(){
return {
screens: 0,
newSelectScreens: 0
}
},
ready: function(){
this.$watch('screens', function(){
this.newSelectScreens = this.screens;
});
}
Then for the new select screens
<select v-for="n in newSelectScreens">
<options></options>
</select>
Upvotes: 0