Я начинаю с Vue. js и читал примерно splice()
в соответствии с идентификатором / индексом элемента. Он отлично работает, если я пытаюсь splice()
элемент в "турах" v-for
:
data:{
tours: null,
},
methods: {
deleteTour: function (id) {
if (confirm('Are you sure?')) {
const index = this.tours.findIndex(item => item.id === id);
if (~index) {
axios.post('deleteTour.php', { "token": token, "tourID": id }).then((response) => {
this.deleted = response;
console.log(this.deleted);
// splice tour
this.tours.splice(index, 1);
}).catch((error) => {
this.errored = error;
console.log(this.errored);
});
}
}
},
}
html
<div id="app" class="row mb-50">
<div v-for="(item, index) in tours" v-bind:key="item.id" id="tours" class="col-md-12 mb-30">
<ul class="pics-list">
<li v-for="(image, index) in item.images" ref="deleteItem" :class="{marginRightOpen: editingTour}">
<span :hidden="editingTour !== item.id" class="badge" @click="deleteImage(image.imageID, item.id, index)">
<i class="fa fa-fw fa-times-circle"></i>
</span>
<div class="pics-list-image-container img-fluid cursor-pointer" v-bind:style="{'background-image': 'url(http://localhost/tours/'+image.image + ')' }" @click="openModal = true, showModal(image.image)">
</div>
</li>
<li v-if="urls && editingTour == item.id" v-for="(url, key) in urls" :key="key">
<div id="preview" :ref="'url'" class="pics-list-image-container img-fluid">
</div>
</li>
</ul>
</div>
</div>
Но я не могу splice()
элемент списка во вложенном v-for
. Я пробовал использовать ref
разными способами, но это не сработало.
deleteImage: function (id, tourID, index) {
if (confirm('Are you sure?')) {
// it could be done here
this.$refs.deleteItem.splice(index, 1);
axios.post('deleteImage.php', { "token": token, "tourID": tourID, "imageID": id })
.then((response) => {
console.log(response);
// or here
this.$refs.deleteItem.splice(index, 1);
}).catch((error) => {
this.errored = error;
console.log(this.errored);
});
}
},
Я хочу удалить весь первый элемент списка, содержащий значок значка. Возможно ли это?