laravel确认删除与sweetalert

时间:2017-05-07 18:13:58

标签: laravel sweetalert

我是编写php laravel的初学者,请帮忙

我使用laravel 5.3并且我想通过使用SweetAlert

进行确认删除

我已经安装了"npm install",它已经在node_modules文件夹

问题是接下来我必须要做的就是使用这个模块。

谢谢

2 个答案:

答案 0 :(得分:2)

我一直在用这个;

JS代码

$('button#deleteButton').on('click', function(e){
var name = $(this).data('name');
e.preventDefault();
swal({
    title: "Careful!",
    text: "Are you sure you want to delete "+name+"?",
    icon: "warning",
    dangerMode: true,
    buttons: {
      cancel: "Exit",
      confirm: "Confirm",
    },
})
.then ((willDelete) => {
    if (willDelete) {
       $(this).closest("form").submit();
    }
});

});

在视图中,您在表单内创建一个按钮,并添加数据名称和要删除的元素的名称;

<button type="submit" id="deleteButton" data-name="{{ $model->name }}" class="btn btn-xs btn-danger">Delete</button>

答案 1 :(得分:-1)

首先在javascript ready函数中添加 addDeleteForms 函数

/**
 * Allows you to add data-method="METHOD to links to automatically inject a form
 * with the method on click
 *
 * Example: <a href="{{route('customers.destroy', $customer->id)}}"
 * data-method="delete" name="delete_item">Delete</a>
 *
 * Injects a form with that's fired on click of the link with a DELETE request.
 * Good because you don't have to dirty your HTML with delete forms everywhere.
 */
function addDeleteForms() {
    $('[data-method]').append(function () {
        if (! $(this).find('form').length > 0)
            return "\n" +
                "<form action='" + $(this).attr('href') + "' method='POST' name='delete_item' style='display:none'>\n" +
                "   <input type='hidden' name='_method' value='" + $(this).attr('data-method') + "'>\n" +
                "   <input type='hidden' name='_token' value='" + $('meta[name="_token"]').attr('content') + "'>\n" +
                "</form>\n";
        else
            return "";
    })
    .removeAttr('href')
    .attr('style', 'cursor:pointer;')
    .attr('onclick', '$(this).find("form").submit();');
}

例如elete按钮,如:

<a href="{{route('customers.destroy', $customer->id)}}"
    data-method="delete"
    data-trans-button-cancel="Cancel"
    data-trans-button-confirm="Delete"
    data-trans-title="Are you sure?"
    class="btn btn-xs btn-danger"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i></a>
相关问题