Hi,
i'm strunggling to get the correct value from v-for.
data() {
return {
categories: [],
category_update: {},
editing: 0
}
},
created() {
this.categories();
},
methods: {
categories() {
axios.get('/api/categories')
.then(response => {
this.categories = response.data.data;
});
},
editCategory(index){
this.editing = index;
this.category_update = this.categories[this.editing];
console.log('Edited ' + this.editing);
},
cancelEditing(index){
this.editing = 0;
console.log('Cancel ' + index);
}
}
}
And in the template :
<tr v-for="(category, index) in categories" :key="category.id">
<th scope="row">{{ index + 1 }}</th>
<td>
<a href="#" class="btn btn-primary btn-sm"
@click.prevent="editCategory(index +1)" v-if="editing !== index +1">
<i class="flaticon-edit-1"></i> Edit
</a>
</td>
</tr>
The issue I have is the Index which is giving me the wrong ID.
When I click on edit, calling the function editcategory(indexbut the bind value is from the second entry not the first.
I would say it's normal because I do the index + 1it's the same thing when using category.id.
On my VUEJS devtools, the category_update value id is 2 for the index 1.
Can someone point me to the right direction ?