Please help me, i want to bind table on datatables when user click a button, but the coding is not working. My code is looks like below :
<script src="jquery.js"></script>
<link href="css/jquery.dataTables.min.css" rel="stylesheet" />
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#example').dataTable({
"ajax": 'data.json',
"paging": false,
"searching": false,
"order": [[ 0, "asc" ]]
});
var t = $('#example').DataTable();
$('#addRow').on( 'click', function () {
t.row.add([
"1",
"2",
"3",
]).draw();
});
$("#button").click(function(e){
$(".wrapper").html('<div><div id="addRow">add</div><table id="example" class="row-border hover" cellspacing="0" width="100%"><thead><tr><th align="left">title 1</th><th align="left">title 2</th><th align="left">title 3</th></tr></thead></table></div>');
});
});
</script>
<body>
<div class="wrapper"></div>
<div class="button"><input name="tbSubmit" type="button" value="click this button" id="button"></div>
My data.json is looks like below :
{
"data": [
[
"1.1",
"1.2",
"1.3"
],
[
"2.1",
"2.2",
"2.3"
],
[
"3.1",
"3.2",
"3.3"
]
]
}
The code will run if this code is not in html click but in class wrapper like below :
$("#button").click(function(e){
$(".wrapper").html('');
});
<div class="wrapper"><div><div id="addRow">add</div><table id="example" class="row-border hover" cellspacing="0" width="100%"><thead><tr><th align="left">title 1</th><th align="left">title 2</th><th align="left">title 3</th></tr></thead></table></div></div>
Thank you for any helps :)