Reputation: 852
I'm trying to launch a remote bootstrap modal and populate a datatable inside the modal. The first time I launch the modal from link1
it works and sets the variables however if I click link2
the modal opens but uses the data from link1
and vise versa.
On the parent page I'm creating the link via php like this:
PHP:
echo '<a data-toggle="modal" data-target="#viewComments" href="./remotemodal.php?object='.urlencode($row['OBJECT_NAME']).'&date='.urlencode($row['DATE_OCCURED']).'&error='.urlencode($row['ERROR']).'" class="btn-sm btn-info" ><i class="fa fa-comment-o"></i> View Comments</a>';
It populates the links correctly, similar to below:
Link1: remotemodal.php?object=Cluster1&date=03%2F01%2F2016+23%3A48&error=Error1
Link2: remotemodal.php?object=Cluster2&date=03%2F02%2F2016+17%3A44&error=Error2
Here is the PHP of the remotemodal.php:
PHP:
<?php
$object = NULL;
$date = NULL;
$error = NULL;
echo '<script type="text/javascript">alert("' . $object . '")</script>';
echo '<script type="text/javascript">alert("' . $date . '")</script>';
echo '<script type="text/javascript">alert("' . $error . '")</script>';
if ( !empty($_GET['object'])) {
$object = $_GET['object'];
$date = $_GET['date'];
$error = $_GET['error'];
}
echo '<script type="text/javascript">alert("' . $object . '")</script>';
echo '<script type="text/javascript">alert("' . $date . '")</script>';
echo '<script type="text/javascript">alert("' . $error . '")</script>';
?>
If I goto the links directly they work and I see the correct variables in the js alert. I've tried to clear the modal data from the parent page on modal close but nothing is working
Javascript:
<script>
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
</script>
Upvotes: 0
Views: 118
Reputation: 852
I ended up doing it differently but similar to the Dynamically load information to Twitter Bootstrap modal
On the parent page I populate the links with the variables I want to pass
<a object="'.urlencode($row['OBJECT_NAME']).'" date="'.urlencode($row['DATE_OCCURED']).'" error="'.urlencode($row['ERROR']).'" data-toggle="modal" data-target="" href="#" class="btn-sm btn-info" ><i class="fa fa-comment-o"></i> View Comments</a>';
The modal has a hidden div
that the javascript shows on click
<div id="modalContent" style="display:none;">
...table here...
</div>
Then when the button is clicked I grab the variables from the link, create the datatable and pass the variables to ajax. Then when the modal is closed I destroy the table so the user can click another link.
Javascript:
$(".btn-info[data-toggle=modal]").click(function() {
var object = $(this).attr('object');
var date = $(this).attr('date');
var error = $(this).attr('error');
$('#viewComments').modal();
$('#modalContent').show();
table = $('#activity-report').DataTable({
"order": [
[0, "desc"]
],
"displayLength": 10,
"bprocessing": true,
"bserverSide": true,
"sAjaxSource": "remotemodal.php?object=" + object + "&date=" + date + "&error=" + error,
"sServerMethod": "POST"
});
});
$('#viewComments').on('hidden.bs.modal', function() {
table.destroy();
});
Upvotes: 0
Reputation: 2088
Looking at the documentation, it appears that removeData
only works on data set using the data attribute.
So either you'll have to change the way you're setting the modal data or individually set the data through jQuery.
Actually, after some googling, it appears that this is a duplicate of this question.
Upvotes: 2
Reputation: 5098
are you populating the modal body/content via ajax?
<a data-toggle="modal" data-target="#viewComments" href="http://www.thisurl.com/never/gets?called" >Open Modal</a>
bootstrap just toggles the visibility of data-target... the link is never followed
Upvotes: 1