A few days ago I blogged about how to do autosuggest in Spry. In that example, the autosuggest data was based on an XML file loaded as a Spry dataset. While this worked, it was pretty static. What would be more useful is if we could integrate with a database. There are a few options we can pass to the Spry Autosuggest feature to enable this.
Our last example created the widget with this line:
<script type="text/javascript">
var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "dsCharacters","name", {hoverSuggestClass:'highlight'});
</script>
The options (or in this case, option) at the end simply specified the CSS class to use when hovering over the list of suggested items. As I had mentioned, there are more options you can use.
For my next demo, I'm going to add the following options:
- minCharsType: As you can guess, this is the minimum number of characters you need to type before the autosuggest feature fires.
- containsString: Normally autosuggest will only match when the search string and the suggestion start with the same letters. If you set containsString to true, then the match can be anywhere.
- loadFromServer: This is the important one for this demo. It tells Spry that for every attempt at an autosuggest, Spry should reload the dataset from the server. This is what will make our autosuggest more dynamic.
- urlParam: When Spry talks to the server, it will create a URL variable with this name.
So lets focus on the last two as they are them most critical. The first thing I will do is switch my dataset from a simple XML file to a ColdFusion file.
<script>
var dsCharacters = new Spry.Data.XMLDataSet("data.cfm","authors/author");
</script>
I then update my autosuggest widget code to contain the new options I listed above:
<script type="text/javascript">
var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "dsCharacters","NAME",
{hoverSuggestClass:'highlight',minCharsType:3,loadFromServer:true,urlParam:'search',containsString:true});
</script>
Note that I used urlParam:'search'. Spry will add ?search=X to the URL, where X is the value in the text field. In case you are wondering - if my original URL already had a URL param in it, Spry is smart enough to add &search=X instead. (Nice!)
So at this point - let me describe what happens. As you type in the box, when you get to 3 characters, Spry will fire off a HTTP request to get the data. When you change the text, Spry will fire off again. Pretty simple, eh? The ColdFusion behind this is nothing more than a simple SQL statement. For the demo, this is the code I used:
<!---
Ensure we have both the value and at least 3 chars.
--->
<cfif not structKeyExists(url, "search") or len(trim(url.search)) lte 2>
<cfabort>
</cfif>
<cfquery name="getauthors" datasource="camden_blog" maxrows="250">
select distinct name
from tblblogcomments
where name like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#url.search#%" maxlength="255">
</cfquery>
<cfif not structKeyExists(application, "toxml")>
<cfset application.toxml = createObject("component", "toxml")>
</cfif>
<cfset result = application.toxml.queryToXml(getauthors,"authors","author")>
<cfcontent type="text/xml"><cfoutput>#result#</cfoutput>
Note that I check for the existence of the the URL param, and I ensure it is 3 characters long. Why do I bother? Didn't I tell Spry to not fire unless there are 3 characters? Don't forget that your AJAX files need to be secure just like any other file. Someone could easily dig into my code and start trying to hack around with data.cfm.
So to see this in action, go here:
http://ray.camdenfamily.com/sprysuggest/index2.html
Search for 'cam' to get some results. Or just try anything. The data is based on the names of people who post comments here so there should be a very wide variety. View source on the page to see the complete HTML/JS.
And yes - it would work faster if I switched to JSON. (Although I'm not 100% sure if autosuggest works with JSON datasets. If it does not, it is a bug!)
Archived Comments
Good tutorial, Ray. Nice to see how Spry works. While looking at your demo with Firebug I noticed the total download size is 226Kb. Spry is quite big. I found an autocomplete function based on jQuery and the download size is just 76Kb.
It's worth looking at because it has lots of extra features such as storing an ID value in a hidden field, multiple items in a single field...
http://jquery.bassistance.d...
I'm a jQuery fan as well. :)
I'm curious where Spry fits in with things? During the Ajax part of the Scorpio presentation - I was expecting to see Spry at least mentioned but instead Tim said they were using the Yahoo library.
Nice tutorial. You should also be able to return an html table and use the Spry HTMLDataSet libraries:
http://labs.adobe.com/techn...
Any chance you're going to do one of these demos with the CFJSON anytime soon? :) I still haven't been able to get it to work correctly. And I'm anxious to see if it's just me or if something is actually wrong! :)
@Sam - I agree it SHOULD work, but I want to test it first.
@Will - Yes, I can do one with JSON.
Just printed the post to take with me to lunch. :-) Thanks.
BTW - The centered text alignment in your PDF generation is driving me crazy! ;-)
Boggle. When did that happen? I'll take a look at it when I get to updating my blog to the latest version. I'm like 200 revs behind now.
@Gary - One thing about size - the Spry team has not yet shipped 'slim' JS files. That IS planned.
@Jim - Jason Delmore mentioned Spry at cfobjective, but as I don't remember the EXACT mention, I will not say anything more.
@Ray - <a href="http://www.infoaccelerator...." target="_blank">I got the JSON dataset sample we were talking about working.</a>
Shameless cross-post, I know.