I'm wondering how to add and remove specific elements from dynamically added sections. I'm fighting with it for a week and can't find the solution for my case.
Problem is clearly described in the images. To sum up the biggest problem is that the removing buttons don't know to which element they belong.
<template>
<div>
<div class="section" v-for="(el, index) in sections" :key="index" >
<td>
SECTION:{{index}}
<div class="element" v-for="(sel, index) in sections" :key="index">
<span>PICK UP YOUR ANIMAL</span>
<select >
<option v-for="(item, index) in zoo" :key="index" v-bind:value="item">
<option>{{item.name}}</option>
</option>
</select>
<button class="rem el" @click='removeElement(index, el)'>-</button>
</div>
</td>
<button class="addEleme" @click='addElement'>add element</button>
<button class="rem sec" @click='removeSection'>-</button>
</div>
<button class="addSect" @click='addSection'>add section</button>
<p>{{test}}</p>
<span>{{sections}}</span>
<span>{{elements}}</span>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data() {
return {
zoo: [
{name: "Crocodyle Sara"},
{name: "Elepfhant Mike"},
{name: "Lion Monika"},
{name: "Shark Robert"},
{name: "Zebra Antony"}
],
sections:[],
elements:[],
test:"",
i:1
}
},
methods:{
// Sections meth
//----------------------------------------//
addSection(){
this.sections.push(this.zoo)
this.i+=1
this.test=`gucci: check console`
console.log('this.sections: ', this.sections);
},
removeSection(index){
this.sections.splice(index, 1)
this.test="remove section"
},
// Elemnts meth
//----------------------------------------//
addElement(){
this.test=`not gucci: should add element`
},
removeElement(index){
this.elements.splice(index, 1)
this.test="not gucci should remove element"
}
}
}
</script>
