Ruby on Rails Wednesday, March 5, 2014

You'd need to do this in Javascript using what's called a "chained select".  There's a variety of plugins that do this, but here's one: http://www.appelsiini.net/projects/chained

You typically have two options:
  1. Write out all of the available options for your second select when the page loads, then selectively hide/show some of them depending on the first select's selection.  
  2. Make an ajax call back to your application when the first select is changed which will return to you the available options to populate the second select.  This may be the best option if you have a lot of options in your selects.
If you need to support clients that do not have javascript enabled: I've also seen option #1 done with optgroups, so you'd write out your second select with the corresponding options for each option in the first select nested within optgroups.  You'd then selectively hide/show the options depending on what's selected from the first select.  The advantage here is that when JS is not available, the user just needs to scroll through the second select to find the options that correspond to their selection from the first select.  It's not ideal, but it's a reasonable fallback.

Jim

On Tuesday, March 4, 2014 4:28:04 AM UTC-5, Ruby-Forum.com User wrote:
Hello,

     I am trying load a drop down list based on selection from first
drop
down list. Based on first drop down list selection, I am able to fetch
data for second drop down list but I am not able to refresh second
dropdown list contents.

How Can I refresh second drop list on selection of first drop down list,
I have values for drop down list in an array.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/48a435a0-8c8a-4411-aefb-54fb47ce6496%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment