使用ajax在codeigniter中使用sweetalert js删除记录

时间:2016-07-13 06:39:04

标签: codeigniter sweetalert

我想使用sweetalert js删除记录。 这是我的查看文件: -

<a class="action-link delete" title="delete" href="#" onclick="confirmDelete()"> <i class="glyphicon glyphicon-trash"></i> </a>

这是我的功能: -

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");
            }
        });
    });
}

这是我的控制器删除功能

public function delete(){
        $id = $this->input->post('id');
        $data[ 'status'] = '0';
        $where = array( 'id' => $id );
        $this->model->update( $this->tablename , $data , $where );

    }

但不删除数据。请帮帮我。

1 个答案:

答案 0 :(得分:0)

编辑:网址如下

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");
                }
            });
        });
    }