Laravel/javascript Modal fail to appear - laravel

is there anything wrong with my id=submit_fw in the blade, the modal did not appear and I already addressed it to the text/javascript below, the button will submit the data in the show.blade and it supposed to have a confirmation message.
show blade
<div class="col-3 col-xs-3 col-sm-3 col-md-1 col-lg-1 col-xl-1 p-0 mt-0"></div>
#if($fw->fw_current_stg == "UR" && $fw->fw_user == $user)
<div class="col-12 col-xs-12 col-sm-12 col-md-5 col-lg-5 col-xl-5 p-0 mt-0 text-right">
<button type="button" class="btn btn-primary btn-block text-white" id="submit_fw">
<span class="spinner-border spinner-border-sm d-none" id="sp_submit" aria-hidden="true"></span>
<i class="fas fa-check" id="icon_submit"></i> SUBMIT REQUEST
</button>
#endif
text/javascript
$('#submit_fw').click(function()
{
$('#sp_submit').removeClass('d-none');
$('#icon_submit').addClass('d-none');
Swal.fire({
title: 'Proceed PR?',
text: 'Are you sure you want to submit this PR to Department Head?',
icon:'question',
confirmButtonText: 'YES',
confirmButtonColor: '#fa0031',
cancelButtonColor: '#fa0031',
showCancelButton:'true',
cancelButtonText:'NO',
}).then((result) => {
if (result.value) {
$.ajax({
url: "{{route('fw.submitfw')}}",
type:"post",
data:{
fw_number:$('#fw_number').val(),
},
success: function(result){
if(result == "submitted")
{
Swal.fire({
title: 'Success',
text: 'Your PR has been submitted to your Department Head',
icon: 'success',
confirmButtonText: 'Return to List',
confirmButtonColor: '#fa0031',
}).then((result) => {
if (result.value) {
let url = "{{route('fw.index')}}";
document.location.href=url;
}
});
}
}
});
}
else
{
}
});
$(this).prop('disabled',true);
});

First of all check id=submit_fw is only one time used in your blade file.
Then you simply set addEventListener to the id.
var submitButton = document.getElementById('submit_fw');
var submitUrl = "{{route('fw.submitfw')}}";
submitButton.addEventListener('click', function (e) {
e.preventDefault();
$('#sp_submit').removeClass('d-none');
$('#icon_submit').addClass('d-none');
submitButton.disabled = true;
Swal.fire({
title: 'Proceed PR?',
text: 'Are you sure you want to submit this PR to Department Head?',
icon:'question',
confirmButtonText: 'YES',
confirmButtonColor: '#fa0031',
cancelButtonColor: '#fa0031',
showCancelButton:'true',
cancelButtonText:'NO',
}).then((result) => {
if (result.value) {
$.ajax({
url: submitUrl,
type:"post",
data:{
fw_number:$('#fw_number').val(),
},
success: function(result){
if(result == "submitted") {
$('#sp_submit').addClass('d-none');
$('#icon_submit').removeClass('d-none');
submitButton.disabled = false;
Swal.fire({
title: 'Success',
text: 'Your PR has been submitted to your Department Head',
icon: 'success',
confirmButtonText: 'Return to List',
confirmButtonColor: '#fa0031',
}).then((result) => {
if (result.value) {
let url = "{{route('fw.index')}}";
document.location.href=url;
}
});
} else {
Swal.fire({
text: 'Somthing went wrong, please try agian!',
icon: "error",
confirmButtonText: "Ok, got it!",
confirmButtonColor: '#fa0031',
});
$('#sp_submit').addClass('d-none');
$('#icon_submit').removeClass('d-none');
submitButton.disabled = false;
}
},
error: function (jqXHR, textStatus, errorThrown) {
$('#sp_submit').addClass('d-none');
$('#icon_submit').removeClass('d-none');
submitButton.disabled = false;
}
});
} else {
$('#sp_submit').addClass('d-none');
$('#icon_submit').removeClass('d-none');
submitButton.disabled = false;
}
});
});

Related

How to submit in swal?

I have this sweetalert triggered on the submit of a form.
<script src="{{ asset('themes/js/sweetalert.min.js') }}"></script>
<script>
$('.btn-danger').click(function(event) {
event.preventDefault();
var form = $(this).parents('#form-delete');
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function(isConfirm){
if (isConfirm) form.submit();
});
});
</script>
But on clicking confirm I want it to continue submitting the form...
<form action="{{ route('admin.blogs.destroy', $blog->id) }}" method="post" id="form-delete">
#csrf
#method('DELETE')
<div class="btn-group btn-group-sm">
edit
<button type="submit" class="btn btn-danger">delete</button>
</div>
</form>
error
The function swal() doesnt accept a second parameter as the callback anymore, user .then()
<script>
$('.btn-danger').click(function(event) {
event.preventDefault();
var form = $(this).parents('#form-delete');
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
})
.then(function (isConfirm) {
if (isConfirm) {
form.submit();
}
});
});
</script>

Calling ActionResult using jquery ajax in .net core

