1

In one of my views, I have the following html to populate a table with data from the database (passed from the view controller), and include a button on each row which opens up a pop-up modal with more information about the item like so.

My index.blade.php:

@extends('layouts.app')

@section('style')
<style>
.panel-pagination {
    margin: 0 0 !important;
}

.panel-container {
    padding-right: 0 !important;
    padding-left: 0 !important;
    height:35px;
}

.abc {
    height:35px;
    display:table-cell !important;
    vertical-align:middle;
}

.link-btn,
.link-btn:visited,
.link-btn:link,
.link-btn:active {
    color: #3196D6;
    background: #fff;
    padding: 10px 20px;
    display: block;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-size: 13px;
}

.link-btn:hover {
    background: #E7E7E7;
}
</style>
@endsection

@section('content')
<div class="container">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <div class="panel-container">
                <div class="col-xs-12 col-sm-6 col-lg-8 text-left">
                    <h4 class="panel-title abc">
                        <i class="glyphicon glyphicon-hdd"></i>
                        Machine Configurator
                    </h4>
                </div>
                <-- some html stuff -->
            </div>
        </div>
        <div class="panel-body">
            <table class="table table-striped table-bordered" id="machineTable">
                <thead>

                    <-- some table header stuff -->

                </thead>
                <tbody>
                @foreach($machines as $key => $value)
                    <tr>
                        <td>{{ $value->machineName }}</td>
                        <td>{{ $value->departmentName }}</td>
                        <td>{{ $value->partName }} {{ $value->partRevision }}</td>
                        <td>{{ $value->productionStatus }}</td>
                        <td>
                            <a class="btn btn-small btn-success" href="{{ URL::to('machine/' . $value->machineId) }}"
                                data-toggle="modal" 
                                data-target="#showMachinePopupModal">
                                <!-- Info -->
                                 <i class="glyphicon glyphicon-info-sign"></i> 
                            </a>

                            <-- some more buttons -->

                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div id="showMachinePopupModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
@endsection

@section('script')
<script>
$(document).ready(function() {
   $("#showMachinePopupModal").on("hidden.bs.modal", function() {
       $("#showMachinePopupModal .modal-content").empty();
   });
});
</script>
@endsection

The problem is that I do not know how to clear the modal content when the modal is closed. Therefore, unless the user refreshes the page, after the user clicks to view more information for one item, the same data is always displayed in the modal. I have tried the following javascripts but they do not seem to work.

$('#showMachinePopupModal').on('hidden.bs.modal', function () {
    $('.modal-content').html("");
});

$('#showMachinePopupModal').on('hidden.bs.modal', '.modal', function () {
    $(this).removeData('bs.modal');
});

I am using Laravel's resource controller. Here is my "show.blade.php"

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">X</button>
    <h2 class="modal-title text-center">#{{ $machine->machineId }} - {{ $machine->machineName }}</h2>
</div>
<div class="modal-body">
    <p>
        <strong>Type:</strong> {{ $machine->machineType }}<br>
        <strong>Department:</strong> {{ $machine->departmentName }}<br>
        <strong>Production Status:</strong> {{ $machine->productionStatus }}<br>
        <strong>Quality Status:</strong> {{ $machine->qualityStatus }}<br>
        <strong>Part Loaded:</strong> {{ $machine->partName }} {{ $machine->partRevision }}<br>
        <strong>Last Updated:</strong> {{ $machine->updatedAt }}<br>
    </p>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>

Here is my app.blade.php

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'SEI-MQE') }}</title>

    <!-- Styles -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
        crossorigin="anonymous"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
        integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
        crossorigin="anonymous"> 
    <!-- <link href="{{ asset('css/app.css') }}" rel="stylesheet">   -->
    @yield('style')
</head>

<body>

    @include('include.navigationBar')

    @include('include.flashMessage')

    @yield('content')

    @yield('modal')

    <!-- Scripts -->
    <!-- <script src="{{ asset('js/app.js') }}"></script>  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" 
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
    @yield('script')
</body>

</html>

SOLUTION: Fixed by following Nikolas's solutions but using this script to clear:

