The Auto-suggest widget is a form text field used as a search field. As the user types in the field, a list of matching hints will list underneath the text field. Users can select from the hinted list. The Auto-suggest widget is different than other Spry widgets because it is a data-widget hybrid. This means that is has the markup structure of a widget and it instantiated with a script below the markup, but it depends on a data set for the suggestion data.
The Auto-suggest widget has a container DIV, an input field and another DIV that contains the suggest results. The suggestion results can be formatted however you wish. The widget, as always, has a constructor script that follows the widget markup. This constructor has 4 required values and a set of optional values.
<div id="productSampleDiv" class="container">
<input type="text" id="productFilterDiv" />
<div id="productMenuDiv" spry:region="dsProducts3">
<span spry:repeat="dsProducts3" spry:suggest="{name}">{name}<br /></span>
</div>
</div>
<script type="text/javascript">
var ac3 = new Spry.Widget.AutoSuggest("productSampleDiv", "productMenuDiv", "dsProducts3", "name", {hoverSuggestClass: 'hover', minCharsType: 0, containsString: true, maxListItems: 0});
</script>
As with all widgets, the actual markup doesn't matter, as long as it follows the rules of HTML (ie. <p> tags cannot contain other block elements).
Breaking down the constructor:
There are a series of optional attributes for the constructor:
| Option | Definition | Default | Values |
|---|---|---|---|
| containsString | By default, the widget returns results that "starts" with the typed string. Setting containString to 'true' will return results where the typed string is anywhere within the result. | false |
|
| minCharsType | This determines how many characters have to be typed before results are listed. This is helpful in improving performance, esp. if retrieving results directly from the server | number | |
| maxListItems | Determines the maximum number of items in the list | number | |
| hoverSuggestClass | This attribute takes a CSS class that will be applied while hovering over the results list. | CSS class name | |
| loadFromServer | This attribute will fetch results from the server directly, rather than from the cached data set. This will trump the default data set caching setting and always fetch the results from the server. | false |
|
| urlParam | This is the URL parameter name to which the search field value is attached. i.e. www.adobe.com?urlParam=searchValue |
required if loadFromServer is true. |
Note that the auto suggest works by filtering the data set. Initially, the data set filters out everything. As you type, the filter is loosened to allow in the expected results. This means that if you have other Spry regions hooked to the same data set as the auto suggest widget, those regions will not show content when the page loads. You may want to dedicate a data set specifically for the auto suggest.
<head>
<script language="JavaScript" type="text/javascript" src="includes/xpath.js"></script> <script language="JavaScript" type="text/javascript" src="includes/SpryData.js"></script> <script language="JavaScript" type="text/javascript" src="includes/SpryAutoSuggest.js"></script> <link href="includes/SpryAutoSuggest.css" rel="stylesheet" type="text/css" /> <link </head>
<div id="mySuggest"></div>
<div id="mySuggest">
<input type="text" />
</div>
<div id="mySuggest">
<input type="text" /> <div id="resultsDIV">
</div>
</div>
<head>
<script language="JavaScript" type="text/javascript" src="includes/xpath.js"></script> <script language="JavaScript" type="text/javascript" src="includes/SpryData.js"></script> <script language="JavaScript" type="text/javascript" src="includes/SpryAutoSuggest.js"></script>
<script language="JavaScript" type="text/javascript"> var ds1 = new Spry.Data.XMLDataSet("data/products.xml","products/product"); </script>
</head>
<div id="mySuggest">
<input type="text" /> <div id="resultsDIV" spry:region="ds1">
</div>
</div>
<div id="mySuggest">
<input type="text" /> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1">{name}</li> </ul>
</div>
</div>
<div id="mySuggest">
<input type="text" /> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1" spry:suggest="{name}">{name}</li> </ul>
</div>
</div>
<div id="mySuggest">The values are ("theContainerID","theResultsContainerID","theDataSetName","theResultsDataColumn")
<input type="text" /> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1" spry:suggest="{name}">{name}</li> </ul>
</div>
</div> <script type="text/javascript"> var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "ds1","name"); </script>
That's it. Preview the page and typing in the field should show results displayed below the field.
If you want to add some optional functionality, add the values to the constructor. The options added below will force the suggest to start after the 3rd character is typed and will also change the search parameter from "starts with" to "contains". Options are contained within {}.
<div id="mySuggest">
<input type="text" /> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1" spry:suggest="{name}">{name}</li> </ul>
</div>
</div> <script type="text/javascript"> var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "ds1","name",{containsString:true,minCharsType:3}); </script>
Or with more options:
<div id="mySuggest">
<input type="text"> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1" spry:suggest="{name}">{name}</li> </ul>
</div>
</div> <script type="text/javascript"> var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "ds1", "name",{containsString:true, minCharsType:3,maxListItems:10, containsString:true, hoverSuggestClass:mySuggest}); </script>
Copyright © 2007. Adobe Systems Incorporated. All rights reserved.