【问题标题】:Laravel bootstrap delete confirmation using modalLaravel bootstrap 使用模态进行删除确认
【发布时间】:2020-07-07 20:37:54
【问题描述】:

我在将数据传输到我的删除确认模式时遇到问题。 我已经验证了我的删除路线可以从数据库中删除数据,但我面临的问题是我无法将联系人-> id 传递到模态以访问以进行删除。

模态

<!-- Delete Warning Modal -->
<div class="modal modal-danger fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="Delete" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Delete Contact</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
            <form action="{{ route('contacts.destroy', 'id') }}" method="post">
                @csrf
                @method('DELETE')
                <input id="id" name="id")>
                <h5 class="text-center">Are you sure you want to delete this contact?</h5>
                <input id="firstName" name="firstName"><input id="lastName" name="lastName">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-sm btn-danger">Yes, Delete Contact</button>
            </div>
            </form>
        </div>
    </div>
</div>
        <!-- End Delete Modal --> 

刀锋呼唤

<td>
    <a href="#" 
        data-id={{$value->id}}
        class="btn btn-danger delete" 
        data-toggle="modal" 
        data-target="#deleteModal">Delete</a>
</td>

联系控制器

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // Need to find all addresses with the contacdt Id and delete them.
        Address::where('contact_id', $id)->delete();
        Contact::find($id)->delete();
        return redirect()->route('contacts.index')->with('success','Contact deleted success');   
    }

总而言之,我的问题是让 jQuery 将数据传输到模态,这样我就可以使用它来删除数据......现在我的 id=null

【问题讨论】:

  • 您没有在表单中发送id
  • 这很简单。首先,您在 jquery 中获取点击事件的 id,然后将 id 作为目标输入字段中的值(在您的情况下为 $('#id').val() ),然后您将获得确切的结果。

标签: php jquery laravel modal-dialog


【解决方案1】:

jQuery 代码:

 $(document).on('click','.delete',function(){
         let id = $(this).attr('data-id');
         $('#id').val(id);
    });

也在你的模态 html 代码中:

<input id="id" name="id">

【讨论】:

  • 谢谢!!!我意识到我忘了发布我的 jquery 代码,但是这两个更改有效!!!
【解决方案2】:

在您的引导模式表单中更改以下内容:

<form action="{{ route('contacts.destroy', 'id') }}" method="post">
                @csrf
                @method('DELETE')
                <input id="id" name="id" hidden>
                <h5 class="text-center">Are you sure you want to delete this contact?</h5>
                <input id="firstName" name="firstName"><input id="lastName" name="lastName">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-sm btn-danger">Yes, Delete Contact</button>
            </div>
            </form>

在上面的代码中,您将字符串参数“id”传递给控制器​​,因此它会检测到“id”,这是您需要在控制器中进行一些更改而不是直接获取“id”的字符串作为参数,您需要在将所选 id 放入输入字段时接受请求:

  <input id="id" name="id" hidden value="">

添加jquery:

<script>
     $(document).on('click','.delete',function(){
             let id = $(this).attr('data-id');
             $('#id').val(id);
        });
</script>

控制器应该是:

public function destroy(Request $request)
    {
        $id= $request->id;

        $items = yourModel::find($id);

        $items->delete();

        return redirect()->route('your page')->with('message', 'report details has been successfully deleted');

    }

【讨论】:

    【解决方案3】:
    <div class="table-responsive">
      <table class="table table-bordered table-sm">
        <thead>
            <tr>
                <th>ID</th>
                <th>Nombre</th>
                <th>Usuario</th>
                <th>Acción</th>
            </tr>
        </thead>
        @foreach($users as $user)
        <tbody>
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->nombre }}</td>
                <td>{{ $user->login}}</td>
                <td>
                  <a href="{{ route('users.edit', $user->id) }}" type="button" class="btn btn-primary"><i class="bi bi-pencil-square"></i></a>
                  <a data-bs-toggle="modal" class="btn btn-warning" data-bs-target="#deleteUserModal_{{$user->id}}"
                  data-action="{{ route('users.destroy', $user->id) }}"><i class="bi bi-trash"></i></a>
    
                </td>
            </tr>
        </tbody>
        <!-- Delete User Modal -->
        <div class="modal fade" id="deleteUserModal_{{$user->id}}" data-backdrop="static" tabindex="-1" role="dialog"
        aria-labelledby="deleteUserModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="deleteUserModalLabel">Esta acción es irreversible.</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
              <form action="{{ route('users.destroy', $user->id) }}">
                <div class="modal-body">
                  @csrf
                  @method('DELETE')
                  <h5 class="text-center">¿Estás seguro de que quieres eliminar al usuario
                    {{ $user->nombre }} {{ $user->apellido_materno }} {{ $user->apellido_paterno }} ?</h5>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                  <button type="submit" class="btn btn-danger">Si, Eliminar Usuario</button>
                </div>
            </form>
            </div>
          </div>
        </div>
        @endforeach
      </table>
    </div>
    

    【讨论】:

    • 您好 Jose,感谢您的第一个回答。考虑稍微解释一下代码 sn-p,以便答案更容易理解。
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-01-19
    • 2014-05-03
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多