Skip to content
Advertisement

Create multiple modals in a loop while passing data

I am trying to make a display while looping through items and displaying buttons. The buttons can open modals that display some data. I have tried to pass the data to the modal but it is just displaying the first iteration of the loops information and never get’s updated. What do I need to do to make a modal for each iteration of my list?

@foreach ($forms as $form)
    <div class="row">
        <div class="col-lg-2">
            <div style="float:left; padding-right: 10px;">
                <a href="/form/{{$cuid}}/{{$form['cuBaseName']}}"
                   data-toggle="tooltip" title="Edit Fields"
                ><i class="fas fa-edit fa-2x"></i></a>
            </div>
            <div style="float:left; padding-right: 10px;">
                <span data-toggle="modal" data-target="#copyFormModal">
                    <a href="#" data-toggle="tooltip" title="Copy"
                    ><i class="fas fa-copy fa-2x"></i></a>
                </span>
            </div>
            <div style="float:left; padding-right: 10px;">
                <span data-toggle="modal" data-target="#deleteFormModal">
                <a href="#" data-toggle="tooltip" title="Delete">
                    <i class="fas fa-trash-alt fa-2x"></i></a>
                </span>
            </div>
        </div>
    </div>
    <hr>


    @include('layouts.partials.modals.copyFormModal',['cuid'=>$cuid, 'cuBaseName'=> $form['cuBaseName']])
    @include('layouts.partials.modals.deleteFormModal',['cuid'=>$cuid, 'cuBaseName'=> $form['cuBaseName']])

@endforeach



<!-- Modal -->
<div class="modal fade" id="deleteFormModal" tabindex="-1" role="dialog" aria-labelledby="deleteFormModal" aria-hidden="true" >
    <div class="modal-dialog" role="document" style="max-width:900px;">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="exampleModalLabel">Delete Form</h3>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete form <b>{{$cuBaseName}}</b> from <b>{{$cuid}}</b>?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-success" data-dismiss="modal"
                        onclick='location.href="/forms/{{$cuid}}/{{$cuBaseName}}/delete"' >Delete</button>
            </div>
        </div>
    </div>
</div>

Advertisement

Answer

Try this, add a unique #id to each modal window:

@foreach ($forms as $form)
    <div class="row">
        <div class="col-lg-2">
            <div style="float:left; padding-right: 10px;">
                <a href="/form/{{$cuid}}/{{$form['cuBaseName']}}"
                   data-toggle="tooltip" title="Edit Fields"
                ><i class="fas fa-edit fa-2x"></i></a>
            </div>
            <div style="float:left; padding-right: 10px;">
                <span data-toggle="modal" data-target="#copyFormModal-{{$cuid}}">
                    <a href="#" data-toggle="tooltip" title="Copy"
                    ><i class="fas fa-copy fa-2x"></i></a>
                </span>
            </div>
            <div style="float:left; padding-right: 10px;">
                <span data-toggle="modal" data-target="#deleteFormModal-{{$cuid}}">
                <a href="#" data-toggle="tooltip" title="Delete">
                    <i class="fas fa-trash-alt fa-2x"></i></a>
                </span>
            </div>
        </div>
    </div>
    <hr>


    @include('layouts.partials.modals.copyFormModal',['cuid'=>$cuid, 'cuBaseName'=> $form['cuBaseName']])
    @include('layouts.partials.modals.deleteFormModal',['cuid'=>$cuid, 'cuBaseName'=> $form['cuBaseName']])

@endforeach



<!-- Modal example -->
<div class="modal fade" id="deleteFormModal-{{$cuid}}" tabindex="-1" role="dialog" aria-labelledby="deleteFormModal" aria-hidden="true" >
    <div class="modal-dialog" role="document" style="max-width:900px;">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="exampleModalLabel">Delete Form</h3>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete form <b>{{$cuBaseName}}</b> from <b>{{$cuid}}</b>?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-success" data-dismiss="modal"
                        onclick='location.href="/forms/{{$cuid}}/{{$cuBaseName}}/delete"' >Delete</button>
            </div>
        </div>
    </div>
</div>

Hope it helps.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement