如何在删除时添加模态

时间:2017-10-30 07:40:22

标签: javascript php jquery codeigniter

如何在删除按钮上添加模型类。

目前我正在获取默认确认对话框,但我想在php CodeIgniter中使用不同的模态框。

function delete_data(id, type) {
    var r = confirm("<?php echo $this->lang->line('delete_confirmation'); ?>");
    if (r == true) {
        // var type = $('#type').val();
        $.ajax({
            url: "<?php echo BASE_URL . 'admin/delete'; ?>",
            type: 'POST',
            data: 'id=' + id + '&type=' + type,
            success: function (data) {
                if (data == 1) {
                    Materialize.toast('delete_success', 'top left', 'green', "<?php echo ('deleted_successfully'); ?>");
                    //window.location='<?php echo BASE_URL; ?>'+'admin/student_list';
                    location.reload();
                } else {
                    Materialize.toast('delete_error', 'top left', 'red', "<?php echo ('delete_error'); ?>");
                }
            }
        })
    } else {
    }
}

 <a href="#" onclick="delete_data(<?php echo $row['id'];?>,'student');" class="btn-floating  red darken-2"><i class="material-icons right">close</i></a>

1 个答案:

答案 0 :(得分:2)

你可以试试这个。我从Materialize主题网站

中获取了代码

http://demo.geekslabs.com/materialize-v1.0/advanced-ui-sweetalert.html

并与上述功能一起粘贴。

为什么是,它完全没有经过测试。 =)

function delete_data(id, type) {

    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!',
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function(isConfirm){
        if (isConfirm){
            $.ajax({
                url: "<?php echo BASE_URL . 'admin/delete'; ?>",
                type: 'POST',
                data: 'id=' + id + '&type=' + type,
                success: function (data) {
                    if (data == 1) {
                        swal("Deleted!", "Your imaginary file has been deleted!", "success");

              //  Materialize.toast('delete_success', 'top left', 'green', "<?php echo ('deleted_successfully'); ?>");
                //window.location='<?php echo BASE_URL; ?>'+'admin/student_list';
                        location.reload();
                    } else {
                        swal("Cancelled", "Your imaginary file is safe :)", "cancel");
               // Materialize.toast('delete_error', 'top left', 'red', "<?php echo ('delete_error'); ?>");
                    }
                }
            })

        } else {

            swal("Cancelled", "Your imaginary file is safe :)", "cancel");
        }
    });
});

此外,此代码来自Materialize主题网站,字面上如图所示,只是包装在jQuery事件监听器中。

$('.btn-warning-cancel').click(function(){
    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!',
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function(isConfirm){
        if (isConfirm){
            swal("Deleted!", "Your imaginary file has been deleted!", "success");
        } else {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
    });

});