Laravel:控制器销毁功能的模态确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25571120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:01:15  来源:igfitidea点击:

Laravel: modal confimation on the controller destroy function

phplaraveleloquent

提问by Phil

I have a delete button used to delete a record like so

我有一个删除按钮用于删除这样的记录

{{Form::open(array('method'=>'DELETE', 'route' => array('users.destroy', $user->id)))}}
{{Form::submit('Delete', array('class'=>'btn btn-danger'))}}
{{Form::close()}}

The form routes to the controller destroy function which I have like so

表单路由到我喜欢的控制器销毁功能

public function destroy($id)
{                                                          
  User::find($id)->delete();
  return Redirect::route('users.index');
}     

But I want a confirmation alert to pop up before deleting the record. Is there a way for the controller to open a modal dialog and get the return value? or does the controller open a view that opens a modal that yet again directs to another controller that deletes or a controller that reroutes...so confused by how the controller is supposed to control the logic this way...

但我希望在删除记录之前弹出确认警报。控制器有没有办法打开模态对话框并获取返回值?或者控制器是否打开一个视图,打开一个模式,该视图再次指向另一个删除的控制器或一个重新路由的控制器......对控制器应该如何以这种方式控制逻辑感到困惑......

I have twitter bootstrap and jquery in my <head>section like so

我的部分中有 twitter bootstrap 和 jquery,<head>就像这样

<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">    |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

回答by Walid Ammar

There is nothing you have to do with your controller, just some JavaScript will do the trick.

您与控制器无关,只需一些 JavaScript 即可解决问题。

Since you are using bootstrap the easiest way would be using BootstrapDialog.

由于您使用的是引导程序,最简单的方法是使用BootstrapDialog

{{Form::open(array('method'=>'DELETE', 'route' => array('users.destroy', $user->id)))}}
{{Form::submit('Delete', array('class'=>'btn btn-danger'))}}
{{Form::close()}}

<script>
$(document).ready(function(){
    $('form').submit(function(e){
      e.preventDefault();
        url = $(this).parent().attr('action');
        BootstrapDialog.confirm('Are you sure you want to delete?', function(result){
            if(result) {
                $.ajax(url);
            }
        });
    });

});
</script>

Notedon't forget to add the following after Bootstrap and jQuery includes.

注意不要忘记在 Bootstrap 和 jQuery 包含之后添加以下内容。

<script src="your link to bootstrap-dialog.js"></script>

You can download it from here.

你可以从这里下载。

Working Demo

工作演示