2017-05-07 90 views
0

我在编码PHP laravel初学者,请帮助laravel确认删除

我用laravel 5.3,我想打一个确认使用删除SweetAlert

我已经安装槽"npm install"和它已经在node_modules文件夹

的问题是什么我必须在使用这个模块去完成。

谢谢

+1

代码添加到您的删除按钮使用它? – ceejayoz

回答

1
我一直用这个

;

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

先加下面addDeleteForms功能的JavaScript功能齐全

/** 
* 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>