<?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; jQuery</title>
	<atom:link href="http://blog.philipbrown.id.au/tag/jquery/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>Awesome Pagination with ZF, Paginator, AjaxContext and the HTML5 History API</title>
		<link>http://blog.philipbrown.id.au/2011/03/awesome-pagination-with-zf-paginator-ajaxcontext-and-the-html5-history-api/</link>
		<comments>http://blog.philipbrown.id.au/2011/03/awesome-pagination-with-zf-paginator-ajaxcontext-and-the-html5-history-api/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 13:09:32 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[AjaxContext]]></category>
		<category><![CDATA[History API]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Pagination]]></category>
		<category><![CDATA[Zend_Paginator]]></category>

		<guid isPermaLink="false">http://blog.philipbrown.id.au/?p=159</guid>
		<description><![CDATA[One of my favourite components in Zend Framework is the AjaxContext action helper. This little beauty allows you to intercept AJAX requests and deliver a different view based on request parameters, all without touching a single line of action code. One scenario where this has always appealed to me is in paginating data. Why reload [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favourite components in Zend Framework is the <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.ajaxcontext">AjaxContext action helper</a>. This little beauty allows you to intercept AJAX requests and deliver a different view based on request parameters, all without touching a single line of action code.</p>
<p>One scenario where this has always appealed to me is in paginating data. Why reload an entire document each time you page through a dataset when you can use AJAX to fetch just the next page.</p>
<p>Now introduce the HTML5 History API, and all of a sudden you&#8217;ve got AJAX data updates with navigable, bookmarkable URLs.</p>
<p>In this article, I&#8217;m going to step through creating a paging controller action that updates the paged data via AJAX and maintains URL location for browsers that support the History API.</p>
<p>If you want to play along at home, all code used in this demo is available on github, with each stage represented by its own branch. Get the code at <a href="https://github.com/philBrown/Awesome-Pagination-Demo">https://github.com/philBrown/Awesome-Pagination-Demo</a> and checkout the &#8220;html-only&#8221; branch</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>philBrown<span style="color: #000000; font-weight: bold;">/</span>Awesome-Pagination-Demo.git
<span style="color: #7a0874; font-weight: bold;">cd</span> Awesome-Pagination-Demo
<span style="color: #c20cb9; font-weight: bold;">git</span> checkout <span style="color: #660033;">-b</span> origin<span style="color: #000000; font-weight: bold;">/</span>html-only</pre></div></div>

<p>To start with, we have a vanilla ZF project with a layout and simple controller action that instantiates a paginator object (using the array adapter for this demo), grabs the &#8220;page&#8221; parameter from the request and assigns the paginator to the view.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$paginator</span> <span style="color: #339933;">=</span> Zend_Paginator<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModels</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$paginator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCurrentPageNumber</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$paginator</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Our view then displays the paginationControl helper</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$paginationControl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginationControl</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginator</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'All'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_partials/paginator.phtml'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div class=&quot;pagination-control&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$paginationControl</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>and a table with our data, looping over each item in the paginator.</p>
<p>What we end up with is something like this</p>
<p><a href="http://blog.philipbrown.id.au/wp-content/uploads/2011/03/Screen-shot-2011-03-10-at-4.13.53-PM.png"><img src="http://blog.philipbrown.id.au/wp-content/uploads/2011/03/Screen-shot-2011-03-10-at-4.13.53-PM-300x146.png" alt="" title="Screen shot 2011-03-10 at 4.13.53 PM" width="300" height="146" class="alignnone size-medium wp-image-162" /></a></p>
<p>Clicking on each paginator link loads an entirely new page with the requested data. Not horrible, but could be better.</p>
<p>Now checkout the &#8220;ajax-loading&#8221; branch</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> checkout <span style="color: #660033;">-b</span> origin<span style="color: #000000; font-weight: bold;">/</span>ajax-loading</pre></div></div>

<p>Our layout now includes the inlineScript helper with a link to jQuery on the Google CDN. The controller now initialises the AjaxContext helper to allow &#8220;html&#8221; AJAX requests for the index action.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">ajaxContext</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addActionContext</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#41;</span>
                               <span style="color: #339933;">-&gt;</span><span style="color: #004000;">initContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The view has also been changed. We&#8217;ve added a link to our pagination JavaScript file (more on this later) and the guts of the view (pagination controls and data) has been moved into a new file <code>index.ajax.phtml</code> which is referenced using the render method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">// index.phtml
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">inlineScript</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'js/paginator.js'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div class=&quot;paged-data-container&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index/index.ajax.phtml'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>The index.ajax.phtml file is the one delivered via the AjaxContext helper when an XMLHTTPRequest is intercepted with the &#8220;html&#8221; format parameter present. Including it in the default template caters for the first page load.</p>
<p>Now on to our JavaScript file. All this file needs to do is catch click events on any of the paginator links and fetch the contents via AJAX, repopulating the paginator controls and data.</p>
<p>In essence, it looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<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;">'.pagination-control'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</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: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> link.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.paged-data-container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> format<span style="color: #339933;">:</span> <span style="color: #3366CC;">'html'</span> <span style="color: #009900;">&#125;</span><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>
            container.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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>We&#8217;re using the &#8220;live&#8221; binding as each AJAX load replaces the paginator controls. The <code>{ format: 'html' }</code> parameter is added to the request to trigger the right context switch in the controller.</p>
<p>So now we have a basic AJAX paginator. The only problem here is that each page loads under the same URL. There&#8217;s also little visual feedback to let the user know the data is changing.</p>
<p>Taking a leaf out of GitHub&#8217;s &#8220;Tree Slider&#8221;, if you checkout the &#8220;ajax-loading-animations&#8221; branch, you&#8217;ll see some nifty left (next) and right (previous) scrolling.</p>
<p>So now we come to the final stage, utilising the HTML5 History API to add stateful URLs for each AJAX data load.</p>
<p>Checkout the master branch to see the code.</p>
<p>The gist of this is to use the <code>history.pushState</code> method to add history records on click and bind an event handler to the &#8220;popstate&#8221; event to catch navigation (back / forward) button presses.</p>
<p>The end result looks like this &#8211; <a href="http://apps.philipbrown.id.au/awesome-pagination/">http://apps.philipbrown.id.au/awesome-pagination/</a></p>
<p>Whilst it&#8217;s certainly not as snazzy as GitHub&#8217;s Tree Slider, it&#8217;s a step in the right direction.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2011/03/awesome-pagination-with-zf-paginator-ajaxcontext-and-the-html5-history-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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! -->
