繁体   English   中英

在 Laravel 中将数据从 Blade 传递到模态

[英]Passing data from Blade to modal in Laravel

我有一个情况,比如显示不同列的表,比如第一列是 id,第二列是 name,第三列有一个按钮,点击它会打开一个模式; 表中的数据来自 foreach 循环。

我想在单击按钮时将 id 传递给模态。

                                    <td>{{ $emp->req_id}} </td>

                                    <td>{{ $emp->empid}} </td>
                                    <td>{{ $emp->visit_title}} </td>
                                    <td>{{ $emp->stays_nights}}</td>
                                    <td>{{ $emp->apply_date }}</td>
                                    <td>{{ $emp->travel_charges }}</td>
                                    <td>{{ $emp->hotel_charges }}</td>
                                    <td>{{ $emp->meal_charges }}</td>

                                    <td>{{ $emp->approv_status}}</td>

                                @endforeach
                                </tr> 
                                </tbody> 

问题的答案(对我有用的那个)

步骤#1在表的列中添加按钮:

<button type="button" onclick="myfunction( {{$a->id }})" > My Button </button>

步骤#2为从按钮调用的函数添加脚本:

                        <script>

                          function myfunction(e){

                            var x = e;

                             $('#edit_req_id').val(req_id);  //The id where to pass the value

                              $('#modal-block-popout').modal('show'); //The id of the modal to show
                            };

                         </script>

步骤#3:添加您要将值传递到的模态:

                      <div class="modal fade" id="modal-block-popout">
                       <div class="modal-content ">
                        <div class="block-options">
                  <button type="button" class="btn-block-option" data-dismiss="modal" aria-label="Close" name="btn"> <i class="fa fa-fw fa-times"></i></button>
                       </div>
                        <div class="block-content">
                   <input class="form-control form-control-alt " type="text" id="edit_req_id" name="empid">

                    </div

                    </div>

给一个可识别的类,比如雇员<tr class="employee"> ,并将该条目的ID放在相同的标签中,例如<tr class="employee" data-id="{{$emp->id}}"> 然后,当您单击该行时,您可以执行以下操作:

const employees = document.querySelectorAll('.employee');
employees.forEach( employee => {
 employee.addEventListener(e => {
  const employeeId = e.target.getAttribute('data-id');
  // do what you need to do with the id 
 })
})

为此,您可以使用jquery

步骤(1)将此代码添加到刀片文件中

 <button type="button" data-toggle="modal" data-target-id="{{ $emp->id }}" data-target="#modelName">Button name </button>

步骤(2)定义您的jquery方法

 <script>
            $(document).ready(function () {
                $("#modelName").on("show.bs.modal", function (e) {
                    var id = $(e.relatedTarget).data('target-id');
                    $('#pass_id').val(id);
                });
            });

</script>

步骤(3)制作模态

<div class="modal fade" id="modelName" tabindex="-1" role="dialog"
                             aria-labelledby="myModalLabel">
                            <div class="modal-dialog data-dialog-centerd" 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">×</span></button>
                                        <h4 class="modal-title text-center" id="myModalLabel">Model header Name</h4>
                                    </div>
                                    <div class="modal-body">
                                        <form class="form-horizontal" action="#"
                                              method="post"
                                              enctype="multipart/form-data">

                                            {{ csrf_field() }}

                                            <div class="portlet-body form">
                                                <div class="form-body">
                                                    <div class="form-group">
                                                        <input class="form-control" name="name" type="text"
                                                               id="pass_id">
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

我建议你重构一下

<a title="Approve" data-toggle="modal" data-target="#modal-block-popout" class="btn btn-outline-success btn-sm" href="approve<?php $emp->id; ?>">Approve </a>

<button type="button" title="Approve" class="btn btn-outline-success btn-sm btn-toggle-modal-block-popout" data-id="<?php $emp->id; ?>" >Approve</button>

并添加此Javascript

$(document).on('click', '.btn-toggle-modal-block-popout', function() {

var id = $(this).attr('data-id');

//DO WHAT EVER YOU NEED

$('#modal-block-popout').modal('show');

};

当然,如果不赞成,那将是相同的。

这对我有用,你可以试试这个:

          <i class="mdi mdi-eyedropper px-2" type="button" data-bs-toggle="modal"
                                        data-target-id="{{ $item->id }}" data-bs-target="#modelName"></i>

然后:

更新模态


<div class="modal fade" id="modelName" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog data-dialog-centerd" 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">×</span></button>
                <h4 class="modal-title text-center" id="myModalLabel">Model header Name</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="#" method="post" enctype="multipart/form-data">

                    {{ csrf_field() }}

                    <div class="portlet-body form">
                        <div class="form-body">
                            <div class="form-group">
                                <input class="form-control" name="name" type="text" id="pass_id">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

然后


<script>
    $(document).ready(function() {
        $("#modelName").on("show.bs.modal", function(e) {
            var id = $(e.relatedTarget).data('target-id');
            $('#pass_id').val(id);
        });
    });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM