This example uses a DS_JSFunction DataSource pointing to a JavaScript function that returns data as an array of strings. Since the data for this example is already loaded into memory, queries should be very fast to return data, and since there is no server load concern, the AutoComplete instance can be configured to have a query delay of zero seconds.
In this example, the AutoComplete instance is able to keep its container
always open by customizing the appropriate CSS styles and enabling the
alwaysShowContainer
property. We hook into the custom events
containerExpandEvent
and containerCollapseEvent
and calling the setHeader()
, setBody()
, and
setFooter()
methods to dynamically update the contents of the open
container. Finally, the AutoComplete's formatResults()
method
has been customized to display multiple data fields in the container.
CSS:
1 | /* custom styles for scrolling container */ |
2 | #statesautocomplete { |
3 | width:15em; /* set width of widget here*/ |
4 | height:12em; /* define height for container to appear inline */ |
5 | } |
6 | #statescontainer .yui-ac-content { |
7 | max-height:11em;overflow:auto;overflow-x:hidden; /* scrolling */ |
8 | _height:11em; /* ie6 */ |
9 | } |
view plain | print | ? |
Markup:
1 | <h3>Filter the US states:</h3> |
2 | <div id="statesautocomplete"> |
3 | <input id="statesinput" type="text"> |
4 | <div id="statescontainer"></div> |
5 | </div> |
view plain | print | ? |
JavaScript:
1 | function getStates(sQuery) { |
2 | aResults = []; |
3 | if (sQuery && sQuery.length > 0) { |
4 | var charKey = sQuery.substring(0, 1).toLowerCase(); |
5 | var oResponse = dataset[charKey]; |
6 | if (oResponse) { |
7 | for (var i = oResponse.length - 1; i >= 0; i--) { |
8 | var sKey = oResponse[i].STATE; |
9 | var sKeyIndex = encodeURI(sKey.toLowerCase()).indexOf(sQuery.toLowerCase()); |
10 | if (sKeyIndex === 0) { |
11 | aResults.unshift([sKey, oResponse[i].ABBR]); |
12 | } |
13 | } |
14 | return aResults; |
15 | } |
16 | } else { |
17 | for (var letter in dataset) { |
18 | var oResponse = dataset[letter]; |
19 | for(var i = 0; i < oResponse.length; i++) { |
20 | aResults.push([oResponse[i].STATE, oResponse[i].ABBR]); |
21 | } |
22 | } |
23 | return aResults; |
24 | } |
25 | } |
26 | |
27 | var dataset = { |
28 | 'a' : [{"STATE" : "Alabama", "ABBR" : "AL"}, |
29 | {"STATE" : "Alaska", "ABBR" : "AK"}, |
30 | {"STATE" : "Arizona", "ABBR" : "AZ"}, |
31 | {"STATE" : "Arkansas", "ABBR" : "AR"}], |
32 | 'b' : [ ], |
33 | 'c' : [{"STATE" : "California", "ABBR" : "CA"}, |
34 | {"STATE" : "Colorado", "ABBR" : "CO"}, |
35 | {"STATE" : "Connecticut", "ABBR" : "CT"}], |
36 | ... // Entire dataset not shown |
37 | }; |
38 | |
39 | YAHOO.example.ACJSFunction = new function(){ |
40 | // Instantiate JS Function DataSource |
41 | this.oACDS = new YAHOO.widget.DS_JSFunction(getStates); |
42 | this.oACDS.maxCacheEntries = 0; |
43 | |
44 | // Instantiate AutoComplete |
45 | this.oAutoComp = new YAHOO.widget.AutoComplete('statesinput','statescontainer', this.oACDS); |
46 | this.oAutoComp.alwaysShowContainer = true; |
47 | this.oAutoComp.minQueryLength = 0; |
48 | this.oAutoComp.maxResultsDisplayed = 50; |
49 | this.oAutoComp.formatResult = function(oResultItem, sQuery) { |
50 | var sMarkup = oResultItem[0] + " (" + oResultItem[1] + ")"; |
51 | return (sMarkup); |
52 | }; |
53 | |
54 | // Show custom message if no results found |
55 | this.myOnDataReturn = function(sType, aArgs) { |
56 | var oAutoComp = aArgs[0]; |
57 | var sQuery = aArgs[1]; |
58 | var aResults = aArgs[2]; |
59 | |
60 | if(aResults.length == 0) { |
61 | oAutoComp.setBody("<div id=\"statescontainerdefault\">No matching results</div>"); |
62 | } |
63 | }; |
64 | this.oAutoComp.dataReturnEvent.subscribe(this.myOnDataReturn); |
65 | |
66 | // Preload content in the container |
67 | this.oAutoComp.sendQuery(""); |
68 | }; |
view plain | print | ? |
INFO1915ms (+438) 12:00:33 PM:LogReader instance0
LogReader initialized
INFO1477ms (+308) 12:00:32 PM:AutoComplete instance0 statesinput
Container expanded
INFO1169ms (+0) 12:00:32 PM:AutoComplete instance0 statesinput
Container populated with list items
INFO1169ms (+12) 12:00:32 PM:AutoComplete instance0 statesinput
Arrowed to first item
INFO1157ms (+1) 12:00:32 PM:DataSource instance0
Results returned for query "": [[Alabama, AL], [Alaska, AK], [Arizona, AZ], [Arkansas, AR], [California, CA], [Colorado, CO], [Connecticut, CT], [Delaware, DE], [Florida, FL], [Georgia, GA], [Hawaii, HI], [Idaho, ID], [Illinois, IL], [Indiana, IN], [Iowa, IA], [Kansas, KS], [Kentucky, KY], [Louisiana, LA], [Maine, ME], [Maryland, MD], [Massachusetts, MA], [Michigan, MI], [Minnesota, MN], [Mississippi, MS], [Missouri, MO], [Montana, MT], [Nebraska, NE], [Nevada, NV], [New Hampshire, NH], [New Jersey, NJ], [New Mexico, NM], [New York, NY], [North Dakota, ND], [North Carolina, NC], [Ohio, OH], [Oklahoma, OK], [Oregon, OR], [Pennsylvania, PA], [Rhode Island, RI], [South Carolina, SC], [South Dakota, SD], [Tennessee, TN], [Texas, TX], [Utah, UT], [Vermont, VT], [Virginia, VA], [Washington, WA], [West Virginia, WV], [Wisconsin, WI], [Wyoming, WY]]
INFO1156ms (+0) 12:00:32 PM:DataSource instance0
Query received "
INFO1156ms (+0) 12:00:32 PM:AutoComplete instance0 statesinput
Sending query ""
INFO1156ms (+2) 12:00:32 PM:AutoComplete instance0 statesinput
AutoComplete initialized
INFO1154ms (+1153) 12:00:32 PM:DataSource instance0
JS Function DataSource initialized
INFO1ms (+1) 12:00:31 PM:global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings