Hello all, I''m hoping to use the Ajax.Autocompleter to help users send private messages to other users on my site. As the user types in the name of another user, Ajax.Autocompleter steps in and offers a few relevant options to help them along. Once they find the right one, Scriptaculous makes it a breeze for them to select it and enter it into the input field. I''d like to take this one more step, however, and make note of the user id (the primary key associated with the name they selected from the database) as an invisible field or javascript variable. With this, it would be simple to send the private message to the appropriate user id. The problem is that since the backend side of Ajax.Autocompleter returns an unordered list, I don''t know how/ where to store the id numbers, or how the form could track that somehow. All help is welcome. Thanks everyone! -Colin
Colin I''ve sort of done this with a Local.Autocompleter (show text and hide ID, submit ID when item is selected). In order to do this you need to override the methods that write the resultdiv and the method that run when an item is selected. You can override any method of the Ajax.Autocompleter by simply including your version AFTER the original code. The browser will use your method since its the latter one. Did that help at all? ;-) -Tommy ---------- Forwarded message ----------> From: "Colin Angevine" <colinangevine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Date: Sun, 13 Aug 2006 20:53:23 -0400 > Subject: [Rails-spinoffs] Ajax.Autocompleter with a twist > Hello all, > > I''m hoping to use the Ajax.Autocompleter to help users send private > messages to other users on my site. As the user types in the name of > another user, Ajax.Autocompleter steps in and offers a few relevant > options to help them along. Once they find the right one, > Scriptaculous makes it a breeze for them to select it and enter it > into the input field. > > I''d like to take this one more step, however, and make note of the > user id (the primary key associated with the name they selected from > the database) as an invisible field or javascript variable. With this, > it would be simple to send the private message to the appropriate user > id. The problem is that since the backend side of Ajax.Autocompleter > returns an unordered list, I don''t know how/ where to store the id > numbers, or how the form could track that somehow. > > All help is welcome. Thanks everyone! > -Colin > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Colin Angevine wrote:> Hello all, > > I''m hoping to use the Ajax.Autocompleter to help users send private > messages to other users on my site. As the user types in the name of > another user, Ajax.Autocompleter steps in and offers a few relevant > options to help them along. Once they find the right one, > Scriptaculous makes it a breeze for them to select it and enter it > into the input field. > > I''d like to take this one more step, however, and make note of the > user id (the primary key associated with the name they selected from > the database) as an invisible field or javascript variable. With this, > it would be simple to send the private message to the appropriate user > id. The problem is that since the backend side of Ajax.Autocompleter > returns an unordered list, I don''t know how/ where to store the id > numbers, or how the form could track that somehow.There''s a couple ways to do this. The auto completer values are an un-ordered list, but they can have other stuff in there. You are allowed to have a tag of class "informal" that is displayed, but not used as a value. So do something like this <ul> <li> <span class="id" style="display:none">1234</span> <span class="informal">John Doe (jdoe-//Bz/+gSJx1AfugRpC6u6w@public.gmane.org)</span> </li> </ul> See http://demo.script.aculo.us/ajax/autocompleter_customized for more details and an example. -- Michael Peters Developer Plus Three, LP
Tommy, Thanks for getting back to me so quickly. Overriding the method in controls.js seems like an effective approach, but after looking through the JavaScript for almost an hour I''m having a hard time figuring out how I should go about doing this. A few questions: -Which method should I be overloading? Some methods work for the mouse functionality while others deal with the keyboard, and rather than writing this in a few times I''m hoping to just throw in a few lines of code in one place to manage this. -Where do you recommend I store the ID from the database? Should I put it in the id of the <li> perhaps? What did you do? With these two things nailed down, I think that it shouldn''t be too much of an issue to figure out how to get that ID number from the AJAX response to store it in the main page. Thanks, Colin On 8/14/06, Tommy Skaue <tommy.skaue-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Colin > > I''ve sort of done this with a Local.Autocompleter (show text and hide ID, > submit ID when item is selected). > In order to do this you need to override the methods that write the > resultdiv and the method that run when an item is selected. > You can override any method of the Ajax.Autocompleter by simply including > your version AFTER the original code. > The browser will use your method since its the latter one. Did that help at > all? ;-) > > -Tommy > > > > > > ---------- Forwarded message ---------- > > From: "Colin Angevine" < colinangevine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > Date: Sun, 13 Aug 2006 20:53:23 -0400 > > Subject: [Rails-spinoffs] Ajax.Autocompleter with a twist > > Hello all, > > > > I''m hoping to use the Ajax.Autocompleter to help users send private > > messages to other users on my site. As the user types in the name of > > another user, Ajax.Autocompleter steps in and offers a few relevant > > options to help them along. Once they find the right one, > > Scriptaculous makes it a breeze for them to select it and enter it > > into the input field. > > > > I''d like to take this one more step, however, and make note of the > > user id (the primary key associated with the name they selected from > > the database) as an invisible field or javascript variable. With this, > > it would be simple to send the private message to the appropriate user > > id. The problem is that since the backend side of Ajax.Autocompleter > > returns an unordered list, I don''t know how/ where to store the id > > numbers, or how the form could track that somehow. > > > > All help is welcome. Thanks everyone! > > -Colin > > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >
I took a look at the example on the Scriptaculous site and I''m still a bit unclear. I''m already using the informal class, and it makes good sense to use it again (with a display:none there) but I''m not sure how I''ll be able to retrieve that id number once the user selects it. Just a bit more context around how you approached it (or maybe a peek at the code you put together) would be wonderful. Thanks, Colin On 8/14/06, Michael Peters <mpeters-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> > > Colin Angevine wrote: > > Hello all, > > > > I''m hoping to use the Ajax.Autocompleter to help users send private > > messages to other users on my site. As the user types in the name of > > another user, Ajax.Autocompleter steps in and offers a few relevant > > options to help them along. Once they find the right one, > > Scriptaculous makes it a breeze for them to select it and enter it > > into the input field. > > > > I''d like to take this one more step, however, and make note of the > > user id (the primary key associated with the name they selected from > > the database) as an invisible field or javascript variable. With this, > > it would be simple to send the private message to the appropriate user > > id. The problem is that since the backend side of Ajax.Autocompleter > > returns an unordered list, I don''t know how/ where to store the id > > numbers, or how the form could track that somehow. > > There''s a couple ways to do this. The auto completer values are an un-ordered > list, but they can have other stuff in there. You are allowed to have a tag of > class "informal" that is displayed, but not used as a value. So do something > like this > > <ul> > <li> > <span class="id" style="display:none">1234</span> > <span class="informal">John Doe (jdoe-//Bz/+gSJx1AfugRpC6u6w@public.gmane.org)</span> > </li> > </ul> > > See http://demo.script.aculo.us/ajax/autocompleter_customized for more details > and an example. > > -- > Michael Peters > Developer > Plus Three, LP > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
On Mon, Aug 14, 2006 at 06:33:28PM -0400, Colin Angevine wrote:> I took a look at the example on the Scriptaculous site and I''m still a > bit unclear. I''m already using the informal class, and it makes good > sense to use it again (with a display:none there) but I''m not sure how > I''ll be able to retrieve that id number once the user selects it. Just > a bit more context around how you approached it (or maybe a peek at > the code you put together) would be wonderful.You must use a callback (with updateElement option to Ajax.Autocompleter) and that callback does something like id = chosen_option.getElementsByTagName("span")[0].innerHTML; then you can use the id as you like. Previously discussed on the list: http://lists.rubyonrails.org/pipermail/rails-spinoffs/2006-July/004864.html Saludos, HoraPe --- Horacio J. Peña horape-Zlwz598M9fGnYQj17FrgxVAUjnlXr6A1@public.gmane.org horape-03kPrFAwsvU3uPMLIKxrzw@public.gmane.org
Hi Colin I studied the code a bit, and it seems its somewhat different for the Ajax.Autocompleter compared to the Local.Autocompleter. To understand what really happens I''ve tried to wrap up some of the important events. 1) The ajax request will run onComplete when done, and if you look in the code, the onComplete runs "updateChoices". 2) updateChoices is part of the Base and fills the result from backend into the resultdiv adds some events and runs the "render" 3) render will either hide or show the result 4) when an item is clicked the event selectEntry is triggered, and that method runs updateElement 5) updateElement can be passed in as your own function using the option updateElement: myUpdateElement. The method will take whatevers in the listitem and either add it to a list of values in the formfield (if you use tokens) or just replace the value in the formfield with selected value. At the very end of this method it checks to see if there is an option to run "afterUpdateElement" which is a method that also can be added as an option. It seems to me that you need to copy updateElement, make some changes to it so it can extract the ID from say a hidden SPAN within the LI (use css to hide it) and place the ID in some hidden form field. Maybe that''ll work :D Please feel free to share code using pastebin or whatever... maybe the code can be tweaked. Either way sharing is good for the soul, and will grant you a spot in open-source-heaven ;P -Tommy ---------- Forwarded message ----------> From: "Colin Angevine" <colinangevine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Date: Mon, 14 Aug 2006 11:48:13 -0400 > Subject: Re: [Rails-spinoffs] Re: Ajax.Autocompleter with a twist > Tommy, > > Thanks for getting back to me so quickly. Overriding the method in > controls.js seems like an effective approach, but after looking > through the JavaScript for almost an hour I''m having a hard time > figuring out how I should go about doing this. A few questions: > > -Which method should I be overloading? Some methods work for the > mouse functionality while others deal with the keyboard, and rather > than writing this in a few times I''m hoping to just throw in a few > lines of code in one place to manage this. > > -Where do you recommend I store the ID from the database? Should I > put it in the id of the <li> perhaps? What did you do? > > With these two things nailed down, I think that it shouldn''t be too > much of an issue to figure out how to get that ID number from the AJAX > response to store it in the main page. > > Thanks, > Colin > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs