This is my code based on this link
But I do not get the index of the for cycle, Basically I need a button to delete the element that is clicked on.
The removeChild
method does not work.
ItemClean.vue
<template>
<li>
<div
:class="{bold: isFolder}"
>
{{model.name}}
<span v-show="!isFolder" class="remove" @click="changeType"> [Add] </span>
<span class="remove" @click="removeChild(index)"> [Remove] </span>
</div>
<ul v-if="isFolder">
<item-clean
class="item"
v-for="model, index in model.children"
:key="index"
:model="model"
>
</item-clean>
<li class="add" @click="addChild">+</li>
</ul>
</li>
</template>
<script>
export default {
props: {
model: Object
},
data () {
return {
}
},
computed: {
isFolder: function () {
return this.model.children &&
this.model.children.length
}
},
methods: {
changeType: function () {
if (!this.isFolder) {
Vue.set(this.model, 'children', [])
this.addChild()
}
},
addChild: function () {
this.model.children.push({
name: 'new stuff'
})
},
//Esta funcion no me sirve ya que no obtiene el index
removeChild: function(index){
//Vue.delete(this.model, 'name')
this.$delete(this.model.children, index);
},
}
}
</script>
<style>
.remove, .bold{
cursor: pointer;
}
.bold{
font-weight: bold;
}
</style>
index.html
<ul id="item_clean">
<item-clean
class="item"
:model="treeData">
</item-clean>
</ul>
app.js
const item_clean = new Vue({
el: '#item_clean',
data: {
treeData: {
name: 'My Tree',
children: [
{ name: 'hello' },
{ name: 'wat' },
]
}
}
});