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.
A Spry widget is a page element containing built-in behaviors and functions that provide a richer user experience by enabling user interaction. These behaviors can include functionality that lets users show or hide content on the page, change the appearance (such as color of the input) in the page, interact with menu items, and much more.
The Spry framework supports a set of re-usable widgets, written in standard HTML, CSS, and JavaScript. You can easily insert these widgets — the code is HTML and JavaScript at its simplest — and then style the widget according to your liking.
Each widget in the Spry framework is associated with unique CSS and JavaScript files. The CSS file contains everything necessary for styling the widget, and the JavaScript file gives the widget its functionality. You must link both of these files to the page on which you want the widget to function and appear styled. Otherwise, the widget won’t have any functionality or styling. For more information, see the appropriate sections about linking associated files in the topics that follow.
The CSS and JavaScript files associated with a given widget are named after the widget, so it’s easy for you to know which files correspond to which widgets. (For example, the files associated with the Auto Suggest widget are called SpryAutoSuggest.css and SpryAutoSuggest.js).
See the Auto Suggest reference file and sample.
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" name="product"/> <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. |
|
moveNextKeyCode | This option allows the user to assign a specific key to go to the next suggestion when the list of suggestions appears. Keyboard code numbers are described here. The value of the option is the keyboard code number. | number | 40 (Down Arrow key code) |
movePrevKeyCode | This option allows the user to assign a specific key to go to the previous suggestion when the list of suggestions appears. Keyboard code numbers are described here. The value of the option is the keyboard code number. | number | 38 (Up Arrow key code) |
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" /> </head>
<div id="mySuggest"></div>
<div id="mySuggest">
<input type="text" name="product" />
</div>
<div id="mySuggest">
<input type="text" name="product"/> <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> <link href="includes/SpryAutoSuggest.css" rel="stylesheet" type="text/css" /> <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" name="product"/> <div id="resultsDIV" spry:region="ds1">
</div>
</div>
<div id="mySuggest">
<input type="text" name="product"/> <div id="resultsDIV" spry:region="ds1"> <ul> <li spry:repeat="ds1">{name}</li> </ul>
</div>
</div>
<div id="mySuggest">
<input type="text" name="product"/> <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" name="product"/> <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.
Note: This chapter details only how to build the Spry Auto Suggest widget with XML Data Set. The steps to use a different Data Sets like JSON or HTML are the same, the things that should be changed are the JS files to be included in your page and the specific Data Set JS creation code.
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" name="product"/> <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" name="product"/> <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>
Making widgets accessible for keyboard navigation is an important part of every widget. Keyboard navigation lets the user control the widget with arrow keys and custom keys.
You can set custom keys for keyboard navigation. Custom keys are set as arguments of the autosuggest constructor script:
<script type="text/javascript"> var theSuggest = new Spry.Widget.AutoSuggest("mySuggest","resultsDIV", "ds1","name",{movePrevKeyCode: 33 /* PgUp key */, moveNextKeyCode: 34 /* PgDn key */}); </script>
Copyright © 2007. Adobe Systems Incorporated. All rights reserved.