Sweetalert2 удалить подтверждение - PullRequest
0 голосов
/ 17 января 2020

Я хочу сделать подтверждение удаления с помощью sweetalert2 в codeigniter, но я не знаю, как это сделать, может кто-нибудь мне помочь ..?

это скрипт sweetalert2

      <script src="<?php echo base_url(); ?>sweetalert2/dist/sweetalert2.all.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/promise-polyfill"></script>
      <script src="<?php echo base_url(); ?>sweetalert2/dist/sweetalert2.min.js"></script>
      <link rel="stylesheet" href="<?php echo base_url(); ?>assets/sweetalert2/dist/sweetalert2.min.css"> 

      <script type="text/javascript">

      function hapus() {  
      event.preventDefault(); 
      var form = event.target.form; 
      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) {
        Swal.fire(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        )
      }
    })}
    </script>

и это кнопка удаления

 <button onclick="hapus()" style="width:70px"type="button" class="btn btn-block btn-outline-danger"><?php echo anchor('postingan/hapus/'.$b->id,'Hapus');  ?></button>

и это контроллер

function hapus($id){
        $where = array('id' => $id);
        $this->m_data->hapus_data($where,'blogs');
        redirect('Postingan');
    }

Ответы [ 4 ]

0 голосов
/ 22 января 2020

Я хочу попытаться ответить, потому что в настоящее время у меня возникает та же проблема, что и удаление данных со сладким предупреждением. поэтому я использую метод с ajax (post) и работаю.

Сначала получаем данные идентификатора в кнопке

 <button onclick="hapus(<?php echo $b->id; ?>)" style="width:70px"type="button" class="btn btn-block btn-outline-danger"><?php echo 'Hapus');  ?></button>

Затем делаем функцию delete для вызова sweetalert

function del(id){
  Swal.fire({
  title: 'Yakin menghapus?',
  text: "Data yang sudhah dihapus tidak dapat dikembalikan!",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Ya, hapus sekarang!'
  }).then((result) => {
    if (result.value) {
      Swal.fire({
        title: 'Terhapus!',
        text: 'Data berhasil dihapus.',
        icon: 'success',
        showConfirmButton: false
      });
      $.ajax({
        type:"POST",
        url: "back/rule/kontak_blog/del", //url function delete in controller
        data:{
          id:id //id get from button delete
        },
        success:function(data){ //when success will reload page after 3 second
         window.setTimeout( function(){ 
             location.reload();
         }, 300 );
        }
      });
    }
  })
}

Удаление контроллера

function del() {
    $id = $this->input->post('id'); //get data from ajax(post)
    $del = $this->M_kontak_blog->del($id);
}

Удаление модели

function del($id) {
    $this->db->where('kb_id', $id);
    $this->db->delete(' tb_kontak_blog');
}

Надеюсь, этот ответ поможет вам

0 голосов
/ 20 января 2020

попробуйте

function hapus($___id) {  
  swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'
    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : `postingan/hapus/${$__id}`,
                type : 'GET',
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });
}

Кнопка

<button onclick="hapus(<?php echo $b->id;  ?>)" style="width:70px"type="button" class="btn btn-block btn-outline-danger">Hapus</button>
0 голосов
/ 21 января 2020

Джанган Лупа включает jQuery Кира - Кира Сеперти Ини

function hapus($___id) {  
  swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'
    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : `postingan/hapus/${$___id}`,
                type : 'GET',
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9.6.1/dist/sweetalert2.all.min.js"></script>

<button onclick="hapus(10)" style="width:70px" type="button" class="btn btn-block btn-outline-danger">Hapus</button>
0 голосов
/ 17 января 2020

Попробуйте это

swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'

    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : 'enter your url',
                type : 'POST',
                data : {id:id },
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });

это контроллер

function hapus($id){
        $where = array('id' => $id);
        $this->m_data->hapus_data($where,'blogs');
        //echo true; OR return true;
    }
...