YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: AutoComplete Control: Subset Matching

# AutoComplete Control: Subset Matching

This example demonstrates AutoComplete's `queryMatchSubset` property. The first instance of AutoComplete has `queryMatchSubset` enabled for maximum cache performance such that as you type, the query is searched within previously cached results. For best results, the DataSource should return a complete set of results when a single letter is queried such that subset matching will also return a complete set of results.

The second AutoComplete instance does not enable `queryMatchSubset` so each typed letter results in a new request to the server.

Note the custom CSS that is needed for stacking AutoComplete instances.

View example in new window.

Note: The flat-file database accessed here has a limited number of terms; for best results, type one-letter at at time and let the AutoComplete instance return — if you type a full, highly-specifc phrase (such as your name) you'll probably get no results from the small dataset.

First AutoComplete instance enables queryMatchSubset:
Second AutoComplete instance does not enable queryMatchSubset:

## Sample Code

Data:

... food and drink 1924 food basic 1075 food carts 1042 ...

CSS:

/* custom styles for multiple stacked instances */ #example1 { z-index:9001; } /* z-index needed on top instances for ie & sf absolute inside relative issue */ #example2 { z-index:9000; } /* z-index needed on top instances for ie & sf absolute inside relative issue */ .autocomplete { padding-bottom:2em;width:40%; }/* set width of widget here*/ label {color:#E76300;}

Markup:

<div id="autocomplete_examples"> <p><strong>Note:</strong> The flat-file database accessed here has a limited number of terms; for best results, type one-letter at at time and let the AutoComplete instance return — if you type a full, highly-specifc phrase (such as your name) you'll probably get no results from the small dataset.</p> <label for="ysearchinput1">First AutoComplete instance enables queryMatchSubset:</label> <div id="example1" class="autocomplete"> <input id="ysearchinput1" type="text"> <div id="ysearchcontainer1"></div> </div> <label for="ysearchinput2">Second AutoComplete instance does not enable queryMatchSubset:</label> <div id="example2" class="autocomplete"> <input id="ysearchinput2" type="text"> <div id="ysearchcontainer2"></div> </div> </div>

JavaScript:

YAHOO.example.QueryMatchSubset = function(){ var myDataSource = new YAHOO.util.XHRDataSource("assets/php/ysearch_flat.php"); myDataSource.responseSchema = { recordDelim: "\n", fieldDelim: "\t" }; myDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT; myDataSource.maxCacheEntries = 60; // First AutoComplete var myAutoComp1 = new YAHOO.widget.AutoComplete("ysearchinput1","ysearchcontainer1", myDataSource); myAutoComp1.queryMatchSubset = true; // Second AutoComplete var myAutoComp2 = new YAHOO.widget.AutoComplete("ysearchinput2","ysearchcontainer2", myDataSource); return { oDS: myDataSource, oAC1: myAutoComp1, oAC2: myAutoComp2 } }();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### AutoComplete Control Examples:

### More AutoComplete Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings