Skip to content

Instantly share code, notes, and snippets.

@nitishk72
Created August 19, 2022 05:41
Show Gist options
  • Save nitishk72/be5aa24b155b484a4ecf82da0861339e to your computer and use it in GitHub Desktop.
Save nitishk72/be5aa24b155b484a4ecf82da0861339e to your computer and use it in GitHub Desktop.
<template>
<div>
<input type="number" v-model="count" placeholder="Number" ref="counter" />
<button @click="addRow">Add</button>
<table>
<div v-for="index in rowsCount" :key="index">
<tr>
<td>
<input placeholder="Name" :ref="'name' + index" />
</td>
<td>
<input placeholder="Email" />
</td>
</tr>
</div>
</table>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
count: 0,
rowsCount: 0,
};
},
methods: {
addRow() {
const currentCount = this.rowsCount;
this.rowsCount = this.count + Number(currentCount);
this.count = 0;
setTimeout(() => {
const key = "name" + (currentCount + 1);
this.focusElement(key);
});
},
focusElement(key) {
const refs = this.$refs;
const element = refs[key];
console.log(key);
console.log(refs);
console.log(element);
if (element == null) return;
element.focus();
},
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment