In my vue.js application, there is a feature to remove items.
The following code snippet shows the div element:
<div class="ride-delete" @click="delete">
<p>Delete</p>
</div>
This is the function used to handle the click event:
methods: {
delete ()
{
swal({
title: "Are you sure?",
text: "This action cannot be undone!",
cancelButtonText: 'Cancel',
type: "error",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete this ride.",
closeOnConfirm: false
}, () => {
RideService.destroy(this.ride)
.then(() => {
swal({
title: "Ride successfully deleted",
type: "success",
showCancelButton: false,
timer: 2000,
showConfirmButton: false
});
this.$router.go('/administration/rides');
});
});
}
}
To prevent sending multiple requests when the user clicks rapidly, the button should be disabled after the first click.
--EDIT--
import swal from 'sweetalert';
import RideService from '../../services/RideService';
export default {
data () {
return {
ride: { user: {}, location: {}, type: {} },
deleting: false
}
},
route: {
data ({ to }) {
return RideService.show(this.$route.params.rideId)
.then(function(data)
{
this.ride = data.data.ride;
}.bind(this));
}
},
methods: {
remove ()
{
if (!this.deleting) {
this.deleting = true
swal({
title: "Are you sure?",
text: "This action cannot be undone!",
cancelButtonText: 'Cancel',
type: "error",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete this ride.",
closeOnConfirm: false
}, () => {
RideService.destroy(this.ride)
.then(() => {
swal({
title: "Ride successfully deleted",
type: "success",
showCancelButton: false,
timer: 2000,
showConfirmButton: false
});
this.deleting = false
this.$router.go('/administration/rides');
});
});
this.deleting = false
}
}
}
}
</script>
--EDIT 2--
<template>
<div class="row center">
<div class="panel ride">
<div class="ride-title bar-underline">
<div class="ride-item-title">
<strong class="ride-item-title-body">Ride on {{ ride.created_at }}</strong>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Name</p>
</div>
<div class="ride-item-content">
<p>{{ ride.user.name }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>From Location</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.from }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>To Location</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.from }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Description</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.description }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Kmz</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.kmz }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>kmp</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.kmp }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Hours</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.hour }} hours</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Google maps</p>
</div>
<div class="ride-item-content">
<p>{{ ride.location.maps }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Date</p>
</div>
<div class="ride-item-content">
<p>{{ ride.created_at }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Time</p>
</div>
<div class="ride-item-content">
<p>{{ ride.time }}</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Billable Time</p>
</div>
<div class="ride-item-content">
<p>{{ ride.billabletime }} hours</p>
</div>
</div>
<div class="ride-item bar-underline">
<div class="ride-item-title">
<p>Type</p>
</div>
<div class="ride-item-content">
<p>{{ ride.type.name }}</p>
</div>
</div>
<div class="ride-item">
<div class="ride-edit">
<p>Edit</p>
</div>
<div class="ride-delete" @click="remove">
<p>Delete</p>
</div>
</div>
</div>
</div>
</template>
<script>
import swal from 'sweetalert';
import RideService from '../../services/RideService';
export default {
data () {
return {
ride: { user: {}, location: {}, type: {} },
processing: false
}
},
route: {
data ({ to }) {
return RideService.show(this.$route.params.rideId)
.then(function(data)
{
this.ride = data.data.ride;
}.bind(this));
}
},
methods: {
remove ()
{
if (this.processing === true) {
return;
}
this.processing = true
swal({
title: "Are you sure?",
text: "This action cannot be undone!",
cancelButtonText: 'Cancel',
type: "error",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete this ride.",
closeOnConfirm: false
}, () => {
RideService.destroy(this.ride)
.then(() => {
swal({
title: "Ride successfully deleted",
type: "success",
showCancelButton: false,
timer: 2000,
showConfirmButton: false
});
this.processing = false
this.$router.go('/administration/rides');
});
});
this.processing = false
}
}
}
</script>