<?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>Smartkey - Java Software Consultancy &#187; AJAX</title>
	<atom:link href="http://blog.smartkey.co.uk/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartkey.co.uk</link>
	<description></description>
	<lastBuildDate>Tue, 13 Dec 2011 15:03:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Formatting the Grails RichUI AutoComplete component</title>
		<link>http://blog.smartkey.co.uk/2009/10/formatting-the-grails-richui-autocomplete-component/</link>
		<comments>http://blog.smartkey.co.uk/2009/10/formatting-the-grails-richui-autocomplete-component/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 20:04:20 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails programming]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[AutoComplete]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[RichUI]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=321</guid>
		<description><![CDATA[The RichUI component plug-in for Grails is great! It provides a really simple means of accessing rich Ajax functionality in a Grails application. I used this plug-in recently to implement an AutoComplete text field but needed to apply some formatting to the results. This post explains how I did this.]]></description>
			<content:encoded><![CDATA[<p>The RichUI component plug-in for Grails is great! It provides a really simple means of accessing rich Ajax functionality in a Grails application. I used this plug-in recently to implement an AutoComplete text field but needed to apply some formatting to the results. This post explains how I did this.</p>
<p>I&#8217;m not going to explain anything that&#8217;s <a href="http://grails.org/RichUI+Plugin#AutoComplete" target="_blank">already documented</a> for RichUI&#8217;s AutoComplete component. Basic formatting customisation is already documented, so if you&#8217;re looking to change the colours used or the width of the output then take a look there.</p>
<p>The code I was working on searched a multi-lingual database for words, the initial output being displayed looked like this:</p>
<p><img class="aligncenter size-full wp-image-331" title="plain autocomplete" src="http://blog.smartkey.co.uk/wp-content/uploads/2009/10/Picture-5.png" alt="plain autocomplete" width="212" height="249" /></p>
<p>in addition to this, I wanted to display locale flags for the languages alongside the results:</p>
<p><img class="aligncenter size-full wp-image-332" title="Picture 4" src="http://blog.smartkey.co.uk/wp-content/uploads/2009/10/Picture-4.png" alt="Picture 4" width="211" height="251" /></p>
<p>This change would involve modifying, not just the styles, but the actual HTML  that was being used to render the results by the AutoComplete component.</p>
<p>The RichUI plug-in is based on the <a href="http://developer.yahoo.com/yui/2/" target="_blank">Yahoo User Interface Library</a> or YUI for short. The AutoComplete component in RichUI, is just a Grails tag that creates a YUI AutoComplete component; the documentation for which explains how the HTML for the results list <a href="http://developer.yahoo.com/yui/autocomplete/#formatting">can be customised</a>.</p>
<p>In short, to customise the HTML, there is a method named &#8216;<strong>formatResult</strong>&#8216; on the AutoComplete JavaScript object that gets called to render each of the results. By default, this method just returns the plain text of the result. However, it can be overridden to provide custom mark-up.</p>
<p>The documentation for the RichUI AutoComplete tag explains how a JavaScript function can be registered to handle an &#8216;onItemSelect&#8217; event, but there is no explanation of how to override any other methods. After a little investigation of the RichUI plug-in code I found that the renderer that produces the HTML for the tag does in fact and associate any attributes declared on the tag as properties/methods on the YUI component. Note that this is not a documented feature, but one I would hope would not be changed in future versions of this plug-in.</p>
<p>So by declaring the RichUI tag like this:</p>
<pre class="brush: xml">
&lt;richui:autoComplete
    name=&quot;keywords&quot;
    action=&quot;${createLinkTo(&#039;dir&#039;: &#039;word/findAjax&#039;)}&quot;
    maxResultsDisplayed=&quot;20&quot;
    formatResult=&quot;myFormatResult&quot;/&gt;
</pre>
<p>the renderer will generate JavaScript like this; setting not only the formatResult event handler, but the maxResultsDisplayed property too:</p>
<pre class="brush: html">
autoComplete = new YAHOO.widget.AutoComplete(&#039;keywords&#039;,&#039;...&#039;, dataSource);
autoComplete.maxResultsDisplayed = 20;
autoComplete.formatResult = myFormatResult;
</pre>
<p>it&#8217;s then just a matter of implementing a JavaScript function that generates the HTML for each of the items:</p>
<pre class="brush: javascript">
function formatResult(oResultData, sQuery, sResultMatch) {
  var sMarkup;
  var locale = oResultData[1];
  var imageUri = &quot;${createLinkTo(dir:&#039;/images/flags&#039;)}/&quot; + locale + &quot;.png&quot;;
  if (sResultMatch) {
    sMarkup = &quot;&lt;img src=&quot; + imageUri + &quot;/&gt; &quot; + sResultMatch
  } else {
    sMarkup = &quot;&quot;
  }
  return sMarkup;
}
</pre>
<p>Finally, it&#8217;s worth noting here that the data returned from the search controller is available in the &#8216;oResultData&#8217; array that gets passed in to this function. In this example, you can see that the locale for the word is being accessed and used to generate the image URI.</p>
<p>To achieve this, the response is being generated in the search controller like this:</p>
<pre class="brush: javascript">
render(contentType: &quot;text/xml&quot;) {
  results() {
    words.each { w -&gt;
      result() {
        name(w.stem)
        id(w.locale)
      }
    }
  }
}
</pre>
<p>the name and id elements that are being produced in the XML are those specified in the schema that RichUI uses. The name element is the value that is usually displayed by the component, the ID is a supplemental value that is available to the RichUI &#8216;onItemSelect&#8217; JavaScript event handler. it should be possible to extend this but I&#8217;ll leave that challenge for another day&#8230;</p>
<p>If you&#8217;d like to see this code in action take a look at the <a href="http://www.languagespider.com" target="_blank">Language Spider</a> project web-site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2009/10/formatting-the-grails-richui-autocomplete-component/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

