简体   繁体   中英

Passing data from Blade to modal in Laravel

I have a situation like display table with different columns, say the first column is id and the second column is name, the third column has a button which on click opens up a modal; the data in the table is coming from foreach loop.

I want to pass the id to the modal when button clicked.

                                    <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> 

The Answer To The Question (The One That Worked For Me)

Step#1 Add Button in the column Of the table:

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

Step#2 Add Script for The function that is calling from the button:

                        <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>

Step#3: Add Modal to which you want to pass the value to:

                      <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>

give an identifiable class say employee <tr class="employee"> and put the id of the entry in the same tag like this <tr class="employee" data-id="{{$emp->id}}"> . Then when you would click on the row you could do something like this:

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 
 })
})

For this, you can use jquery

step(1) add this code in your blade file

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

step (2) define your jquery method

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

</script>

step (3) Make Modal

<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>

I suggest you to refactor this

<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>

to

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

And add this 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');

};

Of course for disapprove it will be the same.

This is working for me you can try this:

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

then:

Modal for Update


<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>

then


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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