My requirement is to make nested data in my form and each of this data has ability to add more rows or delete them. I have done such thing with JavaScript before but with VueJs it's my first attempt so I kind of need push :)
Logic
- Parent data has select option which will let user to select what type of input they need and based on that selection it append the input. (RED)
- My parent data can be added or removed so i can have several parent and each of them several child at the same time (BLUE)
- My child data can be added or removed (GREEN)
Code
Here is my first static html (no script has written for it yet)
Ps: all parts are commented.
<el-form-item label="Variation Name">
<el-col :span="12">
// parent
<el-input placeholder="Please input your variation name" v-model="form.variationParent" class="input-with-select">
// select type of child's input
<el-select v-model="form.variationParent" slot="prepend" placeholder="Select">
<el-option label="Text" value="1"></el-option>
<el-option label="Text Area" value="2"></el-option>
<el-option label="Boolean" value="3"></el-option>
</el-select>
<el-button slot="append" type="success" icon="el-icon-plus"></el-button> //add parent
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button> // delete parent
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9">
// child's
<el-input v-model="form.variationChilds" placeholder="Please input your variation value" class="input-with-select">
<el-button slot="append" type="success" icon="el-icon-plus"></el-button> //add child
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button> // delete child
</el-input>
</el-col>
</el-form-item>
Any kind of idea and sample codes are appreciated.
Update
I have manage to add and remove parent part with this code:
<el-form-item v-for="(index, k) in variationParents" :key="k" label="Variation Name">
<el-col :span="12">
<!-- parent -->
<el-input placeholder="Please input your variation name" v-model="form.variationParent" class="input-with-select">
<el-select v-model="form.variationParent" slot="prepend" placeholder="Select">
<el-option label="Text" value="input"></el-option>
<el-option label="Text Area" value="textarea"></el-option>
<el-option label="Boolean" value="boolean"></el-option>
</el-select>
<el-button slot="append" @click="add(k)" type="success" icon="el-icon-plus"></el-button>
<el-button slot="append" @click="remove(k)" v-show="k || ( !k == variationParents.lenghth > 1)" type="danger" icon="el-icon-delete"></el-button>
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9">
<!-- child's -->
<el-input v-model="form.variationChilds" placeholder="Please input your variation value" class="input-with-select">
<el-button slot="append" type="success" icon="el-icon-plus"></el-button>
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button>
</el-input>
</el-col>
</el-form-item>
data() {
return {
variationParents: [
{
name: '',
type: ''
}
],
}
},
methods: {
add(index){
this.variationParents.push({name: '', type: ''});
},
remove(index){
this.variationParents.splice(index, 1);
},
}
and here is the result + issue
As you see i have new rows of parents and add / remove buttons working but my inputs are all getting same value, meaning if i set first input to something all of them will get same value.
They need to each one have different value and be send to back-end as array.


v-modelon different object (form) instead of what's being looped in thev-for.