在laravel中删除带有模态的项目 [英] delete project with modal in laravel

查看:131
本文介绍了在laravel中删除带有模态的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目旁边有一个删除按钮,每当单击按钮时,该按钮就会显示一个弹出模式以供确认.我希望模式中的是"按钮在单击该按钮时删除该项目.

I have a delete button beside my projects that shows a popup modal for confirmation whenever the button is clicked on. I want the yes button in the modal to then delete the project whenever this button is clicked on.

我正在使用@include将模式调用到项目/显示视图中.使用以下jquery单击按钮时,模态确实出现,但是按下按钮时的yes按钮不会删除项目.

I'm calling the modal into the projects/show view with @include. The modal does appear when the button is clicked using the following jquery but the yes button when pressed is not deleting the project.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script>
$(document).ready(function() {

   $(".delete").on("submit", function(e){

       e.preventDefault();

       $('#myModal').modal('show'); 


    });

});

项目旁边的删除按钮:

  {!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}

           {!!Form::button('<span class="glyphicon glyphicon-remove"></span>', ['class' => 'btn btn-danger btn-md', 'type' => 'submit'])!!}

            {!! Form::close() !!}

模式代码:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-md" 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 text-center" id="myModalLabel">Delete Confirmation</h4>
      </div>
      <div class="modal-body text-center">
        Are you sure you want to delete this project?
      </div>
      <div class="modal-footer">
        <button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>

        <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>

ProjectController:

ProjectController:

<?php

namespace App\Http\Controllers;

use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;

class ProjectsController extends Controller
{

 public function destroy($id){

        $project = Project::find($id);

        $project->delete();

        Session::flash('success', 'The project was successfully deleted!');

        return redirect()->route('projects.show', $project->project_id);

    }

}

推荐答案

将模式中的提交"按钮更改为此:

Change the submit button in your modal to this:

{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}

<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>

{!! Form::close() !!}

最好的解决方案是删除对现有删除按钮的实现,并将其设置为普通按钮.让该按钮弹出模式,并像我上面那样用实际的删除按钮替换是"按钮.

The best solution is to remove your implementation of the delete button you have now and make it a normal button. Let that button pop up the modal and replace your "Yes" button with an actual remove button like I did above.

这篇关于在laravel中删除带有模态的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