douchuxun4162
2014-08-29 15:08
浏览 72
已采纳

Laravel:控制器破坏功能的模态确认

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

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

图片转代码服务由CSDN问答提供 功能建议

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

 <  code> {{Form :: open(array('method'=&gt;'DELETE','route'=&gt; array('users.destroy',$ user-&gt; id)))}} 
 {{  Form :: submit('删除',数组('class'=&gt;'btn btn-danger'))}} 
 {{Form :: close()}} 
   \  n 
 

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

  public function destroy($ id)
 {
 User :: find(  $ id) - &gt; delete(); 
返回Redirect :: route('users.index'); 
} 
   
 
 

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

我的&lt; head&gt; 部分中有twitter bootstrap和jquery,如此

 &lt; link href =“//  netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"rel =“stylesheet”&gt;  | 
&lt; script src =“// ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  \  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxiangshi6568 2014-08-29 15:33
    已采纳

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

    Since you are using bootstrap the easiest way would be using 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>
    

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

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

    You can download it from here.

    Working Demo

    打赏 评论

相关推荐 更多相似问题