This is my datatable code:
table = $('#datatable-buttons').DataTable({
lengthChange: false,
pageLength: 25,
order: [ 0, 'desc' ],
//buttons: ['copy', 'excel', 'pdf', 'colvis']
columns: [
{ data: "sample_lab_ref" },
{ data: "sample_client_ref" },
{ data: "sample_client_ref2" },
{ data: "sample_client_ref3" },
{ data: "sample_date" },
{ data: "report_date" },
{ data: "test_parameter_name" },
{ data: "test_parameter_sop" },
{ data: "test_technique_name" },
{ data: "test_value_text" },
{ data: "test_units" }
],
columnDefs: [
{
targets: [4, 5],
visible: false,
searchable: false
}
],
ajax: {
url: "/assets/ajax/test_data_ajax_handler.php",
type: "POST",
data: {
action: "getTestData",
user_data: '<?=json_encode($userData)?>'
}
},
buttons: {
buttons: [
{ extend: 'copy', className: 'btn-info' },
{ extend: 'pdf', className: 'btn-danger' },
{
extend: 'csv',
className: 'btn-success',
text: 'Export to CSV',
exportOptions: {
modifier: {
search: 'applied'
}
}
},
{ extend: 'colvis', className: 'btn-primary' },
]
},
initComplete: function(settings, json) {
table.buttons().container()
.appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
}
});
Works fine, but in the event the function in the ajax handler returns NULL (no rows found). How can I tell the datatable to display something like "No data found". Right now it gives me an invalid json syntax error.
You can create empty json like this:
{
"data": [],
"total": 0,
"recordsTotal": 0,
"recordsFiltered": 0
}
Try using dataSrc and pass a function to process your data.. Hope the attached data pre-processing callback will be called even on HTTP errors
sample code
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"dataSrc": function ( json ) {
for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
json.data[i][0] = '<a href="/message/'+json.data[i][0]+'>View message</a>';
}
return json.data;
}
}
} );
From here Display warning if records null Datatables AJAX - dataSrc You can fix it with dataSrc
ajax: {
url: "/assets/ajax/test_data_ajax_handler.php",
type: "POST",
data: {
action: "getTestData",
user_data: '<?=json_encode($userData)?>'
},
dataSrc: function(data){
if(data.data == null){
return [];
} else {
return data.data;
}
}
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.