I have used fnDestroy() to clear the dataTable on change of a select element, but it doesn't clear the table, instead it appends the data into the table. Here's the code.
HTML:
<div class="col-sm-2">
<select class="form-control" id="changeView">
<option value="1">All</option>
<option value="2">Compiled</option>
<option value="3">On-Going</option>
<option value="4">Cancelled</option>
</select>
</div>
<table class="table table-condensed" id="documentsTable">
<thead>
<tr>
<th>From</th>
<th>Status</th>
<th>Subject</th>
<th>Date Received</th>
<th>Due Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JQuery:
$('#changeView').on('change', function() {
var value = $(this).val();
var change = '';
if (value === '1') {
change = 'All';
} else if (value === '2') {
change = 'Compiled';
} else if (value === '3') {
change = 'On-Going';
} else if (value === '4') {
change = 'Cancelled';
}
loadDocuments(change);
});
function loadDocuments(change) {
$('#documentsTable').dataTable().fnDestroy();
$.post(base_url + "admin/document/getAllDocuments", {change: change}, function(response, status) {
var result = JSON.parse(response);
$.each(result, function(i, field) {
var dateReceived = format_mysqldate(field['dateReceived']);
var dueDate = format_mysqldate(field['dueDate']);
$('#documentsTable tbody').append('<tr><td>' + field['from'] + '</td><td>' + field['status'] + '</td><td>' + field['subject'] + '</td><td>' + dateReceived + '</td><td>' + dueDate +
'</td><td><div class="visible-md visible-lg visible-sm visible-xs btn-group">' +
'<a href="' + base_url + 'admin/document/view/' + field['id'] + '" class="btn btn-primary btn-xs" title="View Details" data-toggle="tooltip"><i class="glyphicon glyphicon-search"></i></a>' +
'<a href="' + base_url + 'admin/document/edit/' + field['id'] + '" class="btn btn-success btn-xs" title="Edit Document" data-toggle="tooltip"><i class="glyphicon glyphicon-pencil"></i></a>' +
'</div></td></tr>');
});
$('#documentsTable').dataTable({
"aoColumns": [
null,
null,
null,
null,
null,
{"bSortable": false}
],
"order": [[3, "desc"]],
"bDestroy": true
});
});
}
Can any one tell me what is wrong with my code? I can't get to clear the dataTable in every change of selection. Thank you ahead.
You may try to clear the datatable first and then destroy it.
dataTable.fnClearTable();
dataTable.fnDraw();
dataTable.fnDestroy();
Clear and destroy methods could be called like this,
var table = $('#example').DataTable();
//clear datatable
table.clear().draw();
//destroy datatable
table.destroy();
Reference;
Note: This code will work for datatable version 1.10 and above.
It could be destroy() http://datatables.net/reference/api/destroy(). Then empty the tbody.
$('#documentsTable').dataTable().destroy();
$('#documentsTable tbody').empty();
Use This Code to clear and destroy datatable
$('#example1').dataTable().fnClearTable();
$('#example1').dataTable().fnDraw();
$('#example1').dataTable().fnDestroy();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With