TheFlanman91 TheFlanman91 - 2 years ago 147
Ruby Question

Display spinner while DataTables table loads Ruby on Rails

Using Rails I have built a web app. One of the pages of the web app displays a table which uses the DataTables API. This JSFiddle shows an example of what my web app looks like.

The problem with this is that when I begin to add in large amounts of data (current test data is 1500 rows), the table loads each row first before running the javascript meaning you get an unformated table for a few seconds before the Javascript kicks in and DataTables activates.

I would like to display a spinner, or processing message (something along those lines) in place of the table until the page has fully populated, once that has finished I would like to run my javascript activating DataTables.

EDIT: My main issue is I'm not sure how to use Javascript to display the spinner while the table loads but then change to the table once the page has finished loading

My code is as follows:


<div class="list">
<div class="container">
<div class="row">
<div class="col-md-12">
<table id="app-list-table" class="display table" cellspacing="0">
<tr class="table-headers">
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
<th>Header 7</th>
<th>Header 8</th>
<th>Header 9</th>
<th>Header 10</th>
<%= Server.find_each do |server| %>
<td><%= link_to(server.server_name, server_path(server)) %></td>
<td><%= server.application %></td>
<td><%= server.server_role %></td>
<td><%= server.team_contact %></td>
<td><%= server.individual_contact %></td>
<td><%= server.business_owner %></td>
<td><%= server.vendor %></td>
<td><%= server.vendor_contact %></td>
<td><%= link_to("Click Here", server.main_doc) %></td>
<td><%= server.main_win %></td>
<% end %>


$(document).ready(function() {
var table = $('#app-list-table').DataTable({
"scrollX": true


Please let me know if there is anything else you would like me to include.

Answer Source

You can add in a spinner gif (find one here: as a div where your table should be and then clear it when the table loads using initComplete.

Something like:

<img id="loadingSpinner" src="/myspinner.gif">//put this right below `<div class="col-md-12">`

And then call your table like this:

 $(document).ready(function() {
    var table = $('#app-list-table').DataTable({
//any other datatables settings here
"initComplete": function(settings, json) {
//or $('#loadingSpinner").empty();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download