3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.ComboBox
9 * @extends Ext.form.TriggerField
10 * <p>A combobox control with support for autocomplete, remote-loading, paging and many other features.</p>
11 * <p>A ComboBox works in a similar manner to a traditional HTML <select> field. The difference is
12 * that to submit the {@link #valueField}, you must specify a {@link #hiddenName} to create a hidden input
13 * field to hold the value of the valueField. The <i>{@link #displayField}</i> is shown in the text field
14 * which is named according to the {@link #name}.</p>
15 * <p><b><u>Events</u></b></p>
16 * <p>To do something when something in ComboBox is selected, configure the select event:<pre><code>
17 var cb = new Ext.form.ComboBox({
18 // all of your config options
21 'select': yourFunction
25 // Alternatively, you can assign events after the object is created:
26 var cb = new Ext.form.ComboBox(yourOptions);
27 cb.on('select', yourFunction, yourScope);
30 * <p><b><u>ComboBox in Grid</u></b></p>
31 * <p>If using a ComboBox in an {@link Ext.grid.EditorGridPanel Editor Grid} a {@link Ext.grid.Column#renderer renderer}
32 * will be needed to show the displayField when the editor is not active. Set up the renderer manually, or implement
33 * a reusable render, for example:<pre><code>
34 // create reusable renderer
35 Ext.util.Format.comboRenderer = function(combo){
36 return function(value){
37 var record = combo.findRecord(combo.{@link #valueField}, value);
38 return record ? record.get(combo.{@link #displayField}) : combo.{@link #valueNotFoundText};
42 // create the combo instance
43 var combo = new Ext.form.ComboBox({
44 {@link #typeAhead}: true,
45 {@link #triggerAction}: 'all',
46 {@link #lazyRender}:true,
47 {@link #mode}: 'local',
48 {@link #store}: new Ext.data.ArrayStore({
54 data: [[1, 'item1'], [2, 'item2']]
56 {@link #valueField}: 'myId',
57 {@link #displayField}: 'displayText'
60 // snippet of column model used within grid
61 var cm = new Ext.grid.ColumnModel([{
64 header: "Some Header",
65 dataIndex: 'whatever',
67 editor: combo, // specify reference to combo instance
68 renderer: Ext.util.Format.comboRenderer(combo) // pass combo instance to reusable renderer
74 * <p><b><u>Filtering</u></b></p>
75 * <p>A ComboBox {@link #doQuery uses filtering itself}, for information about filtering the ComboBox
76 * store manually see <tt>{@link #lastQuery}</tt>.</p>
78 * Create a new ComboBox.
79 * @param {Object} config Configuration options
82 Ext.form.ComboBox = Ext.extend(Ext.form.TriggerField, {
84 * @cfg {Mixed} transform The id, DOM node or element of an existing HTML SELECT to convert to a ComboBox.
85 * Note that if you specify this and the combo is going to be in an {@link Ext.form.BasicForm} or
86 * {@link Ext.form.FormPanel}, you must also set <tt>{@link #lazyRender} = true</tt>.
89 * @cfg {Boolean} lazyRender <tt>true</tt> to prevent the ComboBox from rendering until requested
90 * (should always be used when rendering into an {@link Ext.Editor} (e.g. {@link Ext.grid.EditorGridPanel Grids}),
91 * defaults to <tt>false</tt>).
94 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or <tt>true</tt> for a default
95 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
96 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
97 * <pre><code>{tag: "input", type: "text", size: "24", autocomplete: "off"}</code></pre>
100 * @cfg {Ext.data.Store/Array} store The data source to which this combo is bound (defaults to <tt>undefined</tt>).
101 * Acceptable values for this property are:
102 * <div class="mdetail-params"><ul>
103 * <li><b>any {@link Ext.data.Store Store} subclass</b></li>
104 * <li><b>an Array</b> : Arrays will be converted to a {@link Ext.data.ArrayStore} internally.
105 * <div class="mdetail-params"><ul>
106 * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
107 * A 1-dimensional array will automatically be expanded (each array item will be the combo
108 * {@link #valueField value} and {@link #displayField text})</div></li>
109 * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
110 * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
111 * {@link #valueField value}, while the value at index 1 is assumed to be the combo {@link #displayField text}.
112 * </div></li></ul></div></li></ul></div>
113 * <p>See also <tt>{@link #mode}</tt>.</p>
116 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
117 * the dropdown list (defaults to undefined, with no header element)
121 defaultAutoCreate : {tag: "input", type: "text", size: "24", autocomplete: "off"},
123 * @cfg {Number} listWidth The width (used as a parameter to {@link Ext.Element#setWidth}) of the dropdown
124 * list (defaults to the width of the ComboBox field). See also <tt>{@link #minListWidth}
127 * @cfg {String} displayField The underlying {@link Ext.data.Field#name data field name} to bind to this
128 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'text'</tt> if
129 * {@link #transform transforming a select} a select).
130 * <p>See also <tt>{@link #valueField}</tt>.</p>
131 * <p><b>Note</b>: if using a ComboBox in an {@link Ext.grid.EditorGridPanel Editor Grid} a
132 * {@link Ext.grid.Column#renderer renderer} will be needed to show the displayField when the editor is not
136 * @cfg {String} valueField The underlying {@link Ext.data.Field#name data value name} to bind to this
137 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'value'</tt> if
138 * {@link #transform transforming a select}).
139 * <p><b>Note</b>: use of a <tt>valueField</tt> requires the user to make a selection in order for a value to be
140 * mapped. See also <tt>{@link #hiddenName}</tt>, <tt>{@link #hiddenValue}</tt>, and <tt>{@link #displayField}</tt>.</p>
143 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
144 * field's data value (defaults to the underlying DOM element's name). Required for the combo's value to automatically
145 * post during a form submission. See also {@link #valueField}.
146 * <p><b>Note</b>: the hidden field's id will also default to this name if {@link #hiddenId} is not specified.
147 * The ComboBox {@link Ext.Component#id id} and the <tt>{@link #hiddenId}</tt> <b>should be different</b>, since
148 * no two DOM nodes should share the same id. So, if the ComboBox <tt>{@link Ext.form.Field#name name}</tt> and
149 * <tt>hiddenName</tt> are the same, you should specify a unique <tt>{@link #hiddenId}</tt>.</p>
152 * @cfg {String} hiddenId If <tt>{@link #hiddenName}</tt> is specified, <tt>hiddenId</tt> can also be provided
153 * to give the hidden field a unique id (defaults to the <tt>{@link #hiddenName}</tt>). The <tt>hiddenId</tt>
154 * and combo {@link Ext.Component#id id} should be different, since no two DOM
155 * nodes should share the same id.
158 * @cfg {String} hiddenValue Sets the initial value of the hidden field if {@link #hiddenName} is
159 * specified to contain the selected {@link #valueField}, from the Store. Defaults to the configured
160 * <tt>{@link Ext.form.Field#value value}</tt>.
163 * @cfg {String} listClass The CSS class to add to the predefined <tt>'x-combo-list'</tt> class
164 * applied the dropdown list element (defaults to '').
168 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list
169 * (defaults to <tt>'x-combo-selected'</tt>)
171 selectedClass : 'x-combo-selected',
173 * @cfg {String} listEmptyText The empty text to display in the data view if no items are found.
178 * @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always
179 * get the class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
180 * (defaults to <tt>'x-form-arrow-trigger'</tt> which displays a downward arrow icon).
182 triggerClass : 'x-form-arrow-trigger',
184 * @cfg {Boolean/String} shadow <tt>true</tt> or <tt>"sides"</tt> for the default effect, <tt>"frame"</tt> for
185 * 4-way shadow, and <tt>"drop"</tt> for bottom-right
189 * @cfg {String} listAlign A valid anchor position value. See <tt>{@link Ext.Element#alignTo}</tt> for details
190 * on supported anchor positions (defaults to <tt>'tl-bl?'</tt>)
192 listAlign : 'tl-bl?',
194 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown
195 * (defaults to <tt>300</tt>)
199 * @cfg {Number} minHeight The minimum height in pixels of the dropdown list when the list is constrained by its
200 * distance to the viewport edges (defaults to <tt>90</tt>)
204 * @cfg {String} triggerAction The action to execute when the trigger is clicked.
205 * <div class="mdetail-params"><ul>
206 * <li><b><tt>'query'</tt></b> : <b>Default</b>
207 * <p class="sub-desc">{@link #doQuery run the query} using the {@link Ext.form.Field#getRawValue raw value}.</p></li>
208 * <li><b><tt>'all'</tt></b> :
209 * <p class="sub-desc">{@link #doQuery run the query} specified by the <tt>{@link #allQuery}</tt> config option</p></li>
211 * <p>See also <code>{@link #queryParam}</code>.</p>
213 triggerAction : 'query',
215 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and
216 * {@link #typeAhead} activate (defaults to <tt>4</tt> if <tt>{@link #mode} = 'remote'</tt> or <tt>0</tt> if
217 * <tt>{@link #mode} = 'local'</tt>, does not apply if
218 * <tt>{@link Ext.form.TriggerField#editable editable} = false</tt>).
222 * @cfg {Boolean} typeAhead <tt>true</tt> to populate and autoselect the remainder of the text being
223 * typed after a configurable delay ({@link #typeAheadDelay}) if it matches a known value (defaults
228 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and
229 * sending the query to filter the dropdown list (defaults to <tt>500</tt> if <tt>{@link #mode} = 'remote'</tt>
230 * or <tt>10</tt> if <tt>{@link #mode} = 'local'</tt>)
234 * @cfg {Number} pageSize If greater than <tt>0</tt>, a {@link Ext.PagingToolbar} is displayed in the
235 * footer of the dropdown list and the {@link #doQuery filter queries} will execute with page start and
236 * {@link Ext.PagingToolbar#pageSize limit} parameters. Only applies when <tt>{@link #mode} = 'remote'</tt>
237 * (defaults to <tt>0</tt>).
241 * @cfg {Boolean} selectOnFocus <tt>true</tt> to select any existing text in the field immediately on focus.
242 * Only applies when <tt>{@link Ext.form.TriggerField#editable editable} = true</tt> (defaults to
245 selectOnFocus : false,
247 * @cfg {String} queryParam Name of the query ({@link Ext.data.Store#baseParam baseParam} name for the store)
248 * as it will be passed on the querystring (defaults to <tt>'query'</tt>)
250 queryParam : 'query',
252 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
253 * when <tt>{@link #mode} = 'remote'</tt> (defaults to <tt>'Loading...'</tt>)
255 loadingText : 'Loading...',
257 * @cfg {Boolean} resizable <tt>true</tt> to add a resize handle to the bottom of the dropdown list
258 * (creates an {@link Ext.Resizable} with 'se' {@link Ext.Resizable#pinned pinned} handles).
259 * Defaults to <tt>false</tt>.
263 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if
264 * <tt>{@link #resizable} = true</tt> (defaults to <tt>8</tt>)
268 * @cfg {String} allQuery The text query to send to the server to return all records for the list
269 * with no filtering (defaults to '')
273 * @cfg {String} mode Acceptable values are:
274 * <div class="mdetail-params"><ul>
275 * <li><b><tt>'remote'</tt></b> : <b>Default</b>
276 * <p class="sub-desc">Automatically loads the <tt>{@link #store}</tt> the <b>first</b> time the trigger
277 * is clicked. If you do not want the store to be automatically loaded the first time the trigger is
278 * clicked, set to <tt>'local'</tt> and manually load the store. To force a requery of the store
279 * <b>every</b> time the trigger is clicked see <tt>{@link #lastQuery}</tt>.</p></li>
280 * <li><b><tt>'local'</tt></b> :
281 * <p class="sub-desc">ComboBox loads local data</p>
283 var combo = new Ext.form.ComboBox({
284 renderTo: document.body,
286 store: new Ext.data.ArrayStore({
289 'myId', // numeric value is the key
292 data: [[1, 'item1'], [2, 'item2']] // data is local
295 displayField: 'displayText',
303 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to <tt>70</tt>, will
304 * be ignored if <tt>{@link #listWidth}</tt> has a higher value)
308 * @cfg {Boolean} forceSelection <tt>true</tt> to restrict the selected value to one of the values in the list,
309 * <tt>false</tt> to allow the user to set arbitrary text into the field (defaults to <tt>false</tt>)
311 forceSelection : false,
313 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
314 * if <tt>{@link #typeAhead} = true</tt> (defaults to <tt>250</tt>)
316 typeAheadDelay : 250,
318 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
319 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined). If this
320 * default text is used, it means there is no value set and no validation will occur on this field.
324 * @cfg {Boolean} lazyInit <tt>true</tt> to not initialize the list for this combo until the field is focused
325 * (defaults to <tt>true</tt>)
330 * The value of the match string used to filter the store. Delete this property to force a requery.
333 var combo = new Ext.form.ComboBox({
338 // delete the previous query in the beforequery event or set
339 // combo.lastQuery = null (this will reload the store the next time it expands)
340 beforequery: function(qe){
341 delete qe.combo.lastQuery;
346 * To make sure the filter in the store is not cleared the first time the ComboBox trigger is used
347 * configure the combo with <tt>lastQuery=''</tt>. Example use:
349 var combo = new Ext.form.ComboBox({
352 triggerAction: 'all',
356 * @property lastQuery
361 initComponent : function(){
362 Ext.form.ComboBox.superclass.initComponent.call(this);
366 * Fires when the dropdown list is expanded
367 * @param {Ext.form.ComboBox} combo This combo box
372 * Fires when the dropdown list is collapsed
373 * @param {Ext.form.ComboBox} combo This combo box
377 * @event beforeselect
378 * Fires before a list item is selected. Return false to cancel the selection.
379 * @param {Ext.form.ComboBox} combo This combo box
380 * @param {Ext.data.Record} record The data record returned from the underlying store
381 * @param {Number} index The index of the selected item in the dropdown list
386 * Fires when a list item is selected
387 * @param {Ext.form.ComboBox} combo This combo box
388 * @param {Ext.data.Record} record The data record returned from the underlying store
389 * @param {Number} index The index of the selected item in the dropdown list
394 * Fires before all queries are processed. Return false to cancel the query or set the queryEvent's
395 * cancel property to true.
396 * @param {Object} queryEvent An object that has these properties:<ul>
397 * <li><code>combo</code> : Ext.form.ComboBox <div class="sub-desc">This combo box</div></li>
398 * <li><code>query</code> : String <div class="sub-desc">The query</div></li>
399 * <li><code>forceAll</code> : Boolean <div class="sub-desc">True to force "all" query</div></li>
400 * <li><code>cancel</code> : Boolean <div class="sub-desc">Set to true to cancel the query</div></li>
406 var s = Ext.getDom(this.transform);
407 if(!this.hiddenName){
408 this.hiddenName = s.name;
412 var d = [], opts = s.options;
413 for(var i = 0, len = opts.length;i < len; i++){
415 value = (o.hasAttribute ? o.hasAttribute('value') : o.getAttributeNode('value').specified) ? o.value : o.text;
416 if(o.selected && Ext.isEmpty(this.value, true)) {
419 d.push([value, o.text]);
421 this.store = new Ext.data.ArrayStore({
423 fields: ['value', 'text'],
427 this.valueField = 'value';
428 this.displayField = 'text';
430 s.name = Ext.id(); // wipe out the name in case somewhere else they have a reference
431 if(!this.lazyRender){
433 this.el = Ext.DomHelper.insertBefore(s, this.autoCreate || this.defaultAutoCreate);
434 this.render(this.el.parentNode, s);
435 Ext.removeNode(s); // remove it
437 Ext.removeNode(s); // remove it
440 //auto-configure store from local array data
442 this.store = Ext.StoreMgr.lookup(this.store);
443 if(this.store.autoCreated){
444 this.displayField = this.valueField = 'field1';
445 if(!this.store.expandData){
446 this.displayField = 'field2';
452 this.selectedIndex = -1;
453 if(this.mode == 'local'){
454 if(!Ext.isDefined(this.initialConfig.queryDelay)){
455 this.queryDelay = 10;
457 if(!Ext.isDefined(this.initialConfig.minChars)){
464 onRender : function(ct, position){
465 Ext.form.ComboBox.superclass.onRender.call(this, ct, position);
467 this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName,
468 id: (this.hiddenId||this.hiddenName)}, 'before', true);
470 // prevent input submission
471 this.el.dom.removeAttribute('name');
474 this.el.dom.setAttribute('autocomplete', 'off');
480 this.on('focus', this.initList, this, {single: true});
485 initValue : function(){
486 Ext.form.ComboBox.superclass.initValue.call(this);
487 if(this.hiddenField){
488 this.hiddenField.value =
489 Ext.isDefined(this.hiddenValue) ? this.hiddenValue :
490 Ext.isDefined(this.value) ? this.value : '';
495 initList : function(){
497 var cls = 'x-combo-list';
499 this.list = new Ext.Layer({
500 parentEl: this.getListParent(),
502 cls: [cls, this.listClass].join(' '),
506 var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
507 this.list.setSize(lw, 0);
508 this.list.swallowEvent('mousewheel');
509 this.assetHeight = 0;
510 if(this.syncFont !== false){
511 this.list.setStyle('font-size', this.el.getStyle('font-size'));
514 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
515 this.assetHeight += this.header.getHeight();
518 this.innerList = this.list.createChild({cls:cls+'-inner'});
519 this.mon(this.innerList, 'mouseover', this.onViewOver, this);
520 this.mon(this.innerList, 'mousemove', this.onViewMove, this);
521 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
524 this.footer = this.list.createChild({cls:cls+'-ft'});
525 this.pageTb = new Ext.PagingToolbar({
527 pageSize: this.pageSize,
530 this.assetHeight += this.footer.getHeight();
535 * @cfg {String/Ext.XTemplate} tpl <p>The template string, or {@link Ext.XTemplate} instance to
536 * use to display each item in the dropdown list. The dropdown list is displayed in a
537 * DataView. See {@link #view}.</p>
538 * <p>The default template string is:</p><pre><code>
539 '<tpl for="."><div class="x-combo-list-item">{' + this.displayField + '}</div></tpl>'
541 * <p>Override the default value to create custom UI layouts for items in the list.
542 * For example:</p><pre><code>
543 '<tpl for="."><div ext:qtip="{state}. {nick}" class="x-combo-list-item">{state}</div></tpl>'
545 * <p>The template <b>must</b> contain one or more substitution parameters using field
546 * names from the Combo's</b> {@link #store Store}. In the example above an
547 * <pre>ext:qtip</pre> attribute is added to display other fields from the Store.</p>
548 * <p>To preserve the default visual look of list items, add the CSS class name
549 * <pre>x-combo-list-item</pre> to the template's container element.</p>
550 * <p>Also see {@link #itemSelector} for additional details.</p>
552 this.tpl = '<tpl for="."><div class="'+cls+'-item">{' + this.displayField + '}</div></tpl>';
554 * @cfg {String} itemSelector
555 * <p>A simple CSS selector (e.g. div.some-class or span:first-child) that will be
556 * used to determine what nodes the {@link #view Ext.DataView} which handles the dropdown
557 * display will be working with.</p>
558 * <p><b>Note</b>: this setting is <b>required</b> if a custom XTemplate has been
559 * specified in {@link #tpl} which assigns a class other than <pre>'x-combo-list-item'</pre>
560 * to dropdown list items</b>
565 * The {@link Ext.DataView DataView} used to display the ComboBox's options.
568 this.view = new Ext.DataView({
569 applyTo: this.innerList,
572 selectedClass: this.selectedClass,
573 itemSelector: this.itemSelector || '.' + cls + '-item',
574 emptyText: this.listEmptyText
577 this.mon(this.view, 'click', this.onViewClick, this);
579 this.bindStore(this.store, true);
582 this.resizer = new Ext.Resizable(this.list, {
583 pinned:true, handles:'se'
585 this.mon(this.resizer, 'resize', function(r, w, h){
586 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
588 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
589 this.restrictHeight();
592 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
598 * <p>Returns the element used to house this ComboBox's pop-up list. Defaults to the document body.</p>
599 * A custom implementation may be provided as a configuration option if the floating list needs to be rendered
600 * to a different Element. An example might be rendering the list inside a Menu so that clicking
601 * the list does not hide the Menu:<pre><code>
602 var store = new Ext.data.ArrayStore({
604 fields: ['initials', 'fullname'],
606 ['FF', 'Fred Flintstone'],
607 ['BR', 'Barney Rubble']
611 var combo = new Ext.form.ComboBox({
613 displayField: 'fullname',
614 emptyText: 'Select a name...',
615 forceSelection: true,
616 getListParent: function() {
617 return this.el.up('.x-menu');
619 iconCls: 'no-icon', //use iconCls if placing within menu to shift to right side of menu
622 triggerAction: 'all',
627 var menu = new Ext.menu.Menu({
630 combo // A Field in a Menu
635 getListParent : function() {
636 return document.body;
640 * Returns the store associated with this combo.
641 * @return {Ext.data.Store} The store
643 getStore : function(){
648 bindStore : function(store, initial){
649 if(this.store && !initial){
650 this.store.un('beforeload', this.onBeforeLoad, this);
651 this.store.un('load', this.onLoad, this);
652 this.store.un('exception', this.collapse, this);
653 if(this.store !== store && this.store.autoDestroy){
654 this.store.destroy();
659 this.view.bindStore(null);
665 this.lastQuery = null;
667 this.pageTb.bindStore(store);
671 this.store = Ext.StoreMgr.lookup(store);
674 beforeload: this.onBeforeLoad,
676 exception: this.collapse
680 this.view.bindStore(store);
686 initEvents : function(){
687 Ext.form.ComboBox.superclass.initEvents.call(this);
689 this.keyNav = new Ext.KeyNav(this.el, {
691 this.inKeyMode = true;
695 "down" : function(e){
696 if(!this.isExpanded()){
697 this.onTriggerClick();
699 this.inKeyMode = true;
704 "enter" : function(e){
706 this.delayedCheck = true;
707 this.unsetDelayCheck.defer(10, this);
715 this.onViewClick(false);
721 doRelay : function(foo, bar, hname){
722 if(hname == 'down' || this.scope.isExpanded()){
723 return Ext.KeyNav.prototype.doRelay.apply(this, arguments);
730 this.queryDelay = Math.max(this.queryDelay || 10,
731 this.mode == 'local' ? 10 : 250);
732 this.dqTask = new Ext.util.DelayedTask(this.initQuery, this);
734 this.taTask = new Ext.util.DelayedTask(this.onTypeAhead, this);
736 if(this.editable !== false && !this.enableKeyEvents){
737 this.mon(this.el, 'keyup', this.onKeyUp, this);
742 onDestroy : function(){
744 this.dqTask.cancel();
747 this.bindStore(null);
754 Ext.form.ComboBox.superclass.onDestroy.call(this);
758 unsetDelayCheck : function(){
759 delete this.delayedCheck;
763 fireKey : function(e){
764 var fn = function(ev){
765 if (ev.isNavKeyPress() && !this.isExpanded() && !this.delayedCheck) {
766 this.fireEvent("specialkey", this, ev);
769 //For some reason I can't track down, the events fire in a different order in webkit.
770 //Need a slight delay here
771 if(this.inEditor && Ext.isWebKit && e.getKey() == e.TAB){
772 fn.defer(10, this, [new Ext.EventObjectImpl(e)]);
779 onResize : function(w, h){
780 Ext.form.ComboBox.superclass.onResize.apply(this, arguments);
781 if(this.list && !Ext.isDefined(this.listWidth)){
782 var lw = Math.max(w, this.minListWidth);
783 this.list.setWidth(lw);
784 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
789 onEnable : function(){
790 Ext.form.ComboBox.superclass.onEnable.apply(this, arguments);
791 if(this.hiddenField){
792 this.hiddenField.disabled = false;
797 onDisable : function(){
798 Ext.form.ComboBox.superclass.onDisable.apply(this, arguments);
799 if(this.hiddenField){
800 this.hiddenField.disabled = true;
805 onBeforeLoad : function(){
809 this.innerList.update(this.loadingText ?
810 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
811 this.restrictHeight();
812 this.selectedIndex = -1;
820 if(this.store.getCount() > 0){
822 this.restrictHeight();
823 if(this.lastQuery == this.allQuery){
825 this.el.dom.select();
827 if(!this.selectByValue(this.value, true)){
828 this.select(0, true);
832 if(this.typeAhead && this.lastKey != Ext.EventObject.BACKSPACE && this.lastKey != Ext.EventObject.DELETE){
833 this.taTask.delay(this.typeAheadDelay);
837 this.onEmptyResults();
843 onTypeAhead : function(){
844 if(this.store.getCount() > 0){
845 var r = this.store.getAt(0);
846 var newValue = r.data[this.displayField];
847 var len = newValue.length;
848 var selStart = this.getRawValue().length;
850 this.setRawValue(newValue);
851 this.selectText(selStart, newValue.length);
857 onSelect : function(record, index){
858 if(this.fireEvent('beforeselect', this, record, index) !== false){
859 this.setValue(record.data[this.valueField || this.displayField]);
861 this.fireEvent('select', this, record, index);
867 var hf = this.hiddenField;
868 return hf && hf.name ? hf.name : this.hiddenName || Ext.form.ComboBox.superclass.getName.call(this);
872 * Returns the currently selected field value or empty string if no value is set.
873 * @return {String} value The selected value
875 getValue : function(){
877 return Ext.isDefined(this.value) ? this.value : '';
879 return Ext.form.ComboBox.superclass.getValue.call(this);
884 * Clears any text/value currently set in the field
886 clearValue : function(){
887 if(this.hiddenField){
888 this.hiddenField.value = '';
890 this.setRawValue('');
891 this.lastSelectionText = '';
892 this.applyEmptyText();
897 * Sets the specified value into the field. If the value finds a match, the corresponding record text
898 * will be displayed in the field. If the value does not match the data value of an existing item,
899 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
900 * Otherwise the field will be blank (although the value will still be set).
901 * @param {String} value The value to match
902 * @return {Ext.form.Field} this
904 setValue : function(v){
907 var r = this.findRecord(this.valueField, v);
909 text = r.data[this.displayField];
910 }else if(Ext.isDefined(this.valueNotFoundText)){
911 text = this.valueNotFoundText;
914 this.lastSelectionText = text;
915 if(this.hiddenField){
916 this.hiddenField.value = v;
918 Ext.form.ComboBox.superclass.setValue.call(this, text);
924 findRecord : function(prop, value){
926 if(this.store.getCount() > 0){
927 this.store.each(function(r){
928 if(r.data[prop] == value){
938 onViewMove : function(e, t){
939 this.inKeyMode = false;
943 onViewOver : function(e, t){
944 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
947 var item = this.view.findItemFromChild(t);
949 var index = this.view.indexOf(item);
950 this.select(index, false);
955 onViewClick : function(doFocus){
956 var index = this.view.getSelectedIndexes()[0];
957 var r = this.store.getAt(index);
959 this.onSelect(r, index);
961 if(doFocus !== false){
967 restrictHeight : function(){
968 this.innerList.dom.style.height = '';
969 var inner = this.innerList.dom;
970 var pad = this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight;
971 var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
972 var ha = this.getPosition()[1]-Ext.getBody().getScroll().top;
973 var hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height;
974 var space = Math.max(ha, hb, this.minHeight || 0)-this.list.shadowOffset-pad-5;
975 h = Math.min(h, space, this.maxHeight);
977 this.innerList.setHeight(h);
978 this.list.beginUpdate();
979 this.list.setHeight(h+pad);
980 this.list.alignTo(this.wrap, this.listAlign);
981 this.list.endUpdate();
985 onEmptyResults : function(){
990 * Returns true if the dropdown list is expanded, else false.
992 isExpanded : function(){
993 return this.list && this.list.isVisible();
997 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
998 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
999 * @param {String} value The data value of the item to select
1000 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1001 * selected item if it is not currently in view (defaults to true)
1002 * @return {Boolean} True if the value matched an item in the list, else false
1004 selectByValue : function(v, scrollIntoView){
1005 if(!Ext.isEmpty(v, true)){
1006 var r = this.findRecord(this.valueField || this.displayField, v);
1008 this.select(this.store.indexOf(r), scrollIntoView);
1016 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1017 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1018 * @param {Number} index The zero-based index of the list item to select
1019 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1020 * selected item if it is not currently in view (defaults to true)
1022 select : function(index, scrollIntoView){
1023 this.selectedIndex = index;
1024 this.view.select(index);
1025 if(scrollIntoView !== false){
1026 var el = this.view.getNode(index);
1028 this.innerList.scrollChildIntoView(el, false);
1034 selectNext : function(){
1035 var ct = this.store.getCount();
1037 if(this.selectedIndex == -1){
1039 }else if(this.selectedIndex < ct-1){
1040 this.select(this.selectedIndex+1);
1046 selectPrev : function(){
1047 var ct = this.store.getCount();
1049 if(this.selectedIndex == -1){
1051 }else if(this.selectedIndex !== 0){
1052 this.select(this.selectedIndex-1);
1058 onKeyUp : function(e){
1060 if(this.editable !== false && (k == e.BACKSPACE || !e.isSpecialKey())){
1062 this.dqTask.delay(this.queryDelay);
1064 Ext.form.ComboBox.superclass.onKeyUp.call(this, e);
1068 validateBlur : function(){
1069 return !this.list || !this.list.isVisible();
1073 initQuery : function(){
1074 this.doQuery(this.getRawValue());
1078 beforeBlur : function(){
1079 var val = this.getRawValue();
1080 if(this.forceSelection){
1081 if(val.length > 0 && val != this.emptyText){
1082 this.el.dom.value = Ext.isDefined(this.lastSelectionText) ? this.lastSelectionText : '';
1083 this.applyEmptyText();
1088 var rec = this.findRecord(this.displayField, val);
1090 val = rec.get(this.valueField || this.displayField);
1097 * Execute a query to filter the dropdown list. Fires the {@link #beforequery} event prior to performing the
1098 * query allowing the query action to be canceled if needed.
1099 * @param {String} query The SQL query to execute
1100 * @param {Boolean} forceAll <tt>true</tt> to force the query to execute even if there are currently fewer
1101 * characters in the field than the minimum specified by the <tt>{@link #minChars}</tt> config option. It
1102 * also clears any filter previously saved in the current store (defaults to <tt>false</tt>)
1104 doQuery : function(q, forceAll){
1105 q = Ext.isEmpty(q) ? '' : q;
1112 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1116 forceAll = qe.forceAll;
1117 if(forceAll === true || (q.length >= this.minChars)){
1118 if(this.lastQuery !== q){
1120 if(this.mode == 'local'){
1121 this.selectedIndex = -1;
1123 this.store.clearFilter();
1125 this.store.filter(this.displayField, q);
1129 this.store.baseParams[this.queryParam] = q;
1131 params: this.getParams(q)
1136 this.selectedIndex = -1;
1143 getParams : function(q){
1145 //p[this.queryParam] = q;
1148 p.limit = this.pageSize;
1154 * Hides the dropdown list if it is currently expanded. Fires the {@link #collapse} event on completion.
1156 collapse : function(){
1157 if(!this.isExpanded()){
1161 Ext.getDoc().un('mousewheel', this.collapseIf, this);
1162 Ext.getDoc().un('mousedown', this.collapseIf, this);
1163 this.fireEvent('collapse', this);
1167 collapseIf : function(e){
1168 if(!e.within(this.wrap) && !e.within(this.list)){
1174 * Expands the dropdown list if it is currently hidden. Fires the {@link #expand} event on completion.
1176 expand : function(){
1177 if(this.isExpanded() || !this.hasFocus){
1180 this.list.alignTo(this.wrap, this.listAlign);
1183 this.innerList.setOverflow('auto'); // necessary for FF 2.0/Mac
1187 mousewheel: this.collapseIf,
1188 mousedown: this.collapseIf
1190 this.fireEvent('expand', this);
1194 * @method onTriggerClick
1198 // Implements the default empty TriggerField.onTriggerClick function
1199 onTriggerClick : function(){
1203 if(this.isExpanded()){
1208 if(this.triggerAction == 'all') {
1209 this.doQuery(this.allQuery, true);
1211 this.doQuery(this.getRawValue());
1222 * @cfg {Boolean} grow @hide
1225 * @cfg {Number} growMin @hide
1228 * @cfg {Number} growMax @hide
1232 Ext.reg('combo', Ext.form.ComboBox);