Featured post
ruby - Rails: How to pass value from textfield to a partial -
how can pass value auto_complete textfield partial.
<%= text_field_with_auto_complete :participant, :name, {}, {:url => {:controller => "contentcom/discussions", :action => :get_users_for_auto_complete}, :method => :get, :param_name => 'search'} %> <%= button_to_function(:ok) |page| page.insert_html :top, :participants, :partial => 'participant', :locals => end %>
bye,
nico
first of need know helpers text_field_with_auto_complete
, button_to_function
not handle user actions, generate html , javascript code. generated javascript code can interact user. in case text_field_with_auto_complete
generates following (approximately):
<input type="text" id="participant_name" name="participant[name]" size="15" /> <div id="participant_name_auto_complete" class="auto_complete"> </div> <script type="text/javascript"> var participant_auto_completer = new ajax.autocompleter ( 'participant', 'name', '/contentcom/discussions/get_users_for_auto_complete', {method: 'get', param_name: 'search'} ); </script>
the above code user gets in browser. if read documentation text_field_with_auto_complete
, see can use option :after_update_element
. option allows specify name of javascript function called when user selects 1 of proposed values.
what need do:
- write javascript function display anywhere user-selected value autocomplete field.
- call
text_field_with_auto_complete
:after_update_element
that's how template like:
<ul id="selected_participant_container"></ul> <%= text_field_with_auto_complete :participant, :name, {}, { :url => {:controller => "contentcom/discussions", :action => :get_users_for_auto_complete}, :method => :get, :param_name => 'search', :after_update_element => 'afterparticipantselected' }%> <script type="text/javascript"> function afterparticipantselected (el, value) { var container = document.getelementbyid ('selected_participant_container'); container.innerhtml = value; } </ script>
now, when user selects value in autocomplete field, displayed in element id = selected_participant_container
of course, can use methods proposed tokland.
but recommend first learn basics of html , javascript.
- Get link
- X
- Other Apps
Comments
Post a Comment