Eric Boucher
2006-Jul-07 03:27 UTC
[Rails] 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Daniel N
2006-Jul-07 03:32 UTC
[Rails] dynamically updating a listbox from another listbox
On 7 Jul 2006 03:27:47 -0000, Eric Boucher < devlists-rubyonrails@devlists.com> wrote:> > 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)?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> > > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060707/64421021/attachment.html
Kevin Olbrich
2006-Jul-07 14:18 UTC
[Rails] dynamically updating a listbox from another listbox
On Friday, July 07, 2006, at 3:27 AM, Eric Boucher wrote:>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 > > > >-- >Posted with http://DevLists.com. Sign up and save your mailbox. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsThis is how I did it recently (using the KRJS plugin). ## View ... <div id=''container-type-select''> <label for=''container_type_id''>Container Type</label> <%= select_tag ''container_type_id'', options_for_select([''==Please Select==''] + ContainerType.to_dropdown(:conditions=>''can_hold_samples = true'', :order=>:name)) %> </div> <div id=''container-select''> <label for="container_id">What Container?</label> <%= select ''reagent'',''container_id'', [] %> </div> ... ## 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=>"<label for=''container_id''>What Container?</label><%= 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Kevin Olbrich
2006-Jul-07 16:30 UTC
[Rails] dynamically updating a listbox from another listbox
On Friday, July 07, 2006, at 2:19 PM, Kevin Olbrich wrote:> >On Friday, July 07, 2006, at 3:27 AM, Eric Boucher wrote: >>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 >> >> >> >>-- >>Posted with http://DevLists.com. Sign up and save your mailbox. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > >This is how I did it recently (using the KRJS plugin). > >## View > >... ><div id=''container-type-select''> > <label for=''container_type_id''>Container Type</label> > <%= select_tag ''container_type_id'', options_for_select([''==Please >Select==''] + ContainerType.to_dropdown(:conditions=>''can_hold_samples >true'', :order=>:name)) %> ></div> > ><div id=''container-select''> > <label for="container_id">What Container?</label> > <%= select ''reagent'',''container_id'', [] %> ></div> >... > >## 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=>"<label >for=''container_id''>What Container?</label><%= 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 > >-- >Posted with http://DevLists.com. Sign up and save your mailbox. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsI 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.