I've managed to figure out how to grab external json and use that in a modal using a manually set id in the javascript.
I'm stuck on passing the id from the button to the javascript url. My button markup is as follows;
<button type="button" class="btn btn-primary btn-sm modal-btn" data-id="10">Details</button>
My JS currently looks like this;
<script type="application/javascript">
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'T<"clear">lfrtip',
tableTools: {
"sRowSelect": "multi",
"aButtons": [ "select_all", "select_none" ],
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ 1 ] }
]
}
});
$('#example tbody').on( 'click', 'tr', function (e) {
if($(e.target).is('.modal-btn')){
$('#fullDetails').modal('show');
var event = "details.php?id=" + ($(this).data('id'));
$.getJSON(event, function( data ) {
$(".modal-title").text(data.title);
$(".modal-audience").text(data.audience);
$(".modal-leader").text(data.leader);
$(".modal-date").text(data.date);
$(".modal-start_time").text(data.start_time);
$(".modal-end_time").text(data.end_time);
$(".modal-details").text(data.details);
$(".modal-venue").text(data.venue);
$(".modal-cost").text(data.cost);
}, "json" );
}else{
$(this).toggleClass('selected');
}
});
$('#button').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
});
}); </script>
the line im having issue with is;
var event = "details.php?id=" + ($(this).data('id'));
I'm using ($(this).data('id')); to try and grab the id attribute from the button, but i keep getting undefined.
When the submit button is clicked, it invokes the jQuery function. The data entered the text area is extracted using the val() method into the text variable. This text string is passed to the modal body using the html() method of jQuery.
modal', function (event) { var button = $(event. relatedTarget); // Button that triggered the modal var recipient = button. data('whatever'); // Extract info from data-* attributes // If necessary, you could initiate an AJAX request here (and then do the updating in a callback). // Update the modal's content.
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
Try
var event = "details.php?id=" + ($(e.target).data('id'));
$(this) is pointing to the tr that was clicked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With