I have a table which is printing data in this form,
var table;
// DATA TABLE!!!
function show_assigned_table()
{
$.ajax({
url: "action.php",
type: "POST",
data: {action:"view_assigned_course_table"},
dataType: 'json',
success: function(data)
{
console.log(data);
table = $('#assign_table').DataTable({
"bDestroy": true,
data: data,
columns: [
{'data': 'course_name'},
{'data': 'teacher_name'},
{'data': 'grade_name'},
{'render': function(data, type, row, meta){
return '<a id="replace_teacher' + meta.row + '" data-value = ' + row.taught_id +
' class="btn btn-outline-warning btn-sm" href="">Replace</a> <a id="remove_teacher' +
meta.row + '" data-value = ' +
row.taught_id + ' class="btn btn-outline-danger btn-sm" href="">Delete</a>';
}},
]
});
},
error: function(e)
{
alert("no");
}
});
}
Now, I have another function for the "Replace" and "Delete" buttons. A function for delete button is as follows,
$(document).on("click", "[id^=remove_teacher]", function(e){
var id = $(this).data('value');
e.preventDefault();
$.ajax({
url: "action.php",
type: "POST",
data:{action: "Remove_Assigned_Teacher", id: id},
success:function(response)
{
response = response.trim();
console.log(response);
if (response == 'ok')
{
alert("Teacher has been removed from the course successfully!");
$("#assign_table").DataTable().ajax.reload();
}
else
alert("Error occured");
}
});
});
This function works well, except one thing, it doesn't Refresh the table whenever a row is deleted... To refresh the table, I have to refresh the whole page. What I want is only this table to be refreshed whenever the delete event occurs.
I used ajax.reload() function for this but it didn't work, as it apparently didn't receive JSON data on refresh (now I tried to search it but I couldn't find any thing which I could properly understand. I did find a code to convert array to JSON but didn't get how to implement it in my code. I am still very new in this).
I also tried using destroy function and then created the table again, that worked but it destroyed all the properties of my table (like the border of table, search bar and etc. The look of the table changed)