scim
scim

Reputation: 55

Rails 3: How to get javascript to run after link_to

In my view I have a link_to for downloading an excel file:

<%= link_to 'Download Excel', url_for( :controller => "my_controller", :action => "file", :format => 'xlsx', :params => params ) %>

The controller uses axlsx to render the file like:

  format.xlsx {
    render xlsx: 'file', filename: 'filename', disposition: 'inline'
  }

Now it can take a bit of time to generate and return that file, so I'd like to give the user an indication that the site's working.

I have a hidden div in the view code:

<div id="loader"><img src="/assets/loading.gif" ></div>

When someone clicks on the link, I can show the div with this jQuery:

$('#excel_export').click(function() { 
  $("#loader").show();
});

My question is: how can I .hide() that div when the file download starts? Maybe an ajax callback of some sort?

Upvotes: 1

Views: 183

Answers (2)

Deepak A
Deepak A

Reputation: 322

I don't know if this may help you. I found this code helpful to show spinner while an ajax request is performed.

$(document)
    .ajaxStart(function() {
        $("#loader").show();
    })
    .ajaxStop(function() {
        $('#loader').hide();
    });

You can get more samples from the following link.

Upvotes: 1

Jeet
Jeet

Reputation: 1380

You can use setTimeout() function by checking general time taken for download to start

Like this

$('#excel_export').click(function() { 
  $("#loader").show();
  setTimeout($("#loader").hide();, 1000);
});

So it will be automatically close after 1000ms

Upvotes: 0

Related Questions