I have a column in my table that is a long string. So i'm trying to show it in a popup modal.
When i click the button to launch the modal, the page just refreshes and nothing is even printed to console.
HTML of Modal:
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 id="modalTitle"></h3>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
</div>
</div>
Here is snippet in DataTable creation.
{ data: 'Journal',
render: function(data, type, row) {return '<button class="btn btn-primary" data-toggle="modal" data-Journal="'+data+'" data-target="#myModal">'+"Details"+'</button>'} },
The modal show event. Nothing is printed in console so i'm guessing the error is before here.
$("#myModal").on('show.bs.modal', function (e) {
var triggerLink = $(e.relatedTarget);
var journal = triggerLink.data("Journal");
console.log(e.relatedTarget);
console.log(journal);
$("modalTitle").text("Title");
$(this).find(".modal-body").html("<h5>"+journal+"</h5>");});
Try changing your button to type = button as its default is submit'
render: function(data, type, row) {return '<button class="btn btn-primary" data-toggle="modal" data-Journal="'+data+'" type="button" data-target="#myModal">'+"Details"+'</button>'} },
Since you didn't provide any relevant details regarding your use case, I've made up my own example, which delivers major idea, though:
//make up random source for DataTable
const tableSrc = [...Array(5)].map((item, index) => {
const rowObj = {};
rowObj.user = 'user'+(index+1);
rowObj.ip = [...Array(4)].map(() => Math.floor(Math.random()*140)+10).join('.');
rowObj.hash = [...Array(256)].map(() => String.fromCharCode(Math.floor(Math.random()*(126-33))+33)).join('');
return rowObj;
});
//initialize DataTable
const dataTable = $('#mytable').DataTable({
sDom: 't',
data: tableSrc,
columns: [
{title: 'Username', data: 'user'},
{title: 'IP address', data: 'ip'},
{
title: 'Hash',
data: 'hash',
render: data => `<span>******</span><button class="showhash">show</button>`
}
]
});
//display 'hash' property for particular row upon clicking the button
$('#mytable').on('click', 'button', function(){
const clickedRow = dataTable.row($(this).closest('tr'));
const modalTitle = `Hash for ${clickedRow.data().user}`;
const modalBody = clickedRow.data().hash;
$('#mymodal .modal-title').text(modalTitle);
$('#mymodal .modal-body').text(modalBody);
$('#mymodal').modal('toggle');
});
td button {
float:right
}
<!doctype html>
<html>
<head>
<!--jQuery DataTables prerequisites-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<!--bootstrap prerequisites-->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<!--DataTable-->
<table id="mytable"></table>
<!--Bootstrap modal-->
<div id="mymodal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>