YUI Library Home

YUI Library Examples: AutoComplete Control: Combobox, with CSS for Multiple Stacked Instances

AutoComplete Control: Combobox, with CSS for Multiple Stacked Instances

This "combo box" AutoComplete implementation allows the user to pick an item from a set list or enter a custom value directly into the input field. Please note the following custom CSS:

  • z-index has been applied to support multiple vertically stacked AutoComplete instances
  • the input field and container have been styled to support a button inline to the right

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.

Sample Code

Data:

1YAHOO.example.Data.menu: { 
2    breakfasts: [ 
3        "donuts"
4        "omelette"
5        "pancakes"
6        "yogurt" 
7    ], 
8    lunches: [ 
9        "burrito"
10        "hamburger"
11        "salad"
12        "turkey sandwich" 
13    ], 
14    dinners: [ 
15        "meatloaf"
16        "spaghetti"
17        "pot roast"
18        "roast chicken"
19        "steak" 
20    ] 
21}; 
view plain | print | ?

CSS:

1/* custom styles for inline instances */ 
2.yui-skin-sam .yui-ac-input { position:static;width:20emvertical-align:middle;} 
3.yui-skin-sam .yui-ac-container { width:20em;left:0px;} 
4 
5/* needed for stacked instances for ie & sf z-index bug of absolute inside relative els */ 
6#bAutoComplete { z-index:9001}  
7#lAutoComplete { z-index:9000} 
8 
9/* buttons */ 
10.yui-ac .yui-button {vertical-align:middle;} 
11.yui-ac .yui-button button {background: url(../autocomplete/assets/img/ac-arrow-rt.png) center center no-repeat } 
12.yui-ac .open .yui-button button {background: url(../autocomplete/assets/img/ac-arrow-dn.png) center center no-repeat
view plain | print | ?

Markup:

1<label for="bInput">What would you like for breakfast?</label> 
2<div id="bAutoComplete"
3    <input id="bInput" type="text"<span id="toggleB"></span> 
4    <div id="bContainer"></div> 
5</div> 
6 
7<label for="lInput">What would you like for lunch?</label> 
8<div id="lAutoComplete"
9    <input id="lInput" type="text"<span id="toggleL"></span> 
10    <div id="lContainer"></div> 
11</div> 
12 
13<label for="dInput">What would you like for dinner?</label> 
14<div id="dAutoComplete"
15    <input id="dInput" type="text"<span id="toggleD"></span> 
16    <div id="dContainer"></div> 
17</div> 
view plain | print | ?

JavaScript:

1YAHOO.example.Combobox = function() { 
2    // Instantiate DataSources 
3    var bDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.breakfasts); 
4    var lDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.lunches); 
5    var dDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.dinners); 
6 
7    // Instantiate AutoCompletes 
8    var oConfigs = { 
9        prehighlightClassName: "yui-ac-prehighlight"
10        useShadow: true
11        queryDelay: 0, 
12        minQueryLength: 0, 
13        animVert: .01 
14    } 
15    var bAC = new YAHOO.widget.AutoComplete("bInput""bContainer", bDS, oConfigs); 
16    var lAC = new YAHOO.widget.AutoComplete("lInput""lContainer", lDS, oConfigs); 
17    var dAC = new YAHOO.widget.AutoComplete("dInput""dContainer", dDS, oConfigs); 
18     
19    // Breakfast combobox 
20    var bToggler = YAHOO.util.Dom.get("toggleB"); 
21    var oPushButtonB = new YAHOO.widget.Button({container:bToggler}); 
22    var toggleB = function(e) { 
23        //YAHOO.util.Event.stopEvent(e); 
24        if(!YAHOO.util.Dom.hasClass(bToggler, "open")) { 
25            YAHOO.util.Dom.addClass(bToggler, "open"
26        } 
27         
28        // Is open 
29        if(bAC.isContainerOpen()) { 
30            bAC.collapseContainer(); 
31        } 
32        // Is closed 
33        else { 
34            bAC.getInputEl().focus(); // Needed to keep widget active 
35            setTimeout(function() { // For IE 
36                bAC.sendQuery(""); 
37            },0); 
38        } 
39    } 
40    oPushButtonB.on("click", toggleB); 
41    bAC.containerCollapseEvent.subscribe(function(){YAHOO.util.Dom.removeClass(bToggler, "open")}); 
42     
43    // Lunch combobox 
44    var lToggler = YAHOO.util.Dom.get("toggleL"); 
45    var oPushButtonL = new YAHOO.widget.Button({container:lToggler}); 
46    var toggleL = function(e) { 
47        //YAHOO.util.Event.stopEvent(e); 
48        if(!YAHOO.util.Dom.hasClass(lToggler, "open")) { 
49            YAHOO.util.Dom.addClass(lToggler, "open"
50        } 
51         
52        // Is open 
53        if(lAC.isContainerOpen()) { 
54            lAC.collapseContainer(); 
55        } 
56        // Is closed 
57        else { 
58            lAC.getInputEl().focus(); // Needed to keep widget active 
59            setTimeout(function() { // For IE 
60                lAC.sendQuery(""); 
61            },0); 
62        } 
63    } 
64    oPushButtonL.on("click", toggleL); 
65    lAC.containerCollapseEvent.subscribe(function(){YAHOO.util.Dom.removeClass(lToggler, "open")}); 
66 
67    // Dinner combobox 
68    var dToggler = YAHOO.util.Dom.get("toggleD"); 
69    var oPushButtonD = new YAHOO.widget.Button({container:dToggler}); 
70    var toggleD = function(e) { 
71        //YAHOO.util.Event.stopEvent(e); 
72        if(!YAHOO.util.Dom.hasClass(dToggler, "open")) { 
73            YAHOO.util.Dom.addClass(dToggler, "open"
74        } 
75         
76        // Is open 
77        if(dAC.isContainerOpen()) { 
78            dAC.collapseContainer(); 
79        } 
80        // Is closed 
81        else { 
82            dAC.getInputEl().focus(); // Needed to keep widget active 
83            setTimeout(function() { // For IE 
84                dAC.sendQuery(""); 
85            },0); 
86        } 
87    } 
88    oPushButtonD.on("click", toggleD); 
89    dAC.containerCollapseEvent.subscribe(function(){YAHOO.util.Dom.removeClass(dToggler, "open")}); 
90 
91    return { 
92        bDS: bDS, 
93        bAC: bAC, 
94        lDS: lDS, 
95        lAC: lAC, 
96        dDS: dDS, 
97        dAC: dAC 
98    }; 
99}(); 
view plain | print | ?

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:

Logger Console

INFO 292ms (+153) 3:10:11 PM:

LogReader instance0

LogReader initialized

INFO 139ms (+1) 3:10:11 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 138ms (+0) 3:10:11 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 138ms (+30) 3:10:11 PM:

Get

_next: q0, loaded: undefined

INFO 108ms (+0) 3:10:10 PM:

AutoComplete instance2 dInput

AutoComplete initialized

INFO 108ms (+0) 3:10:10 PM:

AutoComplete instance1 lInput

AutoComplete initialized

INFO 108ms (+2) 3:10:10 PM:

AutoComplete instance0 bInput

AutoComplete initialized

INFO 106ms (+1) 3:10:10 PM:

DataSource instance2

DataSource initialized

INFO 105ms (+0) 3:10:10 PM:

DataSource instance1

DataSource initialized

INFO 105ms (+105) 3:10:10 PM:

DataSource instance0

DataSource initialized

INFO 0ms (+0) 3:10:10 PM:

global

Logger initialized

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

Reload with logging
and debugging disabled.

More AutoComplete Control Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

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