JQuery DataTables - AJAX reloading how to catch wh

2019-05-11 05:28发布

问题:

I am using a datatable and allow records to be deleted from the database. After deletion, I reload the table to display the recent changes. However, if I delete the last record from the database, null is returned by the AJAX call and does not update the datatable (it still displays the record that was just deleted).

My Call after the delete function is performed:

oTable.fnDraw();
oTable.fnReloadAjax();

The console error here is:

json.aaData is null
[Break On This Error] for ( var i=0 ; i<json.aaData.length ; i++ ) 

Is there a way to detect when nothing is returned from AJAX so that the datatable can detect that there are no records and display the 'datatable empty' message (i.e. No data available in table)?

回答1:

Ok so the issue was on the server side, when my sql queries were empty it was returning NULL as the aaData['aaData'] value.

Just added this and now it returns as a table that is just empty:

if(mysql_num_rows($result)==0){
        $aaData = array();
        $aaData['aaData']='';
        echo json_encode($aaData);
        exit;
    }


回答2:

Put your JSON data in a wrapper, and have the wrapper tell you there's no data, instead of returning null:

{"data":[... your current data ...],"total":0,"success":true}


回答3:

I suspect you need to look at your server side code and find why aaData is null. I use datatables and when there are no records the json generated is:

...,"aaData":[],...

and empty message displays fine.