I've got a nest array of objects, like this:
taskList: [{
taskName: "Number One"
},{
taskName: "Number Two",
children: [{
taskName: "Child One"
},{
taskName: "Child due"
}]
},{
taskName: "Number Three"
}]}
I loop through this list with a nested v-for and create an input for every element like this:
<div v-for="(task, index) in taskList">
<input :ref="'inputField' + index" type="text" v-model="task.taskName" @keydown.up="arrowPress(index, -1)">
<div v-for="(childOne, childOneIndex) in task.children">
<input ref="inputField" type="text" v-model="task.taskName" @keydown.up="arrowPress(childOneIndex, -1)">
I've setup an event that allows me to move focus up/down through these inputs with arrow-keys. The method for it looks like this:
arrowPress(index, value) {
this.$nextTick(() => this.$refs['inputField'+ (value + index)][0].focus());
}
This works well for the parent.
But I want to be able to move between the children as well. I.e. with focus in "Number 3" when I press up I want to go to "Child Two" and then to "Child One" and then to "Number Two", etc.
I can see some solutions to this but haven't figured out how to get any of them to work:
- Change
:ref="'inputField' + index"toref="inputField". But how do I then know what inputField is calling to change it +-1? E.g. how to I go from inputField2 to inputField1 in my method? - Add a general counter that does ++ whenever an input is added, and use
:ref="'inputField' + counter". However whenever I try to do that by adding{{counter++}}after the v-for div I get an infite loop. - Any other ideas?
Thanks in advance!