In this post I am showing how JavaScript can used to add Image as table cells for DataTables rows.
CSS and JS Library to include
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js"></script>
HTML Code
<div style="width:90%;margin-top:100px;" class="container">
<table style="width:100%;" id="userTable_MainId" class="table table-striped table-bordered">
<thead>
<tr>
<th>No.</th>
<th>Image</th>
<th>Image URL</th>
</tr>
</thead>
<tbody id="userTable"> </tbody>
</table>
</div>
JavaScript
function enlargeImage(url){
alert(url);
}
function fillTable(){
var table = $('#userTable_MainId').DataTable();
table.clear().draw();
var imgURLs = ['https://homepages.cae.wisc.edu/~ece533/images/fruits.png','https://homepages.cae.wisc.edu/~ece533/images/peppers.png','https://homepages.cae.wisc.edu/~ece533/images/airplane.png'];
for(var i=0;i < imgURLs.length;i++){
var imgEle = " <img src='"+imgURLs[i]+"' style='height:30px; width:30px' onclick=enlargeImage('"+imgURLs[i]+"')>";
var d = [i,imgEle,imgURLs[i]];
table.row.add(d).draw( false );
}
}
$(document).ready(function() {
$('#userTable_MainId').DataTable();
fillTable();
});
No comments:
Post a Comment