Raymond Camden's Blog Rss

Autosuggest in Spry

16

Posted in | Posted on 05-18-2007 | 4,887 views

Autosuggest isn't new to Spry 1.5, but I never had a chance to take a look at it till this morning. Surprisingly it wasn't too difficult. Tonight's example will be rather simple, but I'll follow it up with a ColdFusion-based one tomorrow.

At the simplest level, to enable autosuggest you create a simple text field. This is obviously the field that will have the autosuggest tied to it. So I'll start off with that:

view plain print about
1<input type="text" name="search" />

Next I wrap the form field in a div:

view plain print about
1<div id="mySuggest">
2    <input type="text" name="search" />
3</div>

This is simply going to wrap our autosuggest area. So next lets talk data. Where will the autosuggest information come from? Luckily it comes from the datasets we know and love in Spry. So for this demo I'll use the following:

view plain print about
1<script>
2var dsCharacters = new Spry.Data.XMLDataSet("data.xml","people/person");
3</script>

Now that I've got a DataSet, I can use it just like I've used Spry datasets before. So for example:

view plain print about
1<div id="resultsDIV" spry:region="dsCharacters">
2    <span spry:repeat="dsCharacters" spry:suggest="{name}">{name}<br /></span>    
3</div>

This shouldn't look different from previous Spry examples. In this case the repeating of the dataset is used for the autosuggest. I can put whatever HTML I want in there. The value that will be used comes from the spry:suggest.

Basically - thats it! We do need some libraries and a bit of JavaScript. Currently three JavaScript libraries and one CSS file is required:

view plain print about
1<link href="/spryjs/SpryAutoSuggest.css" rel="stylesheet" type="text/css" />
2<script language="JavaScript" type="text/javascript" src="/spryjs/xpath.js"></script>
3<script language="JavaScript" type="text/javascript" src="/spryjs/SpryData.js"></script>
4<script language="JavaScript" type="text/javascript" src="/spryjs/SpryAutoSuggest.js"></script>

Lastly, after the div we add one line of JavaScript:

view plain print about
1<script type="text/javascript">
2var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "dsCharacters","name", {hoverSuggestClass:'highlight'});
3</script>

The first argument points to the great div that wraps the entire autosuggest area. The second argument points to the div that will draw out the suggestions. The third argument is the dataset. Next we tell Spry what field to search. After that we can pass a set of arguments. One of the most important ones is the hoverSuggestClass. Without it the autosuggest is a bit hard to read.

You can see a demo here: http://www.coldfusionjedi.com/sprysuggest/index.html. To see the autosuggest in action, try a few Star Wars names.

Comments

[Add Comment] [Subscribe to Comments]

I saw Ben Forta use auto suggest in CF8 and it was sweet...

<cfinput type="text" name="fruit" autosuggest="apple,banana,lemon,lime,mango,orange,peach,pear">

He even showed some cool auto suggest features on drop downs.

Thanks for the info I actually have a use for it now, can't wait around for CF8.
Auto suggest served up in a data file downloaded to the client works okay with 100 items, but what if you're using a database with 1000's of possible items? I'm surprised Spry doesn't use Ajax or at least provide a method for it. (Or does it?) You might as well use a select box if there isn't a huge number of options.

The Google Toolbar provides a good example of how autosuggest should work. I couldn't imagine it working by downloading a data file with 10 billion items! ;-)
Gary, wait till my next post. You -can- do the following (and I'll be doing this in my next post):

Don't run till N characters typed.
Hit server for every change (this may slow things down, but it does let you get datasets just specific to your text)
"Chad Vader"? I don't remember that *particular* Sith lord :)
/ejt
Is there a way to use spry:suggest with a drop down list?

For example I want the user to be able to start typing the value of the item they want to select and the dropdown will perform a "best match" to find what they are looking for.

Any ideas on using spry to do this?
I should add that I would also like the user to be able to view all selections in the drop down list if they use the pull down arrow.
Tom, I'm not aware of a way to do that. Spry AutoSuggest data comes from a DS. Now - you CAN construct a DS (dataset) by hand. It wouldn't be too terribly hard. As for your second question - you got me there.
Ray I know this can be done in CF8, when Ben came to show off CF8 he did just what Tom is asking.
Right - but that code isn't Spry. :)
link to demo is broken :(
In general, when you see ray.camdenfamily.com, just replace with www.coldfusionjedi.com. In this case, you also have to add index.html. I updated the link.
Is there a limit to how many rows spry's auto suggest can handle? I am having some problems loading more then a couple dozen rows of XML. Has anyone successfully loaded hundreds, or thousands of records with spry's autosuggest?
I don't think there is a built in limit, but there is a practical limit. You don't want to send thousands of rows of data to a user. Period.
Aha - no limit, but watch out for '&' characters, it will crash.
In AjaxCFC there is a way to display one list of data in the suggest box, but actually use a different list of data (if i recall, it does it with a hidden ID), mimicking a select box.

For example, you might have:
1:Ray
2:Bill
3:Steve

If i pick steve, the value of 3 is submitted, NOT "Steve". Is there an easy way to do this in spry - I couldn't find it.
As far as I know - no. But it would be a good thing for them to add.

[Add Comment] [Subscribe to Comments]