Action in controller
Method GetById is the same as the Find method and EntityUpdate == Update
public IActionResult CategoryStatus(int id)
{
var data = cm.GetById(id);
if (data.CategoryStatus)
{
data.CategoryStatus = false;
}
else
{
data.CategoryStatus = true;
}
cm.EntityUpdate(data);
return RedirectToAction("Index");
}
I want to call this action from razor view with ajax, can anyone help , thanks.
I add some parts of razor view just in case
<a href="/Admin/Category/CategoryStatus/#item.CategoryID"
class="btn btn-outline-success btn-sm"
onclick="return functionConfirm(this)">
Aktivləşdir
</a>
<script>
function functionConfirm(event) {
swal({
title: 'Are you sure?',
text: "You will not be able to recover this item!",
type: 'warning',
showCancelButton: true,
cancelButtonText: 'No',
cancelButtonClass: 'btn btn-danger',
showConfirmButton: true,
confirmButtonText: 'Yes',
confirmButtonClass: 'btn btn-success'
}, function (isConfirm) {
if (isConfirm) {
$.ajax({//I need your help here
type: "GET",
url: '/Admin/Category/CategoryStatus',
success: function (msg) {
console.log(msg);
},
error: function (req, status, error) {
console.log(msg);
}
});
return true;
} else {
return false;
}
});
return false;
}
Change your code like below:
//add this....
<a data-id="#item.CategoryID"
class="btn btn-outline-success btn-sm"
onclick="return functionConfirm()">
Aktivləşdir
</a>
#section Scripts
{
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" integrity="sha512-gOQQLjHRpD3/SEOtalVq50iDn4opLVup2TF8c4QPI3/NmUPNZOk2FG0ihi8oCU/qYEsw4P6nuEZT2lAG0UNYaw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js" integrity="sha512-7VTiy9AhpazBeKQAlhaLRUk+kAMAb8oczljuyJHPsVPWox/QIXDFOnT9DUk1UC8EbnHKRdQowT7sOBe7LAjajQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
function functionConfirm() {
swal({
title: 'Are you sure?',
text: "You will not be able to recover this item!",
type: 'warning',
showCancelButton: true,
cancelButtonText: 'No',
cancelButtonClass: 'btn btn-danger',
showConfirmButton: true,
confirmButtonText: 'Yes',
confirmButtonClass: 'btn btn-success'
}, function (isConfirm) {
var id = $(event).data("id"); //add....
if (!isConfirm) return;
$.ajax({
type: "GET",
url: '/Admin/Category/CategoryStatus/'+id, //modify...
success: function (msg) {
window.location.href="/xxx/Index"
},
error: function (req, status, error) {
console.log(msg);
}
});
});
}
</script>
}

my vue data is not changing when i click at a button

