new Vue({ el: '#app', data: { new: '', contacts: ['(11) 2423-2324', '(11) 4356-6567', '(31) 3976-3345'] }, methods: { handler (index, e) { const contact = e.target.value if (!contact) { console.log('removing item') this.contacts.splice(index, 1) if (this.contacts.length > 0) { this.focusLastField() } } else { console.log('updating item') this.contacts.splice(index,1,contact) } }, add () { if (this.new != '') { this.contacts.push(this.new) this.new = '' this.focusLastField() } }, focusLastField () { const lastIndex = this.contacts.length - 1 const selector = `[data-index="${lastIndex}"]` Vue.nextTick(() => { const field = document.querySelector(selector) if (field != null) { field.focus() } }) }, } })
<div id="app"> <pre>{{ $data | json }}</pre> <div v-for="contact in contacts" track-by="$index"> <input :data-index="$index" type="text" :value="contact" @input="handler($index, $event)"> </div> <div> <input type="text" v-el:input-new v-model="new" @input="add" placeholder="Add a number"> </div> </div>