$(document).ready(function() {
    $('#showMachinePopupModal').on('hidden.bs.modal', function () {
        $(this).removeData();
    });
});
Jonathan Tan
  • 129
  • 2
  • 3
  • 12

1 Answers1

1

Try this:

$(document).ready(function() {
   $("#showMachinePopupModal").on("hidden.bs.modal", function() {
       $("#showMachinePopupModal .modal-content").empty();
   });
});

UPDATE

Try adding this in your modal. Like so:

<div id="showMachinePopupModal" tabindex="-1" role="dialog" aria-hidden="true" class="modal fade">

Also try moving the modal div so it is outside any elements with special positioning. One good place might be just before the closing body tag </body>

UPDATE 2 Try creating another yield for modals in your app.blade.php and in your index.blade.php add this:

@section('modal-forms')
    PUT YOUR MODAL HERE
@stop

Also add bootstrap.js in your app.blade.php

diakosavvasn
  • 854
  • 1
  • 8
  • 22
  • I am using the CRUD design with Laravel's resource controller. So the view controller's show() function passes data to "show.blade.php". I will update my question with the code for show.blade.php right now. – Jonathan Tan Aug 11 '17 at 14:38
  • I added `` to my application and now the modal content does clear. However, my modal appears behind the backdrop and it does not open a new modal when I select another "info" button. I thought Laravel already comes with bootstrap in app.js, not sure why adding the script made the clear function work. – Jonathan Tan Aug 11 '17 at 14:40
  • app.blade.php is your Master layout and index.blade.php is your page you using the div ? – diakosavvasn Aug 11 '17 at 15:06
  • Okay, removed it. Added that in when testing stuff lol my bad. Forgot to remove. Also might be good to know, I never changed any default laravel style yet. The only css I did is included in my index.blade.php – Jonathan Tan Aug 11 '17 at 15:16
  • Yes, the modal is behind the backdrop and also the backdrop does not disappear upon modal close. Could you move this discussion to chat so as not to spam? Unfortunately, I do not have enough reputation to do this. – Jonathan Tan Aug 11 '17 at 15:22
  • Oh gg haha. Do I need to include the bootstrap js script in app.blade.php? I thought Laravel came preloaded with bootstrap js. Perhaps things are being loaded in the wrong order or something. It seems like me adding the bootstrap js script made the clear function work but causes the modal to act weird. – Jonathan Tan Aug 11 '17 at 15:26
  • So, it worked ?? But the modal act weird ? I didnt understand – diakosavvasn Aug 11 '17 at 15:38
  • Sorry, probably said too much and everything got lost. Here is the current situation. After adding bootstrap js script into my app.blade.php, the clear modal function you provided does work to clear my modal. However, now the modal pops up behind the background, like in this post: https://stackoverflow.com/questions/10636667/bootstrap-modal-appearing-under-background. Also, the background does not disappear when I close the modal. – Jonathan Tan Aug 11 '17 at 16:37
  • Didn't notice your update 2 until now. I have modified my app.blade.js accordingly (updated code in question). This fixed my previous situation where the modal pops up behind the background and the background did not disappear upon closing the modal... Before I tell you the new situation (lol), I just want to say thank you for all your help and I am sorry if I am frustrating at all, I am trying my best I swear! I am new to web development, my work wants me to make a web application but my experience is in C#/WPF applications... – Jonathan Tan Aug 11 '17 at 20:39
  • Anyways, the new situation is that when I close the modal and then try to open a new modal by clicking another info button again, only the background pops up. The modal-content stays cleared and is not updated. – Jonathan Tan Aug 11 '17 at 20:40
  • Try to show the modal content with ajax. It is much better. So, please if I helped you accept my answer as correct. – diakosavvasn Aug 11 '17 at 20:57
  • Unfortunately, I just joined stackoverflow like a week ago so I don't have enough reputation. But I will definitely upvote this when I can and I will look into showing the modal with ajax. – Jonathan Tan Aug 11 '17 at 21:10
  • Thank you my friend. Have a nice day – diakosavvasn Aug 11 '17 at 21:48
  • Just incase you're curious, I figured out all my problems and updated my question with the solution. Thank you for all the help :) Have a nice day. – Jonathan Tan Aug 11 '17 at 22:41