php - 将数据从 Laravel 传递到 Bootstrap 模式

标签 php twitter-bootstrap laravel

想知道是否有人可以帮助我解决这个问题。我是 JS 和 JQuery 的新手。

我想避免在我的博客中删除帖子时发生意外,所以我想要它,这样当我点击删除时它会弹出一个模式来确认我确实想删除帖子然后执行或不执行。

我添加了以下代码。

 <!-- Button trigger modal to delete post -->
<button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#deletePost">
  Delete
</button>

<!-- Modal -->
<div class="modal fade" id="deletePost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <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="myModalLabel">Delete Blog Post</h4>
      </div>
      <div class="modal-body">
        <p>You are about to delete a blog post, are you sure all that effort is to be sent to the unrecoverable trash can?</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">OMG, NO!!!</button>
        <form action="/blog/{{ $post->id }}" method="POST">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
            <button type="submit" class="pull-right btn btn-danger">Delete it!</button>
        </form>
      </div>
    </div>
  </div>
</div>

现在一切正常,我现在想做的就是当我点击“删除它!”按钮它会做到这一点。

除了删除正确的帖子外,这一切都有效,我想通过

{{ $post->id }}

到模态,以便它将帖子 ID 发送到 Controller 。

我的问题是如何将上面的 id 传递给模态框?我已经阅读了很多示例,它们是如此不同,所以我认为最好获得一些意见。

谢谢!

最佳答案

我非常推荐使用 SweetAlert插件,它在视觉上与 Twitter Bootstrap 兼容并且使用起来更加愉快。 这是一段多语言模型无关删除功能的代码:

    $('body').on('click', 'sweet-delete', function (e) {
        e.preventDefault();
        var id = $(this).data('id');
        var model = $(this).data('model');
        swal({
            title: lang.confirm_please,
            text: lang.delete_warning,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: lang.delete_button,
            closeOnConfirm: false,
            showLoaderOnConfirm: true,

        }, function(){
            $.post('/'+model+'/'+id, {_method: 'DELETE'}, function(data){
                swal({
                    title: lang.delete_title,
                    text: lang.delete_success,
                    type: "success",
                    timer: 5000
                }, function(){
                    window.location.href = '/'+model;
                });
            });
        });
    })

在你看来:

<button data-id="{{$post->id}}" data-model="blog" class="pull-right btn btn-danger sweet-delete">@lang('app.delete')</button>

我必须提到,为了将 CSRF token 附加到我使用的 ajax 表单:

$.ajaxSetup({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
    });

在我的 app.js 中,和

<meta name="csrf-token" content="{{csrf_token()}}">

在我的主布局文件中。

关于php - 将数据从 Laravel 传递到 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40473749/

相关文章:

twitter-bootstrap - 如何以正确的方式在 angular.js 中实现 scrollspy?

html - 使用 Bootstrap 面板删除 bootstrap 表格边框

laravel - 在 OctoberCMS 中创建产品时向用户发送通知?

php - 无法让浏览器截图正常工作

mysql - 似乎无法将 MySQL 查询转换为 Laravel 查询生成器

PHP Mysql 选择

php - 如何仅允许 api 访问 android 或 ios 应用程序(laravel)?

php登录表单密码无效消息问题

javascript - 使用 AJAX 将图像从 Canvas 上传到 PHP 服务器

javascript - 尝试跳过索引时 Bootstrap 轮播崩溃