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.
Implementers who are working with data from third-party sources, user input data, or otherwise untrustworthy sources should be sure to read the Security Considerations section of the AutoComplete user guide.
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.
Data:
1 | ... |
2 | food and drink 1924 |
3 | food basic 1075 |
4 | food carts 1042 |
5 | ... |
view plain | print | ? |
CSS:
1 | /* custom styles for multiple stacked instances */ |
2 | #example1 { z-index:9001; } /* z-index needed on top instances for ie & sf absolute inside relative issue */ |
3 | #example2 { z-index:9000; } /* z-index needed on top instances for ie & sf absolute inside relative issue */ |
4 | .autocomplete { padding-bottom:2em;width:40%; }/* set width of widget here*/ |
5 | label {color:#E76300;} |
view plain | print | ? |
Markup:
1 | <div id="autocomplete_examples"> |
2 | <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> |
3 | <label for="ysearchinput1">First AutoComplete instance enables queryMatchSubset:</label> |
4 | <div id="example1" class="autocomplete"> |
5 | <input id="ysearchinput1" type="text"> |
6 | <div id="ysearchcontainer1"></div> |
7 | </div> |
8 | <label for="ysearchinput2">Second AutoComplete instance does not enable queryMatchSubset:</label> |
9 | <div id="example2" class="autocomplete"> |
10 | <input id="ysearchinput2" type="text"> |
11 | <div id="ysearchcontainer2"></div> |
12 | </div> |
13 | </div> |
view plain | print | ? |
JavaScript:
1 | YAHOO.example.QueryMatchSubset = function(){ |
2 | var myDataSource = new YAHOO.util.XHRDataSource("assets/php/ysearch_flat.php"); |
3 | myDataSource.responseSchema = { |
4 | recordDelim: "\n", |
5 | fieldDelim: "\t" |
6 | }; |
7 | myDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT; |
8 | myDataSource.maxCacheEntries = 60; |
9 | |
10 | // First AutoComplete |
11 | var myAutoComp1 = new YAHOO.widget.AutoComplete("ysearchinput1","ysearchcontainer1", myDataSource); |
12 | myAutoComp1.queryMatchSubset = true; |
13 | |
14 | // Second AutoComplete |
15 | var myAutoComp2 = new YAHOO.widget.AutoComplete("ysearchinput2","ysearchcontainer2", myDataSource); |
16 | |
17 | return { |
18 | oDS: myDataSource, |
19 | oAC1: myAutoComp1, |
20 | oAC2: myAutoComp2 |
21 | } |
22 | }(); |
view plain | print | ? |
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.
INFO 422ms (+1) 7:32:51 AM:
LogReader instance0
LogReader initialized
INFO 421ms (+1) 7:32:51 AM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 420ms (+0) 7:32:51 AM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 420ms (+53) 7:32:51 AM:
Get
_next: q0, loaded: undefined
INFO 367ms (+0) 7:32:51 AM:
AutoComplete instance1 ysearchinput2
AutoComplete initialized
INFO 367ms (+2) 7:32:51 AM:
AutoComplete instance0 ysearchinput1
AutoComplete initialized
INFO 365ms (+365) 7:32:51 AM:
DataSource instance0
DataSource initialized
INFO 0ms (+0) 7:32:51 AM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings