【问题标题】:Laravel perform Update/edit inside index view is it possible?Laravel 在索引视图中执行更新/编辑是否可能?
【发布时间】:2018-07-16 08:20:40
【问题描述】:

是否可以使用模态表单同时使用索引视图执行更新/编辑?我有 index view 和显示数据的表格,并且在表格内它有名为 [edit and delete] 的按钮现在我想在编辑按钮打开后执行编辑/更新点击然后模态表单会出来..?但是每当使用模态表单时,它都会显示如下错误。

这是我的控制器:

public function show_setup()
{
    $batch=Batch::all();

    return view('setup.show_batch',compact('batch'));
}

public function edit_batch(request $request)
{
        $batch = Batch::find ($request->id);
        $batch->batch_name = $request->batch_name;
        $batch->save();

        return redirect()->back();

}

我的看法

<form>
    <div class="form-group">
        <table class="table table-hover table-bordered" id="table">
            <tr>
                <th>Batch</th>
                <th>Action</th>
            </tr>

            @foreach($batch as $bt)
            <tr>
                <td>{{$bt->batch_name}}</td>
                <td>
                    <a href="#" data-toggle="modal" data-target="#edit_batch" class="btn btn-info btn-sm">Edit </a>
                    <a href="#" class="btn btn-danger btn-sm">Delete</a>
                </td>
            </tr>
            @endforeach
        </table>
    </div>

</form>   <!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
    <div role="document" class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
                <button type="button" data-dismiss="modal" aria-label="Close" class="close">
                <span aria-hidden="true">×</span></button>
            </div>
            <div class="modal-body">                        
                <form action="setup/batch/edit" method="POST">
                    {{csrf_field()}}
                    {{ method_field('PUT') }}

                    <div class="form-group">
                        <label>School Year</label>
                        <input type="text" placeholder="School Year" name="batch_name" value="{{$batch->batch_name}}" class="form-control" autocomplete="off">
                    </div>
                </div>
                    <div class="modal-footer">
                      <button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
                      <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
                  </div>
                </div>
            </div>
       <!--End batch Modal-->

【问题讨论】:

  • 是显示模态时还是提交表单后?
  • 不,当我转到 url 时,它会自动出现错误,但是当我删除模态值内的 {{batch_name}} 时,它会显示页面,但模态值是空的..跨度>
  • 可以添加你的js代码吗?
  • 我没有使用 js 代码,你看到的是我的代码

标签: php laravel bootstrap-modal


【解决方案1】:

问题是您想从集合$batch 中获取batch_name,您必须使用javascript 传递您想要的数据,您需要的数据是idbatch_name

这是一个例子:

<form>
    <div class="form-group">
        <table class="table table-hover table-bordered" id="table">
            <tr>
                <th>Batch</th>
                <th>Action</th>
            </tr>

            @foreach($batch as $bt)
            <tr>
                <td>{{$bt->batch_name}}</td>
                <td>
                    <a href="#" id="editBtn" data-toggle="modal" data-target="#edit_batch" class="btn btn-info btn-sm" data-id="{{ $bt->id }}" data-batch_name="{{ $bt->batch_name }}">Edit </a>
                    <a href="#" class="btn btn-danger btn-sm">Delete</a>
                </td>
            </tr>
            @endforeach
        </table>
    </div>

</form>

<!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
    <div role="document" class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
                <button type="button" data-dismiss="modal" aria-label="Close" class="close">
                <span aria-hidden="true">×</span></button>
            </div>
            <div class="modal-body">                        
                <form action="setup/batch/edit" method="POST">
                    {{csrf_field()}}
                    {{ method_field('PUT') }}
                    <input id="batch_id" type="hidden" name="id">
                    <div class="form-group">
                        <label>School Year</label>
                        <input id="batch_name" type="text" placeholder="School Year" name="batch_name" class="form-control" autocomplete="off">
                    </div>
                    <div class="modal-footer">
                      <button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
                      <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--End batch Modal-->

<script type="text/javascript">
    $('.editBtn').on('click',function(e) 
    {
        var link    = $(this);
        batch_id = link.data("id");
        batch_name = link.data("batch_name");
        $("#batch_id").val(batch_id);
        $("#batch_name").val(batch_name);
    });   
</script>

【讨论】:

  • 检查我更新的答案,对于 js 部分,您可以添加您所做的任何事情来显示模态:)
猜你喜欢
  • 2010-11-08
  • 2022-11-20
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2021-04-04
相关资源
最近更新 更多