Sheraz Sheraz - 1 month ago 7
Ajax Question

Datatable error for empty ajax response

i am using Datatable 1.9.2 in my project. I am display a list of applicants in it via AJAX. There is also a filter form which is used to filter data. Everything is working fine, but the problem is that if i filter records and no data is returned by DB then datatable generates an error in POPUP. Can someone guide me how to handle empty ajax response with datatable, how to handle empty dataset.


Below is the code which i am using

$('#applicants_list').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "bootstrap",
"sDom": '<"H"Cfr>t<"F"ip>',
"oColVis": {
"activate": "mouseover",
"aiExclude": [ 10 ],
"sAlign": "left"
},
"bFilter": false,
"sAjaxSource": script.php,
"aoColumns": [
{"bSortable": true }, // attachments
{"bSortable": true }, //Subject Line
{"bSortable": true }, // Date Sent
{"bSortable": true }, // File Name
{"bSortable": false },
{"bSortable": false },
{"bSortable": true },
{"bSortable": true },
{"bSortable": true },
{"bSortable": false }
],
"aaSorting": [[0, 'desc']]
} );

Answer

Just return following data from server/ajax response when filter return record is empty it will display empty record message.

echo '{
    "sEcho": 1,
    "iTotalRecords": "0",
    "iTotalDisplayRecords": "0",
    "aaData": []
}';
Comments