I have a select dropdown and submit button. The user chooses from the select dropdown and submit. Then it will call the ajax. If found the data then that data will display in the data table otherwise it will display the alert "No data found".
Above scenario is working perfectly for me but the issue is,
The first time my ajax is working if I choose a second time from the select dropdown then I am getting the error
DataTables warning: table id=report_list - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3
.
I tried "bDestroy": true
or $("#report_list").dataTable().fnDestroy();
but it's not working perflectly. Error goes but my response data not displaying.
$("form[name='reports']").validate({
rules: {
report_type:{required:true}
},
// errorElement: 'div',
submitHandler: function(form) {
var report_type = $('#report_type').val();
var fromDate = $('#fromDate').val();
var toDate = $('#toDate').val();
$.ajax({
url: baseUrl + "/Reports_control/Get_reports",
method: "POST",
//dataType: "json",
data: {report_type: report_type,fromDate:fromDate,toDate:toDate},
success: function(response) {
$('.search_record tbody tr').hide();
var data = JSON.parse(response);
if (data.status === 'error')
{
$('.report').hide();
alert(data.msg);
}
if (data.status === 'success') {
if ( $.fn.DataTable.isDataTable( '#report_list' ) ) {
$('#report_list').destroy();
}
//alert(data);
$('.company_report').show();
var trHTML = '';
$.each(data.records, function (i, o){
trHTML += '<tr><td>'+o.Sr_no+
'</td><td>' + o.cutomer_name +
'</td><td>' + o.o_product_brandname +
'</td><td>' + o.o_product_qty +
'</td><td>'+ o.o_order_no +
'</td><td>'+ o.created_by +
'</td><td>'+ o.order_status +
'</td><td>'+ o.action_order_status +
'</td></tr>';
});
$('.search_record tbody').append(trHTML);
$('#report_list').DataTable({
language: {
sLengthMenu: "Show _MENU_",// remove entries text
searchPlaceholder: "Search",
search:""
},
"ordering": false, // remove sorting effect from header
});
}
}
});
}
});
Would you help me out in this issue?