I have added DataTables to a table i am fetching data via AJAX, using PHP and SQL. On the very first load, after performing the first Search all is fine and everything works as intended. However when searching a second time, while all the code used to Search has been ran through again, the DataTable functionality seems to stop working.
I added some alerts into crucial parts in the DataTable file, and found after the first search, it never makes it back into the file.
I though maybe i need to "re-initialise" DataTable seeing as the entirety of the data contents has changed after my search again.
Spent a long time looking online, with a lot of suggestions being around Destroy and Clear etc. But nothing seems to work.
Here is my code, as you can see i have it set to do something different after the first search but not sure how to fix this, as the sorting, pagination, searching etc all dont work:
$.ajax({
type:'POST',
url:'/ITSMIS/data/asset/search.php',
data:HardwareAsset,
dataType: "html",
success:function(data){
LoadDataTableHeaders();
var NotSearched = document.getElementById("no-search-default");
var TableContainer = document.getElementById("data-table-container");
var NoSearchResults = document.getElementById("no-search-results");
if(NotSearched.style.display !== 'none'){
$(NotSearched).hide();
}
if(data){
if(TableContainer.style.display == 'none'){
$(TableContainer).show();
}
$(NoSearchResults).hide();
$('#data-table-results').html(data);
$("input:checkbox:not(:checked)").each(function() {
var DataTableColumn = "table ." + $(this).attr("name");
$(DataTableColumn).remove();
});
if ( ! $.fn.DataTable.isDataTable( '#data-table' ) ) {
$('#data-table').DataTable();
}
else{
}
}
else{
if(NoSearchResults.style.display == 'none'){
$(TableContainer).hide();
$(NoSearchResults).show();
}
}
}
})