<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Phil Brown&#039;s Web Development Blog &#187; JSON</title>
	<atom:link href="http://blog.philipbrown.id.au/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.philipbrown.id.au</link>
	<description></description>
	<lastBuildDate>Mon, 05 Dec 2011 22:22:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>jQuery AJAX Autocomplete with JSON data</title>
		<link>http://blog.philipbrown.id.au/2008/10/jquery-ajax-autocomplete-with-json-data/</link>
		<comments>http://blog.philipbrown.id.au/2008/10/jquery-ajax-autocomplete-with-json-data/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 00:57:58 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=33</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>After sorting through a few, I decided on <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">this one from Jörn Zaefferer</a>. The only problem was, I wanted to use the wonderfully simple <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">Zend Framework AjaxContext Action Helper</a> to produce JSON data as my auto-complete data source. I like JSON as it&#8217;s easy to produce and easy to deal with in disparate systems (such as PHP and JavaScript). Unfortunately, Jörn&#8217;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&#8217;t gotten around to documenting it all yet.</p>
<p>For this post, I&#8217;ll assume my AJAX request is for some user data out of LDAP (or a similar data store) and resembles this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;users&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;uid&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;123&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;displayName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;User 123&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mail&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;123@example.com&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;uid&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;456&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;displayName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;User 456&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mail&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;456@example.com&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;uid&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;789&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;displayName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;User 789&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;mail&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;789@example.com&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The secret to extending the Autocomplete plugin is the ability to overwrite certain core functions, primarily <code>parse</code>. The internal version of this function simply loops over each line of the returned data and &#8220;parses&#8221; it into an array of objects, each containing the following attributes:</p>
<ul>
<li><code>data</code> &#8211; the entire entry</li>
<li><code>value</code> &#8211; the default display value</li>
<li><code>result</code> &#8211; the data to populate the input element on selection</li>
</ul>
<p>You can overwrite this by passing your own <code>parse</code> function as part of the options object to <code>autocomplete</code>. Mine also includes a custom <code>formatItem</code> function for displaying each entry.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> acOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    minChars<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
    max<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span>
    dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'json'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// this parameter is currently unused</span>
    extraParams<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        format<span style="color: #339933;">:</span> <span style="color: #3366CC;">'json'</span> <span style="color: #006600; font-style: italic;">// pass the required context to the Zend Controller</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    parse<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> parsed <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        data <span style="color: #339933;">=</span> data.<span style="color: #660066;">users</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> data.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            parsed<span style="color: #009900;">&#91;</span>parsed.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                data<span style="color: #339933;">:</span> data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                value<span style="color: #339933;">:</span> data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">displayName</span><span style="color: #339933;">,</span>
                result<span style="color: #339933;">:</span> data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">displayName</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> parsed<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    formatItem<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ('</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">mail</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The autocomplete plugin will now accept and parse JSON data.</p>
<p>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.</p>
<p>For this trick, I&#8217;ll add a hidden input field and place the UID into that when an item is selected. Here&#8217;s my form field</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_id&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_id&quot;</span>&gt;</span></pre></div></div>

<p>and here&#8217;s the JavaScript</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// autocomplete options as above</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#user_id'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/ajax/data/source'</span><span style="color: #339933;">,</span> acOptions<span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'display_name'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;input type=&quot;hidden&quot; name=&quot;user_id&quot; id=&quot;ac_result&quot;&gt;'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">result</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ac_result'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">uid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The reason I changed the <code>name</code> 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 &#8220;user_id&#8221; value.</p>
<p>The Autocomplete <code>result</code> function receives the selected item&#8217;s <code>data</code> property when an item is selected. With the above configuration, this is the actual JSON object</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2008/10/jquery-ajax-autocomplete-with-json-data/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
