Using the MooTools Autocompleter Plugin with ColdFusion
Posted on Thursday, 21st August 2008 in Development by Simon
In a previous post, I demonstrated how to implement Dylan Verheul’s jQuery Autocomplete plugin. Not content with demonstrating one library’s plugin, it is now the turn of MooTools.
MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. It allows you to write powerful, flexible, and cross-browser code with its elegant, well documented, and coherent API.
In this post I will show you how to implement the AutoCompleter plugin by Harald Kirschner. Kirschner’s AutoCompleter plugin script for MooTools provides the functionality for text suggestion and completion. It features different data-sources (local, JSON or XML), a variety of user interactions, custom formatting, multiple selection, animations and much more.
The Goal
The goal of this post will be the same as the jQuery autocomplete post: Allow the user to type a few characters into a standard form text input field and to automatically provide suggestions from which the user can select.
Prerequisites
- The latest copy of MooTools
- A basic understanding of JavaScript and JSON
- A server-side script that can respond to the AJAX request, in our case ColdFusion
Demo
The demo below will show how to interact with a simple ColdFusion script, but I’ll also provide more (advanced) examples in the download.
How It Works
Once the user begins to type into the form text input field, the MooTools auto-complete is activated. After a set character length and time interval (both optional), a list of items is displayed below the input field. The user can select an item with either the arrow keys or mouse.
NB. Clicking back in the input field will repopulate the auto-complete list, if options are available, so that the user can change the selection. Deleting part of the chosen item will also trigger a new selection list.
The Code
There are three parts to this demo:
- The page’s HTML.
- The server-side code to produce the dynamic page (i.e. to load the autocomplete
divwhen the user types something into the input field). - The MooTools JavaScript.
HTML Form
<h1>Example: Country Lookup</h1> <p>Using <abbr title="Asynchronous JavaScript and XML">AJAX</abbr> to interrogate the database.</p> <p>Example data: Australia, Bulgaria, United Kingdom</p> <form name="frmAutoCompleteCountry" id="frmAutoCompleteCountry" action="#" method="post"> <p> <label for="country">Country</label> <input type="text" name="country" id="country" /> </p> </form>
ColdFusion
Below is a simple ColdFusion component that takes a string as an argument. This string is part or all of the country name. The query results are parsed as an array and returned from the function, as JSON, to the MooTools auto-complete function.
<cfcomponent output="false"> <cffunction name="getCountry" access="remote" output="false" returntype="array" returnformat="json"> <cfargument name="country" type="string" required="true" /> <cfset var qryCountry = queryNew('country') /> <cfset var arrCountry = arrayNew(1) /> <cfquery name="qryCountry" datasource="test"> SELECT countryName FROM country WHERE countryName LIKE <cfqueryparam value="%#ARGUMENTS.country#%" cfsqltype="cf_sql_varchar" /> </cfquery> <cfloop query="qryData"> <cfset arrCountry[currentRow] = qryCountry.countryName[currentRow] /> </cfloop> <cfreturn arrCountry /> </cffunction> </cfcomponent>
JavaScript
The JavaScript will attach itself after the DOM is ready — this more or less relates to when the page has loaded in the browser. Each time the text input field, with the ID of country, is changed, the Autocompleter.Ajax.Json event is fired. This makes a call to the ColdFusion component, which returns a JSON object of matched items. This JSON object is interpreted by the plugin and rendered as an HTML un-ordered list.
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript" src="Observer.js"></script>
<script type="text/javascript" src="Autocompleter.js"></script>
<link rel="stylesheet" href="Autocompleter.css" type="text/css" media="screen" />
<script type="text/javascript">
window.addEvent('domready', function() {
new Autocompleter.Ajax.Json(
'country',
'data/Country.cfc?method=getCountry&returnformat=json&country=' + $('country').getProperty('value')
, {
'minLength': 1, // We wait for at least one character
'overflow': true // Overflow for more entries'
});
});
</script>Where to Take it Next
Unobtrusive JavaScript
As with any page that is loaded with JavaScript and AJAX functionality, it should work without JavaScript.
To achieve this with the above tutorial, you will need to replace the MooTools autocomplete functionality with an ‘interim’ page that allows a user to select from a list of items, effectively turning the input field into a simple search interface. Of course, all other form field information would need to be retained between pages.
Download the Code
The example code can be downloaded from the demo page. Included are ColdFusion and PHP examples.
Hey Simon,
I’m playing around with your code and having a few issues. I think there might be a few bugs.
1. in your Country.cfc shouldn’t be ?
2. I can get cf7 and local working but not the cf8 example. Should i be calling the json encode method on the arrCountry to return the array?
Thanks!
Bruce
@bruce regarding point 1, can you explain further?
Regarding point two, the CF8
returnformat="json"attribute should encode the array as json automatically. If you were using CF7, you will need to parse the array as JSON. There is a handy JSON.cfc by Jehiah Czebotar and Thomas Messier to do this for you in the code example I provide and on RIAForge. The CF7 example is not very neat and could easily be refactored, but it provides a good starting point for you to develop upon.I’m sorry, but AJAX does not imply the use of XML ? If so, where is it?