1

I have a bar graph in my web app (drawn using javascript). It can be wider than the screen, so there are arrows that move the graph along the x-axis on a click (again made using javascript).

I would like to redraw the graph when the user clicks an arrow. The data manipulation performed to generate the graph is too complex to do on the client side, and I don't want to send it all at once, as that would be a waste if the user never decides to scroll.

Instead, on a click I would like to send a request to the server for the new data needed to draw the next portion of the graph. How can this be done?

Note: I only want to request the data, not a new page. The server side is Ruby on Rails v. 3.0.4.

2 Answers 2

1

You need Ajax for this. You could use the built-in Prototype or any other Javascript library to accomplish this. So, giving you an example using jQuery you could add a click handler

$('#target').click(function() {
  // Use the $.ajax or equivalent function to send a request to a method in your controller
});

So in this example, in your controller you could either mention the dom manipulation code in the method like below:

def target_method
  # Process data 
  # dom manipulation code
  render :update do |page|
    #use a rails javascript helper like replace_html or the likes
  end 
end

Or go the Unobtrusive Javascript way and insert the DOM manipulation code inside a js.erb file.

Here are a few helpful links

  1. Rails API on Prototype helpers
  2. Unobtrusive JavaScript on Rails
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I will go with jQuery as you recommended as I am already using it. Including prototype at this point will cause it to fight over the '$' symbol with jQuery.
If you need to use both libraries you could use jQuery's noconflict method. api.jquery.com/jQuery.noConflict
1

you will need to look into AJAX http://edgeguides.rubyonrails.org/ajax_on_rails.html

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.