I have a Bootstrap modal which is launched from a link. For about 3 seconds it just sits there blank, while the AJAX query fetches the data from the database. How can I implement some sort of a loading indicator? Does twitter bootstrap provide this functionality by default?
EDIT: JS code for modal
<script type="text/javascript"> $('#myModal').modal('hide'); $('div.divBox a').click(function(){ var vendor = $(this).text(); $('#myModal').off('show'); $('#myModal').on('show', function(){ $.ajax({ type: "GET", url: "ip.php", data: "id=" + vendor, success: function(html){ $("#modal-body").html(html); $(".modal-header h3").html(vendor); $('.countstable1').dataTable({ "sDom": "T<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>", "sPaginationType": "bootstrap", "oLanguage": { "sLengthMenu": "_MENU_ records per page" }, "aaSorting":[[0, "desc"]], "iDisplayLength": 10, "oTableTools": { "sSwfPath": "swf/copy_csv_xls_pdf.swf", "aButtons": ["csv", "pdf"] } }); } }); }); }); $('#myModal').on('hide', function () { $("#modal-body").empty(); }); </script>
You can display the image just before this call to $. ajax() and then hide/remove the image in the post handler function (just before your . empty()/. append(data) calls.
The Ajax load is a method to send a request to the server using HTTP to load the data and shows data into the output screen. The Ajax load is a process to load the data of another file into an HTTP server and displays data into a web application page.
What About jQuery and AJAX? jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!
I solved the same problem following this example:
This example uses the jQuery JavaScript library.
First, create an Ajax icon using the AjaxLoad site.
Then add the following to your HTML :
<img src="/images/loading.gif" id="loading-indicator" style="display:none" />
And the following to your CSS file:
#loading-indicator { position: absolute; left: 10px; top: 10px; }
Lastly, you need to hook into the Ajax events that jQuery provides; one event handler for when the Ajax request begins, and one for when it ends:
$(document).ajaxSend(function(event, request, settings) { $('#loading-indicator').show(); }); $(document).ajaxComplete(function(event, request, settings) { $('#loading-indicator').hide(); });
This solution is from the following link. How to display an animated icon during Ajax request processing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With