Using jQuery UI Autocomplete with Hidden ID's

coldfusion

The new autocomplete widget in jQuery UI 1.8 is a nice addition. While it works great for basic purposes working with ID / value pairs is not so nice out of the box.

I wanted to use the autocomplete widget to allow someone to select an employee by typing in an employee name into the text box, but I want the form to post the ID of the employee, not the employee name.

First you need to setup your server side search script, I'm using ColdFusion here:

<cfparam name="url.term" default="">
<cfset emp = empDAO.searchByName(url.term)>
<cfset result = ArrayNew(1)>
<cfoutput query="emp">
	<cfset s = StructNew()>
	<cfset s["label"] = emp.FullName>
	<cfset s["value"] = emp.EE_ID>
	<cfset ArrayAppend(result, s)>
</cfoutput>
<cfset json = SerializeJSON(result)>
<cfcontent reset="true" type="application/json"><cfoutput>#json#</cfoutput><cfabort>

The jQuery UI autocomplete widget sends a variable in the query string called term which contains the text the user has typed in. You need to return an array of objects in JSON. For example, your output might look like this:

[{label:"Pete Freitag", value:1}, {label:"Pete Doe", value:2}]

Now the HTML code I am using in the form simply looks like this:

<input type="text" name="EmployeeID" value="" class="employeeAutocomplete" />

Next I need to write some jQuery JavaScript that applies the autocomplete widget to any input tag with the employeeAutocomplete, this code would go in your $(document).ready() event handler:

$('input.employeeAutocompete').autocomplete({source:'employee-search-json.cfm'});

Now this works well up to this point, but when I select an item it put's the employee ID in the text box. From the user perspective this doesn't make any sense. What I want is for the employee's name to be put in the text box, and have a hidden field containing the employee ID passed in the form.

So to accomplish that I need to do a bit more JavaScript, I'm going to do the following:

  1. Change the name on the existing input field to whatever it was plus _autocomplete_label
  2. Create a hidden input field with a name attribute value of the original input element (this will contain my ID value).
  3. Create a custom select event handler for the given jQuery UI autocomplete instance.

So here's my new code:

$('input.employeeAutocomplete').each(function() {
	var autoCompelteElement = this;
	var formElementName = $(this).attr('name');
	var hiddenElementID  = formElementName + '_autocomplete_hidden';
	/* change name of orig input */
	$(this).attr('name', formElementName + '_autocomplete_label');
	/* create new hidden input with name of orig input */
	$(this).after("<input type=\"hidden\" name=\"" + formElementName + "\" id=\"" + hiddenElementID + "\" />");
	$(this).autocomplete({source:'employee-search-json.cfm', 
		select: function(event, ui) {
			var selectedObj = ui.item;
			$(autoCompelteElement).val(selectedObj.label);
			$('#'+hiddenElementID).val(selectedObj.value);
			return false;
		}
	});
});

Now when I submit the form the value of the EmployeeID field will be an employee ID, and the text box will simply show the employee name.

Would be cool if this widget supported an option to do what I just did, but it only takes a few additional lines of code to accomplish.



Related Entries

This entry was:

WAF for CF

Trackbacks

Trackback Address: 756/29E2A4759B0FD14E123AA7DDEDAEE038

Comments

On 07/14/2010 at 4:13:31 PM EDT Mike wrote:
1
Unless I am not understanding what you are after, the AutoSuggest widget already provides this functionality. Check out

http://www.coldfusionjedi.com/index.cfm/2010/4/12/Playing-with-jQuery-UIs-Autocomplete-Control

On 07/19/2010 at 12:41:39 PM EDT Pete Freitag wrote:
2
@Mike Ray's code example there still has javascript to set the hidden ID, he's not using anything builtin to do that. We are both using the select event to do that, the main difference is that my code creates the hidden form field dynamically, Ray's code has it in the HTML to start with.

On 07/19/2010 at 10:27:06 PM EDT Brian wrote:
3
Pete - be aware that using jQuery 1.4 you need to return valid JSON which means keys are quoted too:

[{"label":"Pete Freitag", "value":1}, {"label":"Pete Doe", "value":2}]

It's possible UI is using getAJAX and then eval'ing it but jQuery 1.4 core has already switched to requiring strict JSON for requests like getJSON().

On 08/12/2010 at 10:50:38 PM EDT Jonathan wrote:
4
Hi Pete,

Thank you very much for this tutorial. It really helped me with the new ui-autocomplete !

http://stackoverflow.com/questions/3446297/posting-value-to-server-using-jquery-ui-autocomplete-and-jeditable

Cheers mate

On 08/26/2010 at 10:30:30 PM EDT Alan McCollough wrote:
5
This Was Awesome! Thank you for solving this puzzle. I spent a morning trying to grasp why the jQuery UI autocomplete was not doing what seemed obvious (to me at least), then found your excellent post. You've saved me a handful of hair.

On 08/27/2010 at 10:21:28 PM EDT Alan McCollough wrote:
6
Got a question. I've used your autocompleter workaround, and it's fantastic for filling out a new form.

Got any suggestions on how to deal with laoding the fields with existing data, as when editing an existing entry?

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?
pfreitag on twitter