5

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']]
} );
0

4 Answers 4

6

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": []
}';
Sign up to request clarification or add additional context in comments.

3 Comments

I have a spinner that shows with spinner.spin(target); and stops with spinner.stop();. The spinner shows up when data is retrieved from the server and stops when the data has come. Anyway I would like to stop the spinner even when empty data is coming like this example. How can I accomplish that?
Good stuff. For newer DataTables (1.10 at the time of writing this), use {"draw": 0, "recordsTotal": 0, "recordsFiltered": 0, "data":[]}.
Amazing answer. +10. saved my time
3
echo json_encode(array('aaData'=>'')); 

works for me.

Comments

1

For those looking for an ASP.NET solution, here's an example using JSON.NET:

JObject jObj = new JObject(
    new JProperty("draw", 0),
    new JProperty("recordsTotal", 0),
    new JProperty("recordsFiltered", 0),
    new JProperty("data", new JArray())
);

return Content(jObj.ToString(Formatting.None), "application/json");

The parameters in the other answers here are for legacy versions of DataTables, I think it still might be backwards compatible with them though, at least aaData is handled from what I've seen in the code.

Comments

1

Try using defaultContent attribute per columns objects...

PD: Double quotes arent necessary

columns: [                               
       {bSortable: true, defaultContent: '' }, // attachments
       {bSortable: true, defaultContent: '' }, //Subject Line
       {bSortable: true, defaultContent: '' }, // Date Sent
       {bSortable: true, defaultContent: '' }, // File Name
       {bSortable: false, defaultContent: '' },
       {bSortable: false, defaultContent: '' },
       {bSortable: true, defaultContent: '' },
       {bSortable: true, defaultContent: '' },
       {bSortable: true, defaultContent: '' },
       {bSortable: false, defaultContent: '' }
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.