0

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:

<script type="text/javascript">

$(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
      }

    } );
} );                

</script>

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

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

2 Answers 2

2

I found the solution:

fnDrawCallback: function () {
  var rows = this.fnGetData();
  if ( rows.length === 0 ) {
     spinner.stop();
  }
},
Sign up to request clarification or add additional context in comments.

Comments

0

you tried to inspect the element

   "fnRowCallback": function( e ) {                        
   console.log(e);   // maybe there is a state of the response in it ;) ?
   spinner.stop();   // Hide the spinner
  }

no time for testing it properly..

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.