Is it possible to access an element within the nested v-for loop by using the refs index of the element? I mean, I'm trying to focus a textbox that is within the nested v-for loop which I used to access by its refs index. It works fine for a single v-for loop but not with nested.
For more details here's my loop structure:
This works
<div v-for="(comItem, index) in commentItems" :key="comItem.commentId">
<textarea ref="addRep" ></textarea>
</div>
this.$nextTick(() => {
this.$refs.addRep[index].focus()
});
This won't work
<div v-for="(cont, i) in contentItems" :key="cont.contentId">
...
<div v-for="(comItem, index) in commentItems" :key="comItem.commentId">
<textarea ref="addRep" ></textarea>
</div>
</div>
this.$nextTick(() => {
this.$refs.addRep[index].focus()
});
Or
this.$nextTick(() => {
this.$refs.addRep[i].focus()
});
With the nested html v-for loop structure. The focus will just jump around anywhere. To anyone who encountered this kind of scenario. Please assist me if you know the solutions. Thanks.
$nextTickcallback where are you getting the values forindexandi? Have you tried logging the value ofthis.$refs.addRepwithin that callback to see whether the array exists and what is in it? Is the problem simply trying to calculate the appropriate array index within a nested loop?:key="contItem.commentId"be:key="comItem.commentId"? I don't seecontItemdefined anywhere. 2. Is there a link between the outer and inner loop? As presented in the question the inner loop is just churning out the same list ofcommentItemsfor eachcontitem. I would expect it to be something likev-for="(comItem, index) in cont.commentItems". I know this sounds like an unimportant detail but it makes a huge difference to how to calculate the appropriate index.