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

时间:2018-09-13 06:48:31

标签: javascript laravel modal-dialog

我遇到这样的情况,例如显示表的列不同,例如第一列是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> 

4 个答案:

答案 0 :(得分:0)

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

表列中的第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>

答案 1 :(得分:0)

给出一个可识别的类,例如雇员<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 
 })
})

答案 2 :(得分:0)

为此,您可以使用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>

答案 3 :(得分:0)

我建议您重构此

<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');

};

当然不同意。