MacMan MacMan - 1 year ago 84
Javascript Question

Loading Overlay with DataTables

I'm using datatables and have data being collected from a database to an array then displayed onscreen. All this works fine.

If the data is around 2000 rows and 7 columns it only takes a couple of seconds, however if it's 10,000 rows and 7 columns it can take around 30 -40 seconds, so I would like to add an overlay that greys out the screen and displays "Loading.. Please Wait"

I've tried various scripts and code that I've found on this site and online, but none seem to work. I usually get the delay followed by a flash of the overlay and then it's gone.

Can any one help ?

This is the datatables code I'm using :

UPDATE: This is the page without the DB queries.

<style type="text/css">
@import "layout_page.css";
@import "layout_table.css";
html { overflow-y: scroll; };
<link href="layout.css" rel="stylesheet" type="text/css">
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.dataTables.min.js" type="text/javascript"></script>
<script src="jquery.jeditable.js" type="text/javascript"></script>
<script src="jquery.dataTables.editable.js" type="text/javascript"></script>
<body id="dt_test">
<div id="container"><div id="layout">


$GLOBALS['JSON_OBJECT'] = new Services_JSON();
function json_encode($value)
{ return $GLOBALS['JSON_OBJECT']->encode($value); }

function json_decode($value)
{ return $GLOBALS['JSON_OBJECT']->decode($value); }

$result = json_encode($data);

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
$('#example').dataTable( {
"aaData": <?php echo $result ?>,
"aoColumns": [
{ "sTitle": "Order" },
{ "sTitle": "Name" },
{ "sTitle": "Rank" },
{ "sTitle": "Number", "sClass": "center" },
{ "sTitle": "Locale", "sClass": "center" },
{ "sTitle": "Email", "sClass": "center" },
{ "sTitle": "Description", "sClass": "center" }
"bJQueryUI": true,
"bProcessing": true,
"bSort": false,
"bSortClasses": false,
"bDeferRender": false,
"sPaginationType": "full_numbers",
"aLengthMenu": [[10, 25, 50, 100, 250, 500, -1], [10, 25, 50, 100, 250, 500, "All"]],

"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
var id = "users"+":"+aData[0];
return nRow;
} );
sUpdateURL: "update.php",
} );

<div class='demo' id='demo' name='demo'></demo></div>


How would I add an overly that is displayed BEFORE anything else... then is removed one the table is ready ?

Updated my original post with the entire page minus the DB Queries.
I need the Overlay to display BEFORE any DB queries happen and before datatables starts processing the table.


Answer Source

You can do something like this :


#overlay {
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 99999;
    background-color: gray;
    filter: alpha(opacity=75);
    -moz-opacity: 0.75;
    opacity: 0.75;
    display: none;
#overlay h2 {
    position: fixed;
    margin-left: 40%;
    top: 40%;

markup :

<div id="overlay"><h2>Loading .. Please wait</h2></div>

when you want to show the message / overlay eg just before dataTable :


callback in your dataTable() when it has finished initalizing :

$('#example').dataTable( {
   fnInitComplete : function() {