Contents |
As of jQuery UI 1.7, the autocomplete plugin is not part of the stable release of jQuery UI.
Development plans for this functionality and the inclusion of this plugin's functions can be read about here; http://wiki.jqueryui.com/Autocomplete
The current repository version of this plugin can be found here: http://jquery-ui.googlecode.com/svn/branches/dev/autocomplete/
The original plugin is still available for download, and while not being updated for some time also works with current jQuery versions, as well as 1.2.6: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
Autocomplete an input field to enable users quickly finding and selecting some value, leveraging searching and filtering.
By giving an autocompleted field focus or entering something into it, the plugin starts searching for matching entries and displays a list of values to choose from. By entering more characters, the user can filter down the list to better matches.
This can be used to enter previous selected values, eg. for tags, to complete an address, eg. enter a city name and get the zip code, or maybe enter email addresses from an addressbook.
Both local and remote data can be used: Local is good for small datasets (like an addressbook with 50 entries), remote is necessary for big datasets, like a database with hundreds or millions of entries to select from.
When specifying the data, write it this way:
$("#example").autocomplete( {data:data} );
The example shown here will not work with recent versions of jQueryUI
Autocompletion of input elements is a very helpful tool to guide users in entering the right value. That value can come from a (big) list of possible values or it may consist of values the user has entered before. This plugin makes it easy for developers to setup autocomplete for text inputs and textareas.
First step in setting up autocomplete is to decide what kind of data is provided. To autocomplete a few dozen possible values, it is a good idea to provide local data. The data is loaded only once and then cached, providing a very responsive interface. If the data can consist of hundreds, thousands or even millions of rows, the searching and filtering must be done where performance won't suffer, in most cases a relational database (eg. MySQL, SQL Server, or cached in server memory). The plugin will request the rows for the entered value, displaying only a subset of all possible values.
For local autocomplete, just pass the data as an array to the autocomplete plugin.
For remote autocomplete, specify a URL to the resource providing the data. The plugin then requests data with a "q" parameter containing the current search value. If you are using the default settings your page should output the filtered results simply as plain text separated by line breaks (not HTML, not JSON). If you need to do something more advanced you can define item formatting functions that will take each raw data line and return the text you want to display.
You will also need to set up some styles. You can do this either using Themeroller or by adding your own stylesheet. Below are the default styles. You can copy these into your own stylesheet.
.ui-autocomplete-results {
padding: 0px;
border: 1px solid black;
background-color: white;
overflow: hidden;
z-index: 99999;
}
.ui-autocomplete-results ul {
width: 100%;
list-style-position: outside;
list-style: none;
padding: 0;
margin: 0;
}
.ui-autocomplete-results li {
margin: 0px;
padding: 2px 5px;
cursor: default;
display: block;
/*
if width will be 100% horizontal scrollbar will apear
when scroll mode will be used
*/
/*width: 100%;*/
font: menu;
font-size: 12px;
/*
it is very important, if line-height not setted or setted
in relative units scroll will be broken in firefox
*/
line-height: 16px;
overflow: hidden;
}
.ui-autocomplete-loading {
background: white url('indicator.gif') right center no-repeat;
}
.ui-autocomplete-odd {
background-color: #eee;
}
.ui-autocomplete-over {
background-color: #0A246A;
color: white;
}
Note: in previous versions of jQuery UI, the autocomplete classes were prefixed by ".ac_" instead of ".ui-autocomplete-".