Dynamically updating a listbox from another listbox

Hi,

I would like to be able to dynamically (without reloading the page)
change the values from one listbox when a user select items from another
listbox. I have a model ‘Sector’ which has_many ‘levels’ (and of cours,
‘Level’ beyong_to ‘sector’). I want to show the user two listboxes with
the sectors and levels. But when a user select a certain ‘sector’, the
‘levels’ listbox should be populated dynamically with only the ones
related to that sector. I’m using collection_select for my listboxes.

Is it possible (with some AJAX maybe)?

Thanks

On 7 Jul 2006 03:27:47 -0000, Eric B. <
[email protected]> wrote:

Is it possible (with some AJAX maybe)?

Technoweenie has posted a tip for this on rails weenie

http://rails.techno-weenie.net/tip/2006/6/30/prefill-a-select-box-from-an-ajax-request

Thanks

On Friday, July 07, 2006, at 3:27 AM, Eric B. wrote:

Is it possible (with some AJAX maybe)?
http://lists.rubyonrails.org/mailman/listinfo/rails
This is how I did it recently (using the KRJS plugin).

View

Container Type <%= select_tag 'container_type_id', options_for_select(['==Please Select=='] + ContainerType.to_dropdown(:conditions=>'can_hold_samples = true', :order=>:name)) %>
What Container? <%= select 'reagent','container_id', [] %>
...

Controller

def on_container_type_id_change
render :update do |page|
@containers =
Container.find_all_by_container_type_id(params[:dom_value],
:order=>‘name’)
page.replace_html ‘container-select’, :inline=>“What Container?<%= select
‘reagent’,‘container_id’, @containers.map {|o| [o.title, o.id]} %>”
end
end

The KRJS plugin automagically creates an AJAX call for an ‘onChange’
event on ‘container_type_id’, which then gets called in the controller

Probably the easiest implementation of this I’ve seen. Just remember to
replace the entire select div.

_Kevin

On Friday, July 07, 2006, at 2:19 PM, Kevin O. wrote:

related to that sector. I’m using collection_select for my listboxes.
Rails mailing list
<%= select_tag ‘container_type_id’, options_for_select([’==Please

Controller

_Kevin


Posted with http://DevLists.com. Sign up and save your mailbox.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I also wrote up a quick blog about this since it seems to come up so
often.

http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs