VueJS is data driven, so forget about the direct manipulation of the DOM.
In the example below, you will see that I defined an array inputs- the place where all the rows will be stored - so it will be an array of objects.
In our template, we repeat the array inputs, and for each input we also send an index - required to delete a row.
addRow - push inputs ( ) .
: http://jsbin.com/zusokiy/edit?html,js,output
:
<div id="app">
<ul>
<li v-for="(input, index) in inputs">
<input type="text" v-model="input.one"> - {{ input.one }}
<input type="text" v-model="input.two"> - {{ input.two }}
<button @click="deleteRow(index)">Delete</button>
</li>
</ul>
<button @click="addRow">Add row</button>
</div>
JS:
const app = new Vue({
el: '#app',
data: {
inputs: []
},
methods: {
addRow() {
this.inputs.push({
one: '',
two: ''
})
},
deleteRow(index) {
this.inputs.splice(index,1)
}
}
})
- , , , .