jQuery AJAX Autocomplete with JSON data
I recently had a requirement to implement an auto-complete input box in a dynamic form. Since I was already using jQuery, I had a look at the available plugins.
After sorting through a few, I decided on this one from Jörn Zaefferer. The only problem was, I wanted to use the wonderfully simple Zend Framework AjaxContext Action Helper to produce JSON data as my auto-complete data source. I like JSON as it’s easy to produce and easy to deal with in disparate systems (such as PHP and JavaScript). Unfortunately, Jörn’s plugin is built to work with a simple, formatted ASCII list delimited by newlines. Fortunately, he also put a lot of work into this plugin, he just hasn’t gotten around to documenting it all yet.
For this post, I’ll assume my AJAX request is for some user data out of LDAP (or a similar data store) and resembles this
{"users":[ {"uid":"123","displayName":"User 123","mail":"123@example.com"}, {"uid":"456","displayName":"User 456","mail":"456@example.com"}, {"uid":"789","displayName":"User 789","mail":"789@example.com"}, ]} |
The secret to extending the Autocomplete plugin is the ability to overwrite certain core functions, primarily parse. The internal version of this function simply loops over each line of the returned data and “parses” it into an array of objects, each containing the following attributes:
data– the entire entryvalue– the default display valueresult– the data to populate the input element on selection
You can overwrite this by passing your own parse function as part of the options object to autocomplete. Mine also includes a custom formatItem function for displaying each entry.
var acOptions = { minChars: 3, max: 100, dataType: 'json', // this parameter is currently unused extraParams: { format: 'json' // pass the required context to the Zend Controller }, parse: function(data) { var parsed = []; data = data.users; for (var i = 0; i < data.length; i++) { parsed[parsed.length] = { data: data[i], value: data[i].displayName, result: data[i].displayName }; } return parsed; }, formatItem: function(item) { return item.displayName + ' (' + item.mail + ')'; } }; |
The autocomplete plugin will now accept and parse JSON data.
Another thing I wanted to do with this page was to show one value but use another. In this case, I only want to display the user names and email addresses and use the UID without displaying it or putting it into the form field. This is where having each item available as a JSON object comes in very handy.
For this trick, I’ll add a hidden input field and place the UID into that when an item is selected. Here’s my form field
<input type="text" id="user_id" name="user_id"> |
and here’s the JavaScript
// autocomplete options as above jQuery(document).ready(function($) { $('#user_id') .autocomplete('/path/to/ajax/data/source', acOptions) .attr('name', 'display_name') .after('<input type="hidden" name="user_id" id="ac_result">') .result(function(e, data) { $('#ac_result').val(data.uid); }); }); |
The reason I changed the name attribute of the original input and gave it to the new hidden one is so I can still submit the form normally and work with the submitted “user_id” value.
The Autocomplete result function receives the selected item’s data property when an item is selected. With the above configuration, this is the actual JSON object


November 13th, 2008 at 9:00 am
Thanks so much for the info. Really helped on my project.
March 6th, 2009 at 2:49 am
Second what Brett said, thanks a lot – very useful and exactly what I needed
June 5th, 2009 at 2:38 pm
Thanks a lot for this post, this article helped me a lot to implement jquery autocomplete plugin using json response.
June 30th, 2009 at 7:59 am
Hi, I am implemented the above code, it works perfectly fine when data needs to be populated inside text box from server.
But onsubmit I am not able to retrieve the hidden value at server side. It gives me “” empty text.
I have used your exact code with exact same json data.
I would appreciate if you could check or suggest me some ways to get hidden value at server side
August 18th, 2009 at 2:02 am
Hi,
I am using your example to do autocomplete.I am getting result is not a function error. Can you tell me why I am getting this error?
August 20th, 2009 at 11:27 am
@Rapsy: I suggest you make use of Firebug for debugging and data tracking using the “Net” tab.