Datatables callback/event when empty ajax response

Datatables callback/event when empty ajax response

oualidoualid Posts: 2Questions: 0Answers: 0
edited October 2013 in General
The code below does the following things:

1. Sends a request to getData.php to get some data.
2. The spinner is shown when the server-side code is working to retrieve data.
3. The spinner is hidden when the data has come

My problem is that I don't know how to do to hide the spinner even when no data is coming.

The jquery code:

[code]


$(document).ready(function() {

var spinnerOpts = {
// Options for the spinner here...
...
};

var target = document.getElementById('spinn');
var spinner = new Spinner(spinnerOpts);


$('#myTable').dataTable( {
"bProcessing": true,
"sAjaxSource": "getData.php",
"fnPreDrawCallback": function() {
spinner.spin(target); // Show the spinner
},
"fnRowCallback": function() {
spinner.stop(); // Hide the spinner
}

} );
} );



[/code]

The following code sends a json string from getData.php when there is no data:

[code]

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

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Use fnDrawCallback:

    [code]
    fnDrawCallback: function () {
    var rows = this.fnGetData();
    if ( rows.length === 0 ) {
    ...
    }
    }
    [/code]

    Allan
  • oualidoualid Posts: 2Questions: 0Answers: 0
    This works, thanks!

    Why === and not == in the if statement?
This discussion has been closed.