Dynamic Select Box


#1

Hi,

I’m new to rails/ruby having come from PHP and am just starting to get
my head round how easy it can be :slight_smile:

However, can anyone point me in the right direction for dynamicaly
updating a select box based upon the choice of a previous select box,
without a page refresh.

Any and all help greatly appreciated.

Thanks


#2

Using RJS calls with an observe_field is the way to go. Works great in
FF
but you cannot do this in IE by using RJS alone. The reason is that RJS
uses the innerHTML method to replace content with a page.html_replace
call.
IE does not support the innerHTML call on select boxes. You would need
to
implement it in javascript completely.

The only way that I can think of (maybe) is to use a javascript array
and
update the array from an RJS call.

Cheers


#3

Liquid wrote:

Using RJS calls with an observe_field is the way to go. Works great in
FF but you cannot do this in IE by using RJS alone. The reason is that
RJS uses the innerHTML method to replace content with a
page.html_replace call. IE does not support the innerHTML call on
select boxes. You would need to implement it in javascript completely.
Or rip out the whole select box and replace it, rather than just the
option list.


Alex


#4

I did try to rip out the entire select box and replace it, but for some
reason that I still havn’t worked out, once I replaced the select box,
the
observe_field didn’t work anymore so my next box (I had 3) was never
going
to update.


#5

My approach was exactly that…

My select box was monitored by an observer… The observer called another
action that rendered the partial for the second select box.


#6

On Apr 12, 2006, at 5:02 PM, Liquid wrote:

I did try to rip out the entire select box and replace it, but for
some reason that I still havn’t worked out, once I replaced the
select box, the observe_field didn’t work anymore so my next box (I
had 3) was never going to update.

Here is one way that we have been doing dynamic select boxes. Maybe
this helps you a bit.

----The first select box --------

<%= select_tag( :model_year,
options_for_select( @model_years, session
[:PersVeh].model_year ),
:onchange => remote_function( :update =>
‘NOTUSED’,
:with =>
“‘model_year=’+value”,
:loading =>
“Element.show(‘loading-indicator1’)”,
:complete =>
evaluate_remote_response,
:url =>
{ :action => :vehicle_select_make } ) ) %>
<%= image_tag(‘indicator.gif’,
:id => ‘loading-indicator1’,
:style => ‘display:none;’ ) -%>

----submits to an action that fills the variables and then does
render :layout => false----

----Here is what the action renders------

<%= update_element_function(‘vehicle_makes’,
:content => render( :partial =>
‘vehicle_select_make’ ) ) %>
Element.hide(‘loading-indicator1’);
Element.show(‘vehicle_makes’);
<%= update_element_function(‘button-group’,
:content => render( :partial =>
‘vehicle_buttons’ ) ) %>

So basically the trick is with the :complete =>
evaluate_remote_response in the select tags. This makes the results
of the request get eval’ed in JS.

Hope that helps a bit.

Cheers-
-Ezra


#7

Thanx Ezra…

Is there some documentation for evaluate_remote_response? Is this a
ruby/rails method or a JS function? I’m not familiar with this one.

Cheers


#8

I’m not sure if there is any docs for that :wink: I found it a long time
ago by accident and I don’t remember seeing much docu for it.

Ohh wait here it is!

http://rubyonrails.com/rails/classes/ActionView/Helpers/
PrototypeHelper.html#M000419

Cheers-
-Ezra


#9

Cool … Thanx I’ll give it a try…


#10

can i use ruby to develope mobile application

andre


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#11

Thanks Ezra. That’s just what i needed

Karl


#12

Yes

Dave M.


#13

Daniel ----- wrote:

Cool … Thanx I’ll give it a try…

Were you able to imlement the dynamic select box? I have a similar
requirement to populate City based on State and then Locality based on
City.

If yes, could you please email me the code or paste the code here.

Thx.
Ramanan


#14

On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:

Ramanan


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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Take a look at this…

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

_Kevin
www.sciwerks.com


#15

Kevin O. wrote:

On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:

Ramanan


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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Take a look at this…

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

_Kevin
www.sciwerks.com

I have a working code where there are only 2 select boxes i.e.
populating city when state is selected. I am having issues with
populating locality based on selected city. Working with 2 dynamic
select is straight forward. Working with more than 2 dynamic/cascading
select box is a real issue.

Thx. for your help


#16

I am experiencing problems retrieving information from the related drop
downs.
I can save information in the database (i.e. state_id and city_id are
getting saved in the Address table).

However on the Address’s edit/update screen, the information from the
first “State” drop down is getting retrieved
properly but nothing is getting displayed in the second drop down -
“City”.

I am using observe fields to retrieve data for the second drop down.
Please advise how to prepopulate information for the second drop down
based on the “id” stored in the database on Edits screens.

Thanks in advance for your help!

I

Ramanan wrote:

Kevin O. wrote:

On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:

Ramanan


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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Take a look at this…

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

_Kevin
www.sciwerks.com

I have a working code where there are only 2 select boxes i.e.
populating city when state is selected. I am having issues with
populating locality based on selected city. Working with 2 dynamic
select is straight forward. Working with more than 2 dynamic/cascading
select box is a real issue.

Thx. for your help


#17

Here is relevant code:
The “city” in the second dropdown is not getting selected in Edit
screen.

Thank you so much for you help!

_form.rhtml

<%= collection_select(:address, "state_id", State.find(:all), :id, 

:state) %>

<%= render :partial => ‘select_city’
%>

        <%= observe_field("address_state_id",
                :frequency => 0.25,
                :update => "address_city_container",
                :url => {:controller => 'address', :action => 

:filtered_city_select},
:with => “‘state_id=’ + value”)%>
<% --------
- address_controller.rb

def filtered_city_select

@cities = City.find_all_by_state_id(@params[“state_id”])
render :partial => ‘select_cities’
end

def edit
@address = Address.find(params[:id])
@states = State.find_all
@cities = City.find(:all, :conditions =>[ “state_id = ?”,
@address.state_id])

end

%>


partial _select_cities.rhtml

<% if @cities %>

– Select City –
<%=
options_from_collection_for_select(@cities, “id”, “city”)
%>

<% end %>


#18

Iw wrote:

:state) %>

  • address_controller.rb
    @address = Address.find(params[:id])
    partial _select_cities.rhtml

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

Couple of points.

  1. if you set the frequency of your observer to 0, it should fire off
    whenever the value changes, and not every 0.25 seconds.

  2. check your log files to see if you are getting errors thrown by the
    filtered_city_select method

  3. Don’t use ‘@params’, it’s deprecated.

Does it work at all?

_Kevin


#19

Iw wrote:

I am using observe fields to retrieve data for the second drop down.

On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:
Take a look at this…
select is straight forward. Working with more than 2 dynamic/cascading
select box is a real issue.

Thx. for your help


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

You’d have to give us a bit more information about how you are
populating your select in the first place.

_Kevin


#20

You can do it with mostly javascript.

Here is an Ajax ‘double select’ widget, in object-oriented,
cross-browser JavaScript. Very easy to plug and play:

http://www.webonweboff.com/widgets/ajax/ajax_linked_selection.aspx

Article includes a demo, source code, explanations. The page that
actually retrieves the db data will need to be in your web language of
choice, of course.

Hope this helps…