Reputation: 135
I'm having a bit of trouble trying to get this datatable to refresh after the data is inserted into the database. I would like the datatable to refresh on a javascript ajax, but I don’t want the entire page to refresh.
I'm new to datatables and can't seem to figure out what to do.
Any assistance would be greatly appreciated.
JavaScript
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
}
})
$.getJSON('selectProjectDefinitionDescription', function (data) {
$.each(data, function (i, item) {
ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description,
"<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input>"+
"<script>"+
" $('#"+data[i].id+"delete').on('click', () => {" +
" $.ajax({"+
" url: 'delete_project_definition',"+
" type: 'GET',"+
" data: { "+
" project_def_id: '"+data[i].id+"' "+
" },"+
" });"+
" });" +
"</script>"
]);
var table = $("#projDiscComTable").DataTable();
table.clear().draw();
table = $("#projDiscComTable").DataTable({
data: ProjDefTable,
columns: [
{title: "ID"},
{ title: "Discipline Name" },
{ title: "Discipline Description" },
{ title: "Delete" },
],
destroy: true
});
})//end else
});
});
PHP
public function selectProjectDefinitionDescription()
{
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$type = $session_data['usertype'];
$project_id = $session_data['project_id'];
$stuff = $this->Projects_Model->load_Project_Definition_Discipline($project_id);
echo $stuff;
}
HTML
<table id="projDiscComTable" class="table table-hover display">
<thead>
<tr>
<th>ID</th>
<th>Disciple</th>
<th>Discipline Comment</th>
<th>Delete</th>
</tr>
</thead>
</table>
Upvotes: 0
Views: 801
Reputation: 135
I figured it out, I redrew the table in the success in the ajax and it seems to work from what I can see. But thank you for all the answers. I will use what you guys have suggested in future.
var ProjDefTable = [];
$('#btnDiscipCommPost').on('click', () => {
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
$.getJSON('selectProjectDefinitionDescription', function (data) {
$.each(data, (i) => {
ProjDefTable.push([data[i].id,data[i].definition_name, data[i].description,
"<input type='button' class='btn btn-danger' id='" + data[i].id + "delete' value='-'></input> <span id='" + data[i].id + "deleted' style='display: none; font-weight: 700;'>DELETED</span>"+
"<script>"+
" $('#"+data[i].id+"delete').on('click', () => {" +
" $.ajax({"+
" url: 'delete_project_definition',"+
" type: 'GET',"+
" data: { "+
" project_def_id: '"+data[i].id+"' "+
" },"+
" success: () => {"+
" var x = document.getElementById('" + data[i].id + "delete');" +
" x.style.display = 'none';" +
" var y = document.getElementById('" + data[i].id + "deleted');" +
" y.style.display = 'block';" +
" } "+
" });"+
" });" +
"</script>"
]);
var table = $("#projDiscComTable").DataTable();
table.clear().draw();
table = $("#projDiscComTable").DataTable({
data: ProjDefTable,
columns: [
{title: "ID"},
{ title: "Discipline Name" },
{ title: "Discipline Description" },
{ title: "Delete" },
],
destroy: true
});
})//end else
});
var ProjDefTable = [];
}
})
});
Upvotes: 0
Reputation: 1661
How about just refreshing the table, after inserting the comment.?
$('#projDiscComTable').DataTable().ajax.reload();
$.ajax({
url: 'post_project_definition',
type: 'GET',
data: {
discID: $("#disciplinesProjDef").val(),
discComment: $("#txtDiscipComment").val(),
},
success: () => {
$('#projDiscComTable').DataTable().ajax.reload();
swal({
type: 'success',
title: 'Your comment has been added successfully'
});
}
})
Not sure if this fits your requirement but you can still refresh it after inserting the comment.
Upvotes: 1
Reputation: 12277
I have not used DataTable
much but I believe they are used in order to provide various functionalities such as sorting etc easily to your HTML tables. (Reference)
Coming to the problem here, I believe your scope is to refresh the content of a table based upon the AJAX response without having to refresh the whole page.
You can do it using append()
and remove()
functions of jQuery. A simple algo would be like this:
<div id="definition-row-sample">
<tr class="definition-row">
<td class="definition-id"></td>
<td class="definition-name"></td>
<td class="definition-description"></td>
<td class="definition-delete"></td>
</tr>
</div>
.definition-row
hidden so that it doesnt show up in your page.$('.definition-row').remove()
and then loop through your data and append each row in your table: $.each(data, function (i, item) {
$('#definition-row-sample').find('.definition-id').text(data[i].id);
$('#definition-row-sample').find('.definition-name').text(data[i].definition_name);
$('#definition-row-sample').find('.definition-description').text(data[i].description);
//then simply append it to the table
let content = $('#definition-row-sample').find('.definition-row').clone();
$('#projDiscComTable').append(content);
});
And as for delete, you can define a normal function like:
function definition_delete(id) {
$.ajax({
url: 'delete_project_definition',
type: 'GET',
data: {
project_def_id:id
}
});
}
document.on('click', '.definition-delete', function(){
let id = $(this).parent().find('.definition-id').text();
definition_delete(id);
});
I have not tested the code but I am sure you get the idea and make it work as I have used similar logic to one of my projects as well.
Upvotes: 1