Virajsinh

Sweet Alert Conformation With Ajax Call

Sep 27th, 2025 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.75 KB | Source Code | 0 0
  1. <script type="text/javascript">
  2.  
  3. $(document).on('click', '#employee_type_restore', function(event) {
  4.         event.preventDefault();
  5.         /* Act on the event */
  6.  
  7.         var id = $(this).data('id');
  8.  
  9.         // Select parent row
  10.         const parent = event.target.closest('tr');
  11.  
  12.         // Get permission name
  13.         const firstColName = parent.querySelectorAll('td')[1].innerText;
  14.  
  15.         // SweetAlert2 pop up --- official docs reference: https://sweetalert2.github.io/
  16.         Swal.fire({
  17.             // text: "Are you sure you want to Restore " + firstColName + "?",
  18.             html: "Are you sure you want to Restore <strong>" + firstColName + " </strong>?", // Wrap firstColName in <strong> for bold text
  19.             icon: "warning",
  20.             showCancelButton: true,
  21.             buttonsStyling: false,
  22.             confirmButtonText: "Yes, Restore!",
  23.             cancelButtonText: "No, cancel",
  24.             customClass: {
  25.                 confirmButton: "btn fw-bold btn-success",
  26.                 cancelButton: "btn fw-bold btn-active-light-primary"
  27.             }
  28.         }).then(function (result) {
  29.             if(result.value)
  30.             {
  31.                 var formData = new FormData();
  32.                 let ajax_url = backend_url+'/employee-types/'+id+'/restore';
  33.                 let _method = 'POST';
  34.  
  35.                 formData.append('_method', _method);
  36.                 $.ajax({
  37.                     url: ajax_url,
  38.                     type: 'POST',
  39.                     data: formData,
  40.                     cache: false,
  41.                     contentType: false,
  42.                     processData: false,
  43.                     dataType: 'json',
  44.                     headers: {
  45.                         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  46.                     },
  47.                     beforeSend: function() {
  48.                         // ✅ Show loading indicator
  49.                         Swal.fire({
  50.                             title: 'Restoring...',
  51.                             html: 'Please wait while we restore the record.',
  52.                             allowOutsideClick: false,
  53.                             allowEscapeKey: false,
  54.                             didOpen: () => {
  55.                                 Swal.showLoading();
  56.                             }
  57.                         });
  58.                     },
  59.                     success: function (res)
  60.                     {
  61.                         if(res.status == true)
  62.                         {
  63.                             Swal.fire({
  64.                                 // text: "You have Restored " + firstColName + "!.",
  65.                                 html: "You have Restored <strong>" + firstColName + " </strong> !", // Wrap firstColName in <strong> for bold text
  66.                                 icon: "success",
  67.                                 buttonsStyling: false,
  68.                                 showConfirmButton: false,  // This removes the confirm button
  69.                                 // confirmButtonText: "Ok, got it!",
  70.                                 timer: 2000,  // The alert will close automatically after 2 seconds
  71.                                 timerProgressBar: true,  // Optional: Show a progress bar while waitin
  72.                                 customClass: {
  73.                                     confirmButton: "btn fw-bold btn-primary",
  74.                                 }
  75.                             });
  76.  
  77.                             toastr.success(res.message); //Message
  78.                             window.LaravelDataTables["employee-types-table"].row($(parent)).remove().draw();
  79.                         }else{
  80.                             //Message
  81.                             toastr.error(res.message); //Message
  82.                         }
  83.                     },
  84.                     error: function(xhr) {
  85.                         // if error occured
  86.                         if (xhr.responseJSON && xhr.responseJSON.errors) {
  87.                             $.each(xhr.responseJSON.errors, function(key, value) {
  88.                                 toastr.error(value);
  89.                             });
  90.                         } else if (xhr.responseJSON && xhr.responseJSON.message) {
  91.                             toastr.error(xhr.responseJSON.message);
  92.                         } else {
  93.                             toastr.error('An unexpected error occurred.');
  94.                         }
  95.                     },
  96.                     complete: function(xhr, status) {
  97.                         // var res = JSON.parse(xhr.responseText);
  98.                         // Remove Loader
  99.  
  100.                         // ✅ Close loader no matter what
  101.                         Swal.close();
  102.                     },
  103.                     statusCode: {
  104.                         404: function(xhr) {
  105.                             // Get Response ==> xhr.responseJSON
  106.                             // alert("page not found");
  107.                         },
  108.                         403: function(xhr) {
  109.                             // alert("Forbidden");
  110.                         },
  111.                         419: function(xhr) {
  112.                             // alert("CSRF Token Not Valid / Session Invalid");
  113.                         },
  114.                         422: function(xhr) {
  115.                             // alert("Unprocessable Entity");
  116.                         },
  117.                         500: function(xhr) {
  118.                             // alert("Internal Server Error");
  119.                         }
  120.                     }
  121.                 });
  122.             }
  123.         });
  124.     });
  125. </script>
Advertisement
Add Comment
Please, Sign In to add comment