I'm having two problems with DataTables and Bootstrap modals.
- Can't use dynamically built buttons after page 1
(Solved by Yuri)
I have this table, It is built dynamically by an AJAX request, the data loads correctly and builds up the table as it should, the buttons 'Manage' and 'Delete' have classes set on them, when clicked it should open another modal on top of the current one, on the 1st page of the table it works fine, if I go to page 2 it doesn't action anything as if there is no listener on it.
- Datatables not initialising on the 2nd modal (Could be related to issue 1)
I have the below jQuery to initialise another DataTables on the modal that the 'Manage' button should open up. When it does open it should initialise another DataTables and get data from the AJAX request but in my network tab in Chrome developer tools it isn't being ran.
I have discovered the problem is, the DataTables isn't being initialized for some reason? the jQuery works up to there I can't understand what I'm doing wrong here?
$(document).ready(function(){
$('.valid-tags').on('click', '.manageAutofixes', function(){
$('.autofixes-modal').modal('show');
$('.autofixes-modal').on('shown.bs.modal', function () {
var table = $('.autofixes-table').DataTable({
"ajax": {
"url": "/ajax/getValidTags.php",
"type": "POST",
"data": {
ruleID: ruleID,
type: 'autofixes'
},
},
"columnDefs": [{
"targets": 2,
"render": function(data, type, full, meta){
return '<button class="btn btn-default btn-sm manageAutofixes" type="button">Manage</button> <button class="btn btn-danger btn-sm deleteValid">Delete</button>';
}
}],
destroy: true
});
})
})
});
My modals HTML (included on the page)
<div class="modal fade autofixes-modal" tabindex="-1" role="dialog" aria-labelledby="LargeModalLabel" aria-hidden="true" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog modal-wide">
<div class="modal-content">
<div class="modal-header modal-header-custom">
<input class="ruleID" type="hidden"></input>
<button type="button" class="close destroyTable" data-dismiss="modal" aria-hidden="true">x</button>
<h4 class="modal-title">Autofixes</h4>
</div>
<div class="modal-body">
<div class="topBar">
<div>
<input type="text" class="autofix inputTextStyling">
</div>
</div>
<hr>
<table class="table table-striped table-condensed autofixes-table" style="width:100%;">
<thead>
<tr>
<th>Autofix</th>
<th>Manage</th>
</tr>
</thead>
<tbody class="autoFixesTable">
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default destroyTable" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