<div class="sections_container" v-for="(section, index) in sections" #click="selected_section_id = section.id">
<div class="section">
<input class="section_name" type="text" name="name" v-model="section.name" #keyup.enter="updateSection(section)">
</div>
<div class="actions">
<div style="margin-right: 10px;">
<button class="btn btn-primary" #click="updateSection(section)"type="submit"> <i class="fa fa-edit"></i></button>
</div>
<div>
<button #click="deleteSection(index)" class="btn btn-danger" type="submit"><iclass="fa fa-trash"></i></button>
</div>
</div>
</div>
The Data is abtained right and here is my data and my computed property
computed: {
selectedSection: () => {
return this.sections.filter((section) => {
console.log('selec')
return this.selected_section_id == section.id;
});
}
},
mounted() {
window.axios.get(route('allSections')).then((res) => {
this.sections = res.data;
});
},
data: function () {
return {
selected_section_id: 1,
new_section_name: '',
sections: [],
groups: [],
questions: []
}
Now When i Click the button the Seletcted_section_id should be changed but nothing happens i check the vue dev tool plugin but nothing changed unless i press the refresh button here is the two functions updateSection and deleteSection for updating and deleting the data does those functions can affect the data is not changing
updateSection(section) {
window.axios.patch(route("section.update", section.id), {name: section.name}).then((res) => {
this.sections = res.data;
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
onOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
Toast.fire({
icon: 'success',
title: 'Updated Successfully'
})
});
},
deleteSection(index) {
Swal.fire({
title: 'Are you sure',
text: "You won't be able to revert this",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it'
}).then((result) => {
if (result.value) {
window.axios.delete(route('section.destroy', this.sections[index])).then(() => {
this.sections.splice(index, 1);
Swal.fire(
'Deleted',
'Your file has been deleted.',
'success'
)
})
}
})
<div class="sections_container" v-for="(section, index) in sections" #click="selected_section_id = section.id">
I assume that the reason why you are directly assigning the selected_section_id to section.id is to debug and check it straightforward. Though not sure if the section will be captured on the #click event, you can try #click="console.log(section, section.id)" if it returns anything.
Otherwise, let's try this process of elimination:
Let's revert above back to your function: <div class="sections_container" v-for="(section, index) in sections" #click="selectedSection">
#click is an event that needs user interactions, I could recommended to use it under methods, so instead of using computed, move the function under methods:
methods: {
selectedSection: () => {
return this.sections.filter((section) => {
console.log('selec')
return this.selected_section_id == section.id;
});
}
}
On your function selectedSection, this line return this.selected_section_id == section.id doesn't assign the section.id because you are using here the comparison operator == thus it doesn't do anything, instead use the regular assign operator:
return this.selected_section_id = section.id
If the above fix doesn't work, you can try going skeletal starting with the function itself, by console.log everything and check if it correctly returns anything, like in this order:
selectedSection: () => {
console.log(this.sections)
}
selectedSection: () => {
return this.sections.filter((section) => {
console.log('check the values of section: ', section, section.id);
return this.selected_section_id = section.id;
});
}
Oh, also, you could try assigning a key to your v-for directive: https://v2.vuejs.org/v2/guide/list.html#Maintaining-State

how to use pjax and sweetalert confirmation to delete row in laravel 5.3

I use sweetalert to get confirm for delete rows from user, the code for confirmation is
$('.delete').click(function(){
var formClass = $(this).attr('id');
$("form."+ formClass).submit(function( event ){
event.preventDefault();
swal({
title: "Do you want delete this item?",
text: "",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'yes!',
cancelButtonText: "No",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal({
title: 'Deleted!',
text: 'the item is deleted!',
type: 'success'
}, function() {
form.submit();
});
} else {
swal("Cansel", "Not deleted :)", "error");
}
});
});
});
and this code for use pjax to have an ajax request in laravel
$(document).on('pjax:send', function() {
$('#loading').show();
$("#pjax-container").css({"opacity": "0.3"});
})
$(document).on('pjax:complete', function() {
$('#loading').hide();
$("#pjax-container").css({"opacity": "1"});
})
$(document).on('submit', 'form[form-pjax]', function(event) {
$.pjax.submit(event, '#pjax-container')
});
And the html form is:
<form action="{{ route('admin.comments.destroy',[$comment->id]) }}" class="inline-div comment-{{ $comment->id }}" method="post" form-pjax>
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-danger btn-opt delete" id="comment-{{ $comment->id }}"><span class="glyphicon glyphicon-trash"></span></button>
By this code i haven't an ajax request.
Instead of "form.submit();" What to write?
From what I can see, you don't need to use pjax for this at all. You can delete a record from database asynchronously, simply using an ajax request. Here is how I would go about doing this:
In the code below, when the user clicks on the delete link, an alert would be presented to him. If he confirms the delete action, then the function deleteFromDB would be called which is responsible for sending an ajax request to the server.
$('.delete').click(function(){
var formClass = $(this).attr('id');
event.preventDefault();
swal({
title: "Do you want delete this item?",
text: "",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes!',
cancelButtonText: "No",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
deleteFromDB(formClass);
} else {
swal("Cancel", "Not deleted", "error");
}
});
});
The mentioned function is defined as below:
function deleteFromDB(formClass) {
var url = "http://example.com/delete/this/record";
var form = $("form."+ formClass);
var formData = new FormData();
form.find("input[name]").each(function(index, input) {
formData.append(input.name, input.value);
});
$.post(url, formData).done(function(data) {
swal({
title: 'Deleted!',
text: 'the item is deleted!',
type: 'success'
});
});
}
It finds all the inputs that have a name attribute and appends each to a FormData object. Then a post ajax request is sent to the server and when the request is done, another alert showing the success of deletion shows up.
Pay attention here not to show the success alert before deleting the record (as you were doing in the above code), because then if something goes wrong during the request, i.e. the record could not be deleted for some reason, you would be able to show that error to the user.

Delete the record using sweetalert js in codeigniter using ajax

I want to delete the record using sweetalert js.
This is my view file:-
<a class="action-link delete" title="delete" href="#" onclick="confirmDelete()"> <i class="glyphicon glyphicon-trash"></i> </a>
This is my function:-
function confirmDelete(){
var id=1;
swal({
title: 'Are you sure?',
text: "You won't be able to delete this!",
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'OK',
closeOnConfirm: false
},function (isConfirm) {
$.ajax({
url: base_url+"admin/mycontroller/delete",
type: "POST",
data: {"id":id},
dataType:"HTML",
success: function () {
swal("Done!", "It was succesfully deleted!", "success");
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
});
}
This is my controller delete function
public function delete(){
$id = $this->input->post('id');
$data[ 'status'] = '0';
$where = array( 'id' => $id );
$this->model->update( $this->tablename , $data , $where );
}
But not delete the data. Please help me out.
Edit: url as below
function confirmDelete(){
var id=1;
swal({
title: 'Are you sure?',
text: "You won't be able to delete this!",
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'OK',
closeOnConfirm: false
},function (isConfirm) {
$.ajax({
url: <?=base_url()?>"admin/mycontroller/delete",
type: "POST",
data: {"id":id},
dataType:"HTML",
success: function () {
swal("Done!", "It was succesfully deleted!", "success");
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
});
}

Resources