3 * Copyright(c) 2006-2010 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 * automatically generating {@link Ext.data.Field#name field names} to work with all data components.
106 * <div class="mdetail-params"><ul>
107 * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
108 * A 1-dimensional array will automatically be expanded (each array item will be used for both the combo
109 * {@link #valueField} and {@link #displayField})</div></li>
110 * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
111 * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
112 * {@link #valueField}, while the value at index 1 is assumed to be the combo {@link #displayField}.
113 * </div></li></ul></div></li></ul></div>
114 * <p>See also <tt>{@link #mode}</tt>.</p>
117 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
118 * the dropdown list (defaults to undefined, with no header element)
122 defaultAutoCreate : {tag: "input", type: "text", size: "24", autocomplete: "off"},
124 * @cfg {Number} listWidth The width (used as a parameter to {@link Ext.Element#setWidth}) of the dropdown
125 * list (defaults to the width of the ComboBox field). See also <tt>{@link #minListWidth}
128 * @cfg {String} displayField The underlying {@link Ext.data.Field#name data field name} to bind to this
129 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'field1'</tt> if
130 * {@link #transform transforming a select} or if the {@link #store field name is autogenerated based on
131 * the store configuration}).
132 * <p>See also <tt>{@link #valueField}</tt>.</p>
133 * <p><b>Note</b>: if using a ComboBox in an {@link Ext.grid.EditorGridPanel Editor Grid} a
134 * {@link Ext.grid.Column#renderer renderer} will be needed to show the displayField when the editor is not
138 * @cfg {String} valueField The underlying {@link Ext.data.Field#name data value name} to bind to this
139 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'field2'</tt> if
140 * {@link #transform transforming a select} or if the {@link #store field name is autogenerated based on
141 * the store configuration}).
142 * <p><b>Note</b>: use of a <tt>valueField</tt> requires the user to make a selection in order for a value to be
143 * mapped. See also <tt>{@link #hiddenName}</tt>, <tt>{@link #hiddenValue}</tt>, and <tt>{@link #displayField}</tt>.</p>
146 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
147 * field's data value (defaults to the underlying DOM element's name). Required for the combo's value to automatically
148 * post during a form submission. See also {@link #valueField}.
149 * <p><b>Note</b>: the hidden field's id will also default to this name if {@link #hiddenId} is not specified.
150 * The ComboBox {@link Ext.Component#id id} and the <tt>{@link #hiddenId}</tt> <b>should be different</b>, since
151 * no two DOM nodes should share the same id. So, if the ComboBox <tt>{@link Ext.form.Field#name name}</tt> and
152 * <tt>hiddenName</tt> are the same, you should specify a unique <tt>{@link #hiddenId}</tt>.</p>
155 * @cfg {String} hiddenId If <tt>{@link #hiddenName}</tt> is specified, <tt>hiddenId</tt> can also be provided
156 * to give the hidden field a unique id (defaults to the <tt>{@link #hiddenName}</tt>). The <tt>hiddenId</tt>
157 * and combo {@link Ext.Component#id id} should be different, since no two DOM
158 * nodes should share the same id.
161 * @cfg {String} hiddenValue Sets the initial value of the hidden field if {@link #hiddenName} is
162 * specified to contain the selected {@link #valueField}, from the Store. Defaults to the configured
163 * <tt>{@link Ext.form.Field#value value}</tt>.
166 * @cfg {String} listClass The CSS class to add to the predefined <tt>'x-combo-list'</tt> class
167 * applied the dropdown list element (defaults to '').
171 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list
172 * (defaults to <tt>'x-combo-selected'</tt>)
174 selectedClass : 'x-combo-selected',
176 * @cfg {String} listEmptyText The empty text to display in the data view if no items are found.
181 * @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always
182 * get the class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
183 * (defaults to <tt>'x-form-arrow-trigger'</tt> which displays a downward arrow icon).
185 triggerClass : 'x-form-arrow-trigger',
187 * @cfg {Boolean/String} shadow <tt>true</tt> or <tt>"sides"</tt> for the default effect, <tt>"frame"</tt> for
188 * 4-way shadow, and <tt>"drop"</tt> for bottom-right
192 * @cfg {String/Array} listAlign A valid anchor position value. See <tt>{@link Ext.Element#alignTo}</tt> for details
193 * on supported anchor positions and offsets. To specify x/y offsets as well, this value
194 * may be specified as an Array of <tt>{@link Ext.Element#alignTo}</tt> method arguments.</p>
195 * <pre><code>[ 'tl-bl?', [6,0] ]</code></pre>(defaults to <tt>'tl-bl?'</tt>)
197 listAlign : 'tl-bl?',
199 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown
200 * (defaults to <tt>300</tt>)
204 * @cfg {Number} minHeight The minimum height in pixels of the dropdown list when the list is constrained by its
205 * distance to the viewport edges (defaults to <tt>90</tt>)
209 * @cfg {String} triggerAction The action to execute when the trigger is clicked.
210 * <div class="mdetail-params"><ul>
211 * <li><b><tt>'query'</tt></b> : <b>Default</b>
212 * <p class="sub-desc">{@link #doQuery run the query} using the {@link Ext.form.Field#getRawValue raw value}.</p></li>
213 * <li><b><tt>'all'</tt></b> :
214 * <p class="sub-desc">{@link #doQuery run the query} specified by the <tt>{@link #allQuery}</tt> config option</p></li>
216 * <p>See also <code>{@link #queryParam}</code>.</p>
218 triggerAction : 'query',
220 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and
221 * {@link #typeAhead} activate (defaults to <tt>4</tt> if <tt>{@link #mode} = 'remote'</tt> or <tt>0</tt> if
222 * <tt>{@link #mode} = 'local'</tt>, does not apply if
223 * <tt>{@link Ext.form.TriggerField#editable editable} = false</tt>).
227 * @cfg {Boolean} autoSelect <tt>true</tt> to select the first result gathered by the data store (defaults
228 * to <tt>true</tt>). A false value would require a manual selection from the dropdown list to set the components value
229 * unless the value of ({@link #typeAheadDelay}) were true.
233 * @cfg {Boolean} typeAhead <tt>true</tt> to populate and autoselect the remainder of the text being
234 * typed after a configurable delay ({@link #typeAheadDelay}) if it matches a known value (defaults
239 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and
240 * sending the query to filter the dropdown list (defaults to <tt>500</tt> if <tt>{@link #mode} = 'remote'</tt>
241 * or <tt>10</tt> if <tt>{@link #mode} = 'local'</tt>)
245 * @cfg {Number} pageSize If greater than <tt>0</tt>, a {@link Ext.PagingToolbar} is displayed in the
246 * footer of the dropdown list and the {@link #doQuery filter queries} will execute with page start and
247 * {@link Ext.PagingToolbar#pageSize limit} parameters. Only applies when <tt>{@link #mode} = 'remote'</tt>
248 * (defaults to <tt>0</tt>).
252 * @cfg {Boolean} selectOnFocus <tt>true</tt> to select any existing text in the field immediately on focus.
253 * Only applies when <tt>{@link Ext.form.TriggerField#editable editable} = true</tt> (defaults to
256 selectOnFocus : false,
258 * @cfg {String} queryParam Name of the query ({@link Ext.data.Store#baseParam baseParam} name for the store)
259 * as it will be passed on the querystring (defaults to <tt>'query'</tt>)
261 queryParam : 'query',
263 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
264 * when <tt>{@link #mode} = 'remote'</tt> (defaults to <tt>'Loading...'</tt>)
266 loadingText : 'Loading...',
268 * @cfg {Boolean} resizable <tt>true</tt> to add a resize handle to the bottom of the dropdown list
269 * (creates an {@link Ext.Resizable} with 'se' {@link Ext.Resizable#pinned pinned} handles).
270 * Defaults to <tt>false</tt>.
274 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if
275 * <tt>{@link #resizable} = true</tt> (defaults to <tt>8</tt>)
279 * @cfg {String} allQuery The text query to send to the server to return all records for the list
280 * with no filtering (defaults to '')
284 * @cfg {String} mode Acceptable values are:
285 * <div class="mdetail-params"><ul>
286 * <li><b><tt>'remote'</tt></b> : <b>Default</b>
287 * <p class="sub-desc">Automatically loads the <tt>{@link #store}</tt> the <b>first</b> time the trigger
288 * is clicked. If you do not want the store to be automatically loaded the first time the trigger is
289 * clicked, set to <tt>'local'</tt> and manually load the store. To force a requery of the store
290 * <b>every</b> time the trigger is clicked see <tt>{@link #lastQuery}</tt>.</p></li>
291 * <li><b><tt>'local'</tt></b> :
292 * <p class="sub-desc">ComboBox loads local data</p>
294 var combo = new Ext.form.ComboBox({
295 renderTo: document.body,
297 store: new Ext.data.ArrayStore({
300 'myId', // numeric value is the key
303 data: [[1, 'item1'], [2, 'item2']] // data is local
306 displayField: 'displayText',
314 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to <tt>70</tt>, will
315 * be ignored if <tt>{@link #listWidth}</tt> has a higher value)
319 * @cfg {Boolean} forceSelection <tt>true</tt> to restrict the selected value to one of the values in the list,
320 * <tt>false</tt> to allow the user to set arbitrary text into the field (defaults to <tt>false</tt>)
322 forceSelection : false,
324 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
325 * if <tt>{@link #typeAhead} = true</tt> (defaults to <tt>250</tt>)
327 typeAheadDelay : 250,
329 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
330 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined). If this
331 * default text is used, it means there is no value set and no validation will occur on this field.
335 * @cfg {Boolean} lazyInit <tt>true</tt> to not initialize the list for this combo until the field is focused
336 * (defaults to <tt>true</tt>)
341 * @cfg {Boolean} clearFilterOnReset <tt>true</tt> to clear any filters on the store (when in local mode) when reset is called
342 * (defaults to <tt>true</tt>)
344 clearFilterOnReset : true,
347 * @cfg {Boolean} submitValue False to clear the name attribute on the field so that it is not submitted during a form post.
348 * If a hiddenName is specified, setting this to true will cause both the hidden field and the element to be submitted.
349 * Defaults to <tt>undefined</tt>.
351 submitValue: undefined,
354 * The value of the match string used to filter the store. Delete this property to force a requery.
357 var combo = new Ext.form.ComboBox({
362 // delete the previous query in the beforequery event or set
363 // combo.lastQuery = null (this will reload the store the next time it expands)
364 beforequery: function(qe){
365 delete qe.combo.lastQuery;
370 * To make sure the filter in the store is not cleared the first time the ComboBox trigger is used
371 * configure the combo with <tt>lastQuery=''</tt>. Example use:
373 var combo = new Ext.form.ComboBox({
376 triggerAction: 'all',
380 * @property lastQuery
385 initComponent : function(){
386 Ext.form.ComboBox.superclass.initComponent.call(this);
390 * Fires when the dropdown list is expanded
391 * @param {Ext.form.ComboBox} combo This combo box
396 * Fires when the dropdown list is collapsed
397 * @param {Ext.form.ComboBox} combo This combo box
402 * @event beforeselect
403 * Fires before a list item is selected. Return false to cancel the selection.
404 * @param {Ext.form.ComboBox} combo This combo box
405 * @param {Ext.data.Record} record The data record returned from the underlying store
406 * @param {Number} index The index of the selected item in the dropdown list
411 * Fires when a list item is selected
412 * @param {Ext.form.ComboBox} combo This combo box
413 * @param {Ext.data.Record} record The data record returned from the underlying store
414 * @param {Number} index The index of the selected item in the dropdown list
419 * Fires before all queries are processed. Return false to cancel the query or set the queryEvent's
420 * cancel property to true.
421 * @param {Object} queryEvent An object that has these properties:<ul>
422 * <li><code>combo</code> : Ext.form.ComboBox <div class="sub-desc">This combo box</div></li>
423 * <li><code>query</code> : String <div class="sub-desc">The query</div></li>
424 * <li><code>forceAll</code> : Boolean <div class="sub-desc">True to force "all" query</div></li>
425 * <li><code>cancel</code> : Boolean <div class="sub-desc">Set to true to cancel the query</div></li>
431 var s = Ext.getDom(this.transform);
432 if(!this.hiddenName){
433 this.hiddenName = s.name;
437 var d = [], opts = s.options;
438 for(var i = 0, len = opts.length;i < len; i++){
440 value = (o.hasAttribute ? o.hasAttribute('value') : o.getAttributeNode('value').specified) ? o.value : o.text;
441 if(o.selected && Ext.isEmpty(this.value, true)) {
444 d.push([value, o.text]);
446 this.store = new Ext.data.ArrayStore({
448 fields: ['value', 'text'],
452 this.valueField = 'value';
453 this.displayField = 'text';
455 s.name = Ext.id(); // wipe out the name in case somewhere else they have a reference
456 if(!this.lazyRender){
458 this.el = Ext.DomHelper.insertBefore(s, this.autoCreate || this.defaultAutoCreate);
459 this.render(this.el.parentNode, s);
463 //auto-configure store from local array data
465 this.store = Ext.StoreMgr.lookup(this.store);
466 if(this.store.autoCreated){
467 this.displayField = this.valueField = 'field1';
468 if(!this.store.expandData){
469 this.displayField = 'field2';
475 this.selectedIndex = -1;
476 if(this.mode == 'local'){
477 if(!Ext.isDefined(this.initialConfig.queryDelay)){
478 this.queryDelay = 10;
480 if(!Ext.isDefined(this.initialConfig.minChars)){
487 onRender : function(ct, position){
488 if(this.hiddenName && !Ext.isDefined(this.submitValue)){
489 this.submitValue = false;
491 Ext.form.ComboBox.superclass.onRender.call(this, ct, position);
493 this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName,
494 id: (this.hiddenId||this.hiddenName)}, 'before', true);
498 this.el.dom.setAttribute('autocomplete', 'off');
504 this.on('focus', this.initList, this, {single: true});
509 initValue : function(){
510 Ext.form.ComboBox.superclass.initValue.call(this);
511 if(this.hiddenField){
512 this.hiddenField.value =
513 Ext.value(Ext.isDefined(this.hiddenValue) ? this.hiddenValue : this.value, '');
518 initList : function(){
520 var cls = 'x-combo-list',
521 listParent = Ext.getDom(this.getListParent() || Ext.getBody()),
522 zindex = parseInt(Ext.fly(listParent).getStyle('z-index') ,10);
524 if (this.ownerCt && !zindex){
525 this.findParentBy(function(ct){
526 zindex = parseInt(ct.getPositionEl().getStyle('z-index'), 10);
531 this.list = new Ext.Layer({
532 parentEl: listParent,
534 cls: [cls, this.listClass].join(' '),
536 zindex: (zindex || 12000) + 5
539 var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
540 this.list.setSize(lw, 0);
541 this.list.swallowEvent('mousewheel');
542 this.assetHeight = 0;
543 if(this.syncFont !== false){
544 this.list.setStyle('font-size', this.el.getStyle('font-size'));
547 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
548 this.assetHeight += this.header.getHeight();
551 this.innerList = this.list.createChild({cls:cls+'-inner'});
552 this.mon(this.innerList, 'mouseover', this.onViewOver, this);
553 this.mon(this.innerList, 'mousemove', this.onViewMove, this);
554 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
557 this.footer = this.list.createChild({cls:cls+'-ft'});
558 this.pageTb = new Ext.PagingToolbar({
560 pageSize: this.pageSize,
563 this.assetHeight += this.footer.getHeight();
568 * @cfg {String/Ext.XTemplate} tpl <p>The template string, or {@link Ext.XTemplate} instance to
569 * use to display each item in the dropdown list. The dropdown list is displayed in a
570 * DataView. See {@link #view}.</p>
571 * <p>The default template string is:</p><pre><code>
572 '<tpl for="."><div class="x-combo-list-item">{' + this.displayField + '}</div></tpl>'
574 * <p>Override the default value to create custom UI layouts for items in the list.
575 * For example:</p><pre><code>
576 '<tpl for="."><div ext:qtip="{state}. {nick}" class="x-combo-list-item">{state}</div></tpl>'
578 * <p>The template <b>must</b> contain one or more substitution parameters using field
579 * names from the Combo's</b> {@link #store Store}. In the example above an
580 * <pre>ext:qtip</pre> attribute is added to display other fields from the Store.</p>
581 * <p>To preserve the default visual look of list items, add the CSS class name
582 * <pre>x-combo-list-item</pre> to the template's container element.</p>
583 * <p>Also see {@link #itemSelector} for additional details.</p>
585 this.tpl = '<tpl for="."><div class="'+cls+'-item">{' + this.displayField + '}</div></tpl>';
587 * @cfg {String} itemSelector
588 * <p>A simple CSS selector (e.g. div.some-class or span:first-child) that will be
589 * used to determine what nodes the {@link #view Ext.DataView} which handles the dropdown
590 * display will be working with.</p>
591 * <p><b>Note</b>: this setting is <b>required</b> if a custom XTemplate has been
592 * specified in {@link #tpl} which assigns a class other than <pre>'x-combo-list-item'</pre>
593 * to dropdown list items</b>
598 * The {@link Ext.DataView DataView} used to display the ComboBox's options.
601 this.view = new Ext.DataView({
602 applyTo: this.innerList,
605 selectedClass: this.selectedClass,
606 itemSelector: this.itemSelector || '.' + cls + '-item',
607 emptyText: this.listEmptyText,
608 deferEmptyText: false
611 this.mon(this.view, {
612 containerclick : this.onViewClick,
613 click : this.onViewClick,
617 this.bindStore(this.store, true);
620 this.resizer = new Ext.Resizable(this.list, {
621 pinned:true, handles:'se'
623 this.mon(this.resizer, 'resize', function(r, w, h){
624 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
626 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
627 this.restrictHeight();
630 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
636 * <p>Returns the element used to house this ComboBox's pop-up list. Defaults to the document body.</p>
637 * A custom implementation may be provided as a configuration option if the floating list needs to be rendered
638 * to a different Element. An example might be rendering the list inside a Menu so that clicking
639 * the list does not hide the Menu:<pre><code>
640 var store = new Ext.data.ArrayStore({
642 fields: ['initials', 'fullname'],
644 ['FF', 'Fred Flintstone'],
645 ['BR', 'Barney Rubble']
649 var combo = new Ext.form.ComboBox({
651 displayField: 'fullname',
652 emptyText: 'Select a name...',
653 forceSelection: true,
654 getListParent: function() {
655 return this.el.up('.x-menu');
657 iconCls: 'no-icon', //use iconCls if placing within menu to shift to right side of menu
660 triggerAction: 'all',
665 var menu = new Ext.menu.Menu({
668 combo // A Field in a Menu
673 getListParent : function() {
674 return document.body;
678 * Returns the store associated with this combo.
679 * @return {Ext.data.Store} The store
681 getStore : function(){
686 bindStore : function(store, initial){
687 if(this.store && !initial){
688 if(this.store !== store && this.store.autoDestroy){
689 this.store.destroy();
691 this.store.un('beforeload', this.onBeforeLoad, this);
692 this.store.un('load', this.onLoad, this);
693 this.store.un('exception', this.collapse, this);
698 this.view.bindStore(null);
701 this.pageTb.bindStore(null);
707 this.lastQuery = null;
709 this.pageTb.bindStore(store);
713 this.store = Ext.StoreMgr.lookup(store);
716 beforeload: this.onBeforeLoad,
718 exception: this.collapse
722 this.view.bindStore(store);
728 Ext.form.ComboBox.superclass.reset.call(this);
729 if(this.clearFilterOnReset && this.mode == 'local'){
730 this.store.clearFilter();
735 initEvents : function(){
736 Ext.form.ComboBox.superclass.initEvents.call(this);
739 this.keyNav = new Ext.KeyNav(this.el, {
741 this.inKeyMode = true;
745 "down" : function(e){
746 if(!this.isExpanded()){
747 this.onTriggerClick();
749 this.inKeyMode = true;
754 "enter" : function(e){
769 doRelay : function(e, h, hname){
770 if(hname == 'down' || this.scope.isExpanded()){
771 // this MUST be called before ComboBox#fireKey()
772 var relay = Ext.KeyNav.prototype.doRelay.apply(this, arguments);
773 if(!Ext.isIE && Ext.EventManager.useKeydown){
774 // call Combo#fireKey() for browsers which use keydown event (except IE)
775 this.scope.fireKey(e);
783 defaultEventAction: 'stopEvent'
785 this.queryDelay = Math.max(this.queryDelay || 10,
786 this.mode == 'local' ? 10 : 250);
787 this.dqTask = new Ext.util.DelayedTask(this.initQuery, this);
789 this.taTask = new Ext.util.DelayedTask(this.onTypeAhead, this);
791 if(!this.enableKeyEvents){
792 this.mon(this.el, 'keyup', this.onKeyUp, this);
798 onDestroy : function(){
800 this.dqTask.cancel();
803 this.bindStore(null);
810 Ext.destroyMembers(this, 'hiddenField');
811 Ext.form.ComboBox.superclass.onDestroy.call(this);
815 fireKey : function(e){
816 if (!this.isExpanded()) {
817 Ext.form.ComboBox.superclass.fireKey.call(this, e);
822 onResize : function(w, h){
823 Ext.form.ComboBox.superclass.onResize.apply(this, arguments);
824 if(this.isVisible() && this.list){
831 doResize: function(w){
832 if(!Ext.isDefined(this.listWidth)){
833 var lw = Math.max(w, this.minListWidth);
834 this.list.setWidth(lw);
835 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
840 onEnable : function(){
841 Ext.form.ComboBox.superclass.onEnable.apply(this, arguments);
842 if(this.hiddenField){
843 this.hiddenField.disabled = false;
848 onDisable : function(){
849 Ext.form.ComboBox.superclass.onDisable.apply(this, arguments);
850 if(this.hiddenField){
851 this.hiddenField.disabled = true;
856 onBeforeLoad : function(){
860 this.innerList.update(this.loadingText ?
861 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
862 this.restrictHeight();
863 this.selectedIndex = -1;
871 if(this.store.getCount() > 0 || this.listEmptyText){
873 this.restrictHeight();
874 if(this.lastQuery == this.allQuery){
876 this.el.dom.select();
879 if(this.autoSelect !== false && !this.selectByValue(this.value, true)){
880 this.select(0, true);
883 if(this.autoSelect !== false){
886 if(this.typeAhead && this.lastKey != Ext.EventObject.BACKSPACE && this.lastKey != Ext.EventObject.DELETE){
887 this.taTask.delay(this.typeAheadDelay);
897 onTypeAhead : function(){
898 if(this.store.getCount() > 0){
899 var r = this.store.getAt(0);
900 var newValue = r.data[this.displayField];
901 var len = newValue.length;
902 var selStart = this.getRawValue().length;
904 this.setRawValue(newValue);
905 this.selectText(selStart, newValue.length);
911 assertValue : function(){
913 var val = this.getRawValue(),
914 rec = this.findRecord(this.displayField, val);
916 if(!rec && this.forceSelection){
917 if(val.length > 0 && val != this.emptyText){
918 this.el.dom.value = Ext.value(this.lastSelectionText, '');
919 this.applyEmptyText();
925 val = rec.get(this.valueField || this.displayField);
933 onSelect : function(record, index){
934 if(this.fireEvent('beforeselect', this, record, index) !== false){
935 this.setValue(record.data[this.valueField || this.displayField]);
937 this.fireEvent('select', this, record, index);
943 var hf = this.hiddenField;
944 return hf && hf.name ? hf.name : this.hiddenName || Ext.form.ComboBox.superclass.getName.call(this);
948 * Returns the currently selected field value or empty string if no value is set.
949 * @return {String} value The selected value
951 getValue : function(){
953 return Ext.isDefined(this.value) ? this.value : '';
955 return Ext.form.ComboBox.superclass.getValue.call(this);
960 * Clears any text/value currently set in the field
962 clearValue : function(){
963 if(this.hiddenField){
964 this.hiddenField.value = '';
966 this.setRawValue('');
967 this.lastSelectionText = '';
968 this.applyEmptyText();
973 * Sets the specified value into the field. If the value finds a match, the corresponding record text
974 * will be displayed in the field. If the value does not match the data value of an existing item,
975 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
976 * Otherwise the field will be blank (although the value will still be set).
977 * @param {String} value The value to match
978 * @return {Ext.form.Field} this
980 setValue : function(v){
983 var r = this.findRecord(this.valueField, v);
985 text = r.data[this.displayField];
986 }else if(Ext.isDefined(this.valueNotFoundText)){
987 text = this.valueNotFoundText;
990 this.lastSelectionText = text;
991 if(this.hiddenField){
992 this.hiddenField.value = Ext.value(v, '');
994 Ext.form.ComboBox.superclass.setValue.call(this, text);
1000 findRecord : function(prop, value){
1002 if(this.store.getCount() > 0){
1003 this.store.each(function(r){
1004 if(r.data[prop] == value){
1014 onViewMove : function(e, t){
1015 this.inKeyMode = false;
1019 onViewOver : function(e, t){
1020 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1023 var item = this.view.findItemFromChild(t);
1025 var index = this.view.indexOf(item);
1026 this.select(index, false);
1031 onViewClick : function(doFocus){
1032 var index = this.view.getSelectedIndexes()[0],
1036 this.onSelect(r, index);
1040 if(doFocus !== false){
1047 restrictHeight : function(){
1048 this.innerList.dom.style.height = '';
1049 var inner = this.innerList.dom,
1050 pad = this.list.getFrameWidth('tb') + (this.resizable ? this.handleHeight : 0) + this.assetHeight,
1051 h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight),
1052 ha = this.getPosition()[1]-Ext.getBody().getScroll().top,
1053 hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height,
1054 space = Math.max(ha, hb, this.minHeight || 0)-this.list.shadowOffset-pad-5;
1056 h = Math.min(h, space, this.maxHeight);
1058 this.innerList.setHeight(h);
1059 this.list.beginUpdate();
1060 this.list.setHeight(h+pad);
1061 this.list.alignTo.apply(this.list, [this.el].concat(this.listAlign));
1062 this.list.endUpdate();
1066 * Returns true if the dropdown list is expanded, else false.
1068 isExpanded : function(){
1069 return this.list && this.list.isVisible();
1073 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1074 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1075 * @param {String} value The data value of the item to select
1076 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1077 * selected item if it is not currently in view (defaults to true)
1078 * @return {Boolean} True if the value matched an item in the list, else false
1080 selectByValue : function(v, scrollIntoView){
1081 if(!Ext.isEmpty(v, true)){
1082 var r = this.findRecord(this.valueField || this.displayField, v);
1084 this.select(this.store.indexOf(r), scrollIntoView);
1092 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1093 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1094 * @param {Number} index The zero-based index of the list item to select
1095 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1096 * selected item if it is not currently in view (defaults to true)
1098 select : function(index, scrollIntoView){
1099 this.selectedIndex = index;
1100 this.view.select(index);
1101 if(scrollIntoView !== false){
1102 var el = this.view.getNode(index);
1104 this.innerList.scrollChildIntoView(el, false);
1111 selectNext : function(){
1112 var ct = this.store.getCount();
1114 if(this.selectedIndex == -1){
1116 }else if(this.selectedIndex < ct-1){
1117 this.select(this.selectedIndex+1);
1123 selectPrev : function(){
1124 var ct = this.store.getCount();
1126 if(this.selectedIndex == -1){
1128 }else if(this.selectedIndex !== 0){
1129 this.select(this.selectedIndex-1);
1135 onKeyUp : function(e){
1137 if(this.editable !== false && this.readOnly !== true && (k == e.BACKSPACE || !e.isSpecialKey())){
1140 this.dqTask.delay(this.queryDelay);
1142 Ext.form.ComboBox.superclass.onKeyUp.call(this, e);
1146 validateBlur : function(){
1147 return !this.list || !this.list.isVisible();
1151 initQuery : function(){
1152 this.doQuery(this.getRawValue());
1156 beforeBlur : function(){
1161 postBlur : function(){
1162 Ext.form.ComboBox.superclass.postBlur.call(this);
1164 this.inKeyMode = false;
1168 * Execute a query to filter the dropdown list. Fires the {@link #beforequery} event prior to performing the
1169 * query allowing the query action to be canceled if needed.
1170 * @param {String} query The SQL query to execute
1171 * @param {Boolean} forceAll <tt>true</tt> to force the query to execute even if there are currently fewer
1172 * characters in the field than the minimum specified by the <tt>{@link #minChars}</tt> config option. It
1173 * also clears any filter previously saved in the current store (defaults to <tt>false</tt>)
1175 doQuery : function(q, forceAll){
1176 q = Ext.isEmpty(q) ? '' : q;
1183 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1187 forceAll = qe.forceAll;
1188 if(forceAll === true || (q.length >= this.minChars)){
1189 if(this.lastQuery !== q){
1191 if(this.mode == 'local'){
1192 this.selectedIndex = -1;
1194 this.store.clearFilter();
1196 this.store.filter(this.displayField, q);
1200 this.store.baseParams[this.queryParam] = q;
1202 params: this.getParams(q)
1207 this.selectedIndex = -1;
1214 getParams : function(q){
1216 //p[this.queryParam] = q;
1219 p.limit = this.pageSize;
1225 * Hides the dropdown list if it is currently expanded. Fires the {@link #collapse} event on completion.
1227 collapse : function(){
1228 if(!this.isExpanded()){
1232 Ext.getDoc().un('mousewheel', this.collapseIf, this);
1233 Ext.getDoc().un('mousedown', this.collapseIf, this);
1234 this.fireEvent('collapse', this);
1238 collapseIf : function(e){
1239 if(!e.within(this.wrap) && !e.within(this.list)){
1245 * Expands the dropdown list if it is currently hidden. Fires the {@link #expand} event on completion.
1247 expand : function(){
1248 if(this.isExpanded() || !this.hasFocus){
1251 if(this.bufferSize){
1252 this.doResize(this.bufferSize);
1253 delete this.bufferSize;
1255 this.list.alignTo.apply(this.list, [this.el].concat(this.listAlign));
1258 this.innerList.setOverflow('auto'); // necessary for FF 2.0/Mac
1260 this.mon(Ext.getDoc(), {
1262 mousewheel: this.collapseIf,
1263 mousedown: this.collapseIf
1265 this.fireEvent('expand', this);
1269 * @method onTriggerClick
1273 // Implements the default empty TriggerField.onTriggerClick function
1274 onTriggerClick : function(){
1275 if(this.readOnly || this.disabled){
1278 if(this.isExpanded()){
1283 if(this.triggerAction == 'all') {
1284 this.doQuery(this.allQuery, true);
1286 this.doQuery(this.getRawValue());
1297 * @cfg {Boolean} grow @hide
1300 * @cfg {Number} growMin @hide
1303 * @cfg {Number} growMax @hide
1307 Ext.reg('combo', Ext.form.ComboBox);