Jestem nowym użytkownikiem vue.js (2) i obecnie pracuję nad prostą aplikacją zdarzeń. Udało mi się dodać wydarzenia, ale teraz chciałbym usunąć zdarzenia na podstawie kliknięcia przycisku.Jak usunąć element z tablicy Vue.js
HTML
<div class="list-group">
<div class="list-group-item" v-for="event in events">
<h4 class="list-group-item-heading">
{{ event.name }}
</h4>
<h5>
{{ event.date }}
</h5>
<p class="list-group-item-text" v-if="event.description">{{ event.description }}</p>
<button class="btn btn-xs btn-danger" @click="deleteEvent(event)">Delete</button>
</div>
</div>
</div>
JS (Vue)
new Vue ({
el: '#app',
data: {
events: [
{
id: 1,
name: 'Event 1',
description: 'Just some lorem ipsum',
date: '2015-09-10'
},
{
id: 2,
name: 'Event 2',
description: 'Just another lorem ipsum',
date: '2015-10-02'
}
],
event: { name: '', description: '', date: '' }
},
ready: function() {
},
methods: {
deleteEvent: function(event) {
this.events.splice(this.event);
},
// Adds an event to the existing events array
addEvent: function() {
if(this.event.name) {
this.events.push(this.event);
this.event = { name: '', description: '', date: '' };
}
}
} // end of methods
});
Próbowałem przekazać zdarzenie do funkcji, a nie usuwać, że jeden z funkcją slice, myślałem, że to, że kod do usuwania niektórych danych z tablicy. Jaki jest najlepszy i najczystszy sposób usuwania danych z tablicy za pomocą przycisku simpleb i zdarzenia onclick?
Awesome, już myślałem, że używałem splice źle. Czy możesz mi powiedzieć jaka jest różnica między łączeniem a krojem? Dzięki! – Gijsberts
Pewnie. Zasadniczo sPlice modyfikuje oryginalną tablicę, podczas gdy slice tworzy nową tablicę. Więcej informacji tutaj: http://www.tothenew.com/blog/javascript-splice-vs-slice/ –