Any ideas how I can submit say, a person ID instead of the person''s name on an autocompleter input field? Surely this is a common need. Thanks! Daniel
On 08/12/2005, at 4:38 PM, Daniel Elmore wrote:> Any ideas how I can submit say, a person ID instead of the person''s > name > on an autocompleter input field? Surely this is a common need. >It is... and it''s in the documentation (although it''s not obvious how to achieve it) Use an `afterUpdateElement:` option for the new Ajax.autocompleter(). http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter I do something similar but I handle it in a slightly more transparent way... I let the autocompleter display the name, but take action based on the ID of the <li> element that was returned from the server. You can''t really just use raw numbers as DOM element IDs, so I format mine as: <li id="entityType::entityId">Entity description</li> e.g. <li id="Fabric::24">Cross Dyed Cotton</li> My afterUpdateElement option function extracts the entity type and id, then uses it to load a preview of the selected entity: loadresult: function(element,selectedElement) { var entityParts = selectedElement.id.split(''::''); var entityType = entityParts[0]; var entityId = entityParts[1]; ... If you want to keep it simple – and use the person''s ID in the input field – just set the value of the input field to the entityID parsed from the selected element. Hope that helps. Cheers, Rob
Thank you Ron, that helps a lot! However I am experiencing a problem. I am getting a JS error "element is not defined". Also the afterupdateelement is running directly when the page loads, maybe that''s the cause of the problem. Here is my autocomplete script. new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', ''http://localhost/ajax/act_people.cfm'', { afterUpdateElement:loadSelection(element,selectedElement), paramName:"contact", frequency:0.1 }) Thanks for your time, Daniel -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Rob Wills Sent: Thursday, December 08, 2005 12:03 AM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails-spinoffs] AutoCompleter afterUpdateElement On 08/12/2005, at 4:38 PM, Daniel Elmore wrote:> Any ideas how I can submit say, a person ID instead of the person''s > name > on an autocompleter input field? Surely this is a common need. >It is... and it''s in the documentation (although it''s not obvious how to achieve it) Use an `afterUpdateElement:` option for the new Ajax.autocompleter(). http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter I do something similar but I handle it in a slightly more transparent way... I let the autocompleter display the name, but take action based on the ID of the <li> element that was returned from the server. You can''t really just use raw numbers as DOM element IDs, so I format mine as: <li id="entityType::entityId">Entity description</li> e.g. <li id="Fabric::24">Cross Dyed Cotton</li> My afterUpdateElement option function extracts the entity type and id, then uses it to load a preview of the selected entity: loadresult: function(element,selectedElement) { var entityParts = selectedElement.id.split(''::''); var entityType = entityParts[0]; var entityId = entityParts[1]; ... If you want to keep it simple - and use the person''s ID in the input field - just set the value of the input field to the entityID parsed from the selected element. Hope that helps. Cheers, Rob_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 08/12/2005, at 5:26 PM, Daniel Elmore wrote:> Thank you Ron, that helps a lot!Rob :)> > However I am experiencing a problem. > > I am getting a JS error "element is not defined". Also the > afterupdateelement is running directly when the page loads, maybe > that''s > the cause of the problem. > > new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', > ''http://localhost/ajax/act_people.cfm'', { > afterUpdateElement:loadSelection(element,selectedElement), > paramName:"contact", frequency:0.1 }) >That''s right... it is running `loadSelection()` when the page loads... or more specifically when you are calling `new Ajax.Autocompleter(...)`. This is because of the way you are specifying the "afterUpdateElement" option. You can interpret the code you have written to say "assign the result of running `loadSelection()` **right now**, to the attribute afterUpdateElement:" What you need to be doing is one of two things: a) Make the "afterUpdateElement:" point to a named javascript function. This will call the function with the documented arguments **at the time of the user''s action** new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', ''http://localhost/ajax/act_people.cfm'', { afterUpdateElement:loadSelection}) b) Use an anonymous function for the "afterUpdateElement" option. This will execute the code listed between the function braces **at the time of the user''s action** new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', ''http://localhost/ajax/act_people.cfm'', { afterUpdateElement:function(element,selectedElement){ var entityParts = selectedElement.id.split(''::''); ... etc }}) a) or b) will get the job done, but if you need to re-use the code outside of the the auto-complete event handling (for page intialisation perhaps?) then you''re best off with option a). Cheers, Rob
Yes that did it! Thanks for your time _Rob_ :) -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Rob Wills Sent: Thursday, December 08, 2005 12:45 AM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails-spinoffs] AutoCompleter afterUpdateElement On 08/12/2005, at 5:26 PM, Daniel Elmore wrote:> Thank you Ron, that helps a lot!Rob :)> > However I am experiencing a problem. > > I am getting a JS error "element is not defined". Also the > afterupdateelement is running directly when the page loads, maybe > that''s > the cause of the problem. > > new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', > ''http://localhost/ajax/act_people.cfm'', { > afterUpdateElement:loadSelection(element,selectedElement), > paramName:"contact", frequency:0.1 }) >That''s right... it is running `loadSelection()` when the page loads... or more specifically when you are calling `new Ajax.Autocompleter(...)`. This is because of the way you are specifying the "afterUpdateElement" option. You can interpret the code you have written to say "assign the result of running `loadSelection()` **right now**, to the attribute afterUpdateElement:" What you need to be doing is one of two things: a) Make the "afterUpdateElement:" point to a named javascript function. This will call the function with the documented arguments **at the time of the user''s action** new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', ''http://localhost/ajax/act_people.cfm'', { afterUpdateElement:loadSelection}) b) Use an anonymous function for the "afterUpdateElement" option. This will execute the code listed between the function braces **at the time of the user''s action** new Ajax.Autocompleter(''contact_name'', ''contact_name_auto_complete'', ''http://localhost/ajax/act_people.cfm'', { afterUpdateElement:function(element,selectedElement){ var entityParts = selectedElement.id.split(''::''); ... etc }}) a) or b) will get the job done, but if you need to re-use the code outside of the the auto-complete event handling (for page intialisation perhaps?) then you''re best off with option a). Cheers, Rob _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs