James Ludlow
2006-Sep-02 05:21 UTC
Replacing an HTML select tag with text_field_auto_complete + hidden field?
I''m trying to replace an HTML select tag with an auto-completing text field. This field is part of a larger form. The original select is used to choose a player. This works, but as the number of players grows the number of option tags becomes way too big. So, it seemed like a great idea to provide an auto-completing type ahead. The problem that I''m having is that when I submit my form I don''t really care about the player name. I want the player id (much like when using a select). What I''m trying to do is populate a hidden field with the player id when the user chooses a player name. My latest attempt involves playing with :after_update_element. I can''t really find anything resembling good documentation for this, so I''m kind-of guessing. Is there anything that I can add in the JavaScript function that will populate the hidden id field? <%= start_form_tag ({:action => ''log'', :id => @player}, {:AUTOCOMPLETE => "off"}) %> <label for="player_name">Player</label> <%= text_field_with_auto_complete :player, :name, {}, {:after_update_element => "function(element,value){ ...???... }"} %> <%= hidden_field :player, :id %> <%= end_form_tag %> def auto_complete_for_player_name name = params[:player][:name] query = ''%'' + name + ''%'' @players = Player.find(:all, :conditions => [''firstname ilike ? or lastname ilike ? or nickname ilike ?'', query, query, query], :limit => 15, :order => "lastname") render :inline => "<%= auto_complete_result(@players, ''firstname'') %>" end The dropdown works fine. I see the search results that I expect, the arrow keys work, selection with Enter works, etc. I just can''t figure out how to populate the id. Thanks, -- James --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
_Kevin
2006-Sep-02 11:15 UTC
Re: Replacing an HTML select tag with text_field_auto_complete + hidden field?
James Ludlow wrote:> I''m trying to replace an HTML select tag with an auto-completing text > field. This field is part of a larger form. The original select is > used to choose a player. This works, but as the number of players > grows the number of option tags becomes way too big. So, it seemed > like a great idea to provide an auto-completing type ahead. > > The problem that I''m having is that when I submit my form I don''t > really care about the player name. I want the player id (much like > when using a select). What I''m trying to do is populate a hidden > field with the player id when the user chooses a player name. > > My latest attempt involves playing with :after_update_element. I > can''t really find anything resembling good documentation for this, so > I''m kind-of guessing. Is there anything that I can add in the > JavaScript function that will populate the hidden id field? > > > <%= start_form_tag ({:action => ''log'', :id => @player}, {:AUTOCOMPLETE > => "off"}) %> > <label for="player_name">Player</label> > <%= text_field_with_auto_complete :player, > :name, > {}, > {:after_update_element => "function(element,value){ ...???... }"} %> > > <%= hidden_field :player, :id %> > <%= end_form_tag %> > > > def auto_complete_for_player_name > name = params[:player][:name] > query = ''%'' + name + ''%'' > @players = Player.find(:all, > :conditions => [''firstname ilike ? or > lastname ilike ? or nickname ilike ?'', query, query, query], > :limit => 15, > :order => "lastname") > > render :inline => "<%= auto_complete_result(@players, ''firstname'') %>" > end > > > The dropdown works fine. I see the search results that I expect, the > arrow keys work, selection with Enter works, etc. I just can''t figure > out how to populate the id. > > > Thanks, > -- JamesI usually do a lookup on the name in the controller @player = Player.find_by_name(params[:player][:name]) _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
James Ludlow
2006-Sep-02 13:21 UTC
Re: Replacing an HTML select tag with text_field_auto_complete + hidden field?
On 9/2/06, _Kevin <kevin.olbrich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I usually do a lookup on the name in the controller > > @player = Player.find_by_name(params[:player][:name]) >That would assume that names are unique, which is not true. That''s why I need to pass in the ID. -- James --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Justin Forder
2006-Sep-02 14:59 UTC
Re: Replacing an HTML select tag with text_field_auto_complete + hidden field?
James Ludlow wrote:> On 9/2/06, _Kevin <kevin.olbrich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I usually do a lookup on the name in the controller >> >> @player = Player.find_by_name(params[:player][:name]) >> > > That would assume that names are unique, which is not true. That''s > why I need to pass in the ID.If the names in the autocomplete field aren''t unique, how does the user know he or she is making the right selection? regards Justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
James Ludlow
2006-Sep-03 00:37 UTC
Re: Replacing an HTML select tag with text_field_auto_complete + hidden field?
On 9/2/06, Justin Forder <justin-zSfPWr5aQuznITO/+xaoB7VCufUGDwFn@public.gmane.org> wrote:> > James Ludlow wrote: > > On 9/2/06, _Kevin <kevin.olbrich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I usually do a lookup on the name in the controller > >> > >> @player = Player.find_by_name(params[:player][:name]) > >> > > > > That would assume that names are unique, which is not true. That''s > > why I need to pass in the ID. > > If the names in the autocomplete field aren''t unique, how does the user > know he or she is making the right selection?Because I''m showing more than just the name in the auto complete div. I thought about just adding the ID to the name and then parsing it out when the form is submitted. I still may end up with this, but it seems ugly. -- James --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
_Kevin
2006-Sep-03 12:09 UTC
Re: Replacing an HTML select tag with text_field_auto_complete + hidden field?
James Ludlow wrote:> On 9/2/06, Justin Forder <justin-zSfPWr5aQuznITO/+xaoB7VCufUGDwFn@public.gmane.org> wrote: > > > > James Ludlow wrote: > > > On 9/2/06, _Kevin <kevin.olbrich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I usually do a lookup on the name in the controller > > >> > > >> @player = Player.find_by_name(params[:player][:name]) > > >> > > > > > > That would assume that names are unique, which is not true. That''s > > > why I need to pass in the ID. > > > > If the names in the autocomplete field aren''t unique, how does the user > > know he or she is making the right selection? > > Because I''m showing more than just the name in the auto complete div. > I thought about just adding the ID to the name and then parsing it out > when the form is submitted. I still may end up with this, but it > seems ugly. > > -- JamesIf you have enough info in the auto_complete for the user to know the record is unique, then you should be able to find the record through the text as well. If the submitted param looks something like ''player name - Team'', then just split the param before doing the look-up. @player = Player.find_by_name_and_team(name, team) It would be nice if the auto_complete sent back the id of the match, but I don''t think it does that right now. _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Michael Modica
2006-Sep-03 19:02 UTC
Re: Replacing an HTML select tag with text_field_auto_comple
James Ludlow wrote:> On 9/2/06, Justin Forder <justin-zSfPWr5aQuznITO/+xaoB7VCufUGDwFn@public.gmane.org> wrote: >> >> If the names in the autocomplete field aren''t unique, how does the user >> know he or she is making the right selection? > > Because I''m showing more than just the name in the auto complete div. > I thought about just adding the ID to the name and then parsing it out > when the form is submitted. I still may end up with this, but it > seems ugly. > > -- JamesJames, Do you still need help with this? I was able to do it using something sort of similar to what you had posted above. I think our needs are very much alike. For purposes of example, I have an order and I need to associate a contact to it. I want to associate the id of the contact to the order. My partial rhtml looks like this (very simple for demo): <ul class="moduleListTitle"> <% for contact in @contacts do -%> <li class="ListRow"> <div class="ListRow" id="<%=contact.id%>" onclick="$(''order_contact_id'').value = this.id"><%=h contact.full_name %></div> <div class="ListRow"> <span><%=h contact.phone_number %></span> </div> </li> <% end -%> </ul> Then, my view looks like this: <%= hidden_field :order, :contact_id %> <%= text_field_with_auto_complete :order, :contact_name, {}, :skip_style => true %> When I save, the contact id is saved on the order. Is this helpful? Michael -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Michael Modica
2006-Sep-04 16:33 UTC
Re: Replacing an HTML select tag with text_field_auto_comple
> > It would be nice if the auto_complete sent back the id of the match, > but I don''t think it does that right now. > > _KevinKevin, Read my reply to this question as I was able to get the id by using a hidden field! Michael -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---