duanfan5012 2017-09-08 01:48
浏览 295
已采纳

(Laravel)删除按钮上的确认模型

I have a view which display a table of users from the database and the last column has delete button on it which currently work fine for me but i want to display confirmation model and when the user click on Delete button it will delete the selected use

This is the current code for delete button which is work fine:

<a href="{{ route('deleteUser', ['user_id' => $user->id]) }}"><button type="button" class="btn mr-0 mb-0 btn-outline-primary btn-sm"><i class="icon-trash3"></i></button></a>

Now i used this model which i have to click on delete button in order to delete the selected user

<div class="modal fade text-xs-left" id="iconModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel2"><i class="icon-warning2"></i> Confirmation Message</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure that you want to <strong>Delete</strong> this user ?</p>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn grey btn-outline-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-outline-primary">Delete</button>
            </div>
        </div>
    </
  • 写回答

2条回答 默认 最新

  • doulingna9420 2017-09-12 01:46
    关注

    You do not need to generate a modal for each users. Just build a single modal and use data- attributes for dynamic parts.

    So, as minimal, use data- attribute for every button, e.g. data-user-id="{{ }}" to use in form action:

    <a href="#" class="btn mr-0 mb-0 btn-outline-primary btn-sm" data-toggle="modal" data-target="#deleteUserConfirmation" data-user-id="{{ $user->id }}"><i class="icon-trash3"></i></a>
    

    In the modal window use a form with delete method:

    <form id="deleteUserForm" method="POST">
    {{ method_field("DELETE") }}
    <button class="btn btn-danger" type="submit">DELETE</button>
    </form>
    

    In script use:

    <script>
      $('#deleteUserForm').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget);
      $('#deleteUserForm').attr('action', '/user/' + button.data('user-id'));
    });
    </script>
    

    In route use:

    Route::delete('/user/{user}', 'UserController@destroy');
    

    P.S. You may also use other data attributes ti use dynamically e.g. data-user-name="{{ }}" to show the user's name will be deleted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题