Cancel pada SweatAlert

Saya menggunakan laravel, ajax, dan sweat alert. ini pada operasi hapus data. Ketika klik hapus data, maka sweatalert berhasil muncul dan ketika ditekan button "delete it" data berhasil terhapus. Tetapi ketika saya klik cancel pada sweatalert, data juga tetap terhapus. bagaimana agar dapat dicancel ?

 function deleteData(id){
          var csrf_token = $('meta[name="csrf-token"]').attr('content');
          swal({
              title: 'Are you sure?',
              text: "You won't be able to revert this!",
              type: 'warning',
              showCancelButton: true,
              cancelButtonColor: '#d33',
              confirmButtonColor: '#3085d6',
              confirmButtonText: 'Yes, delete it!'
          }).then(function () {
              $.ajax({
                  url : "{{ url('data') }}" + '/' + id,
                  type : "POST",
                  data : {'_method' : 'DELETE', '_token' : csrf_token, permanent: false},
                  success : function(data) {
                      table.ajax.reload();
                      swal({
                          title: 'Success!',
                          text: data.message,
                          type: 'success',
                          timer: '1500'
                      })
                  },
                  error : function () {
                      swal({
                          title: 'Oops...',
                          text: data.message,
                          type: 'error',
                          timer: '1500'
                      })
                  }
              });
          });
        }
avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Diperbarui 5 tahun yang lalu

8 Jawaban:

pake bawaannya ga bisa gan? <pre> function deleteData(id){ var csrf_token = $('meta[name="csrf-token"]').attr('content'); swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, cancelButtonColor: '#d33', confirmButtonColor: '#3085d6', confirmButtonText: 'Yes, delete it!' }).then((result) =&gt; { $.ajax({ url : "{{ url('data') }}" + '/' + id, type : "POST", data : {'_method' : 'DELETE', '_token' : csrf_token, permanent: false}, success : function(data) { table.ajax.reload(); swal({ title: 'Success!', text: data.message, type: 'success', timer: '1500' }) }, error : function () { swal({ title: 'Oops...', text: data.message, type: 'error', timer: '1500' }) } }); }); } </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 5 tahun yang lalu

Halo gan, klo ane biasanya kek gini

<pre> function deleteData(id){ var csrf_token = $('meta[name="csrf-token"]').attr('content'); swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, cancelButtonColor: '#d33', confirmButtonColor: '#3085d6', confirmButtonText: 'Yes, delete it!' closeOnCancel: true }, function(confirmed){ if (confirmed) { $.ajax({ url : "{{ url('data') }}" + '/' + id, type : "POST", data : {'_method' : 'DELETE', '_token' : csrf_token, permanent: false}, success : function(data) { table.ajax.reload(); swal({ title: 'Success!', text: data.message, type: 'success', timer: '1500' }) }, error : function () { swal({ title: 'Oops...', text: data.message, type: 'error', timer: '1500' }) } }); } }); }; </pre>

Smoga bisa membantu gan.

avatar zergz
@zergz

62 Kontribusi 47 Poin

Dipost 5 tahun yang lalu

tengkyu agan agan

avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

Belum bisa ternyata gan. Sudah saya coba usulan dari @dianarifr ataupun. @zergz

avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

@zergz confirmed itu emang bawaanya sweetalert kah bang?

avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

@newbieprogrammer, bukan gan, itu cuma function untuk cek ketika tombol konfirm di klik, jadi klo tombol cancel di jendela sweetalert akan tertutup tanpa ada action apapun. Sampe hari ini ane pake cara itu lancar2 saja gan. Untuk sweet alertnya mw pake yg package untuk laravel ato download langsung jquery library-nya seharusnya tidak ada masalah. Klo boleh tw sweet alert versi brp yg dipake gan? Klo pakenya sweet alert 2, kode contoh yg ane brikan ga akan berfungsi krn ada beberapa perubahan pada sweet alert 2.

avatar zergz
@zergz

62 Kontribusi 47 Poin

Dipost 5 tahun yang lalu

iyup.. sweet alert 2 yg ane pake

avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

Jawaban Terpilih

[SOLVED] pake ini gan

<pre> function deleteData(id){ var csrf_token = $('meta[name="csrf-token"]').attr('content'); Swal({ title: 'Are you sure?', text: 'You will not be able to recover this imaginary file!', type: 'warning', showCancelButton: true, confirmButtonText: 'Yes, delete it!', cancelButtonText: 'No, keep it' }).then((result) =&gt; { if (result.value) { $.ajax({ url : "{{ url('data') }}" + '/' + id, type : "POST", data : {'_method' : 'DELETE', '_token' : csrf_token, permanent: false}, success : function(data) { table.ajax.reload(); swal({ title: 'Success!', text: data.message, type: 'success', timer: '1500' }) }, error : function () { swal({ title: 'Oops...', text: data.message, type: 'error', timer: '1500' }) } });

            } else if (result.dismiss === Swal.DismissReason.cancel) {
                Swal(
                &#039;Cancelled&#039;,
                &#039;Your imaginary file is safe :)&#039;,
                &#039;error&#039;
                )
            }
        })
      
    } &lt;/pre&gt;
avatar newbieprogrammer
@newbieprogrammer

35 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban