3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.ComboBox
\r
9 * @extends Ext.form.TriggerField
\r
10 * <p>A combobox control with support for autocomplete, remote-loading, paging and many other features.</p>
\r
11 * <p>A ComboBox works in a similar manner to a traditional HTML <select> field. The difference is
\r
12 * that to submit the {@link #valueField}, you must specify a {@link #hiddenName} to create a hidden input
\r
13 * field to hold the value of the valueField. The <i>{@link #displayField}</i> is shown in the text field
\r
14 * which is named according to the {@link #name}.</p>
\r
15 * <p><b><u>Events</u></b></p>
\r
16 * <p>To do something when something in ComboBox is selected, configure the select event:<pre><code>
\r
17 var cb = new Ext.form.ComboBox({
\r
18 // all of your config options
\r
21 'select': yourFunction
\r
25 // Alternatively, you can assign events after the object is created:
\r
26 var cb = new Ext.form.ComboBox(yourOptions);
\r
27 cb.on('select', yourFunction, yourScope);
\r
30 * <p><b><u>ComboBox in Grid</u></b></p>
\r
31 * <p>If using a ComboBox in an {@link Ext.grid.EditorGridPanel Editor Grid} a {@link Ext.grid.Column#renderer renderer}
\r
32 * will be needed to show the displayField when the editor is not active. Set up the renderer manually, or implement
\r
33 * a reusable render, for example:<pre><code>
\r
34 // create reusable renderer
\r
35 Ext.util.Format.comboRenderer = function(combo){
\r
36 return function(value){
\r
37 var record = combo.findRecord(combo.{@link #valueField}, value);
\r
38 return record ? record.get(combo.{@link #displayField}) : combo.{@link #valueNotFoundText};
\r
42 // create the combo instance
\r
43 var combo = new Ext.form.ComboBox({
\r
44 {@link #typeAhead}: true,
\r
45 {@link #triggerAction}: 'all',
\r
46 {@link #lazyRender}:true,
\r
47 {@link #mode}: 'local',
\r
48 {@link #store}: new Ext.data.ArrayStore({
\r
54 data: [[1, 'item1'], [2, 'item2']]
\r
56 {@link #valueField}: 'myId',
\r
57 {@link #displayField}: 'displayText'
\r
60 // snippet of column model used within grid
\r
61 var cm = new Ext.grid.ColumnModel([{
\r
64 header: "Some Header",
\r
65 dataIndex: 'whatever',
\r
67 editor: combo, // specify reference to combo instance
\r
68 renderer: Ext.util.Format.comboRenderer(combo) // pass combo instance to reusable renderer
\r
74 * <p><b><u>Filtering</u></b></p>
\r
75 * <p>A ComboBox {@link #doQuery uses filtering itself}, for information about filtering the ComboBox
\r
76 * store manually see <tt>{@link #lastQuery}</tt>.</p>
\r
78 * Create a new ComboBox.
\r
79 * @param {Object} config Configuration options
\r
82 Ext.form.ComboBox = Ext.extend(Ext.form.TriggerField, {
\r
84 * @cfg {Mixed} transform The id, DOM node or element of an existing HTML SELECT to convert to a ComboBox.
\r
85 * Note that if you specify this and the combo is going to be in an {@link Ext.form.BasicForm} or
\r
86 * {@link Ext.form.FormPanel}, you must also set <tt>{@link #lazyRender} = true</tt>.
\r
89 * @cfg {Boolean} lazyRender <tt>true</tt> to prevent the ComboBox from rendering until requested
\r
90 * (should always be used when rendering into an {@link Ext.Editor} (e.g. {@link Ext.grid.EditorGridPanel Grids}),
\r
91 * defaults to <tt>false</tt>).
\r
94 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or <tt>true</tt> for a default
\r
95 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
\r
96 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
\r
97 * <pre><code>{tag: "input", type: "text", size: "24", autocomplete: "off"}</code></pre>
\r
100 * @cfg {Ext.data.Store/Array} store The data source to which this combo is bound (defaults to <tt>undefined</tt>).
\r
101 * Acceptable values for this property are:
\r
102 * <div class="mdetail-params"><ul>
\r
103 * <li><b>any {@link Ext.data.Store Store} subclass</b></li>
\r
104 * <li><b>an Array</b> : Arrays will be converted to a {@link Ext.data.ArrayStore} internally,
\r
105 * automatically generating {@link Ext.data.Field#name field names} to work with all data components.
\r
106 * <div class="mdetail-params"><ul>
\r
107 * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
\r
108 * A 1-dimensional array will automatically be expanded (each array item will be used for both the combo
\r
109 * {@link #valueField} and {@link #displayField})</div></li>
\r
110 * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
\r
111 * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
\r
112 * {@link #valueField}, while the value at index 1 is assumed to be the combo {@link #displayField}.
\r
113 * </div></li></ul></div></li></ul></div>
\r
114 * <p>See also <tt>{@link #mode}</tt>.</p>
\r
117 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
\r
118 * the dropdown list (defaults to undefined, with no header element)
\r
122 defaultAutoCreate : {tag: "input", type: "text", size: "24", autocomplete: "off"},
\r
124 * @cfg {Number} listWidth The width (used as a parameter to {@link Ext.Element#setWidth}) of the dropdown
\r
125 * list (defaults to the width of the ComboBox field). See also <tt>{@link #minListWidth}
\r
128 * @cfg {String} displayField The underlying {@link Ext.data.Field#name data field name} to bind to this
\r
129 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'field1'</tt> if
\r
130 * {@link #transform transforming a select} or if the {@link #store field name is autogenerated based on
\r
131 * the store configuration}).
\r
132 * <p>See also <tt>{@link #valueField}</tt>.</p>
\r
133 * <p><b>Note</b>: if using a ComboBox in an {@link Ext.grid.EditorGridPanel Editor Grid} a
\r
134 * {@link Ext.grid.Column#renderer renderer} will be needed to show the displayField when the editor is not
\r
138 * @cfg {String} valueField The underlying {@link Ext.data.Field#name data value name} to bind to this
\r
139 * ComboBox (defaults to undefined if <tt>{@link #mode} = 'remote'</tt> or <tt>'field2'</tt> if
\r
140 * {@link #transform transforming a select} or if the {@link #store field name is autogenerated based on
\r
141 * the store configuration}).
\r
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
\r
143 * mapped. See also <tt>{@link #hiddenName}</tt>, <tt>{@link #hiddenValue}</tt>, and <tt>{@link #displayField}</tt>.</p>
\r
146 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
\r
147 * field's data value (defaults to the underlying DOM element's name). Required for the combo's value to automatically
\r
148 * post during a form submission. See also {@link #valueField}.
\r
149 * <p><b>Note</b>: the hidden field's id will also default to this name if {@link #hiddenId} is not specified.
\r
150 * The ComboBox {@link Ext.Component#id id} and the <tt>{@link #hiddenId}</tt> <b>should be different</b>, since
\r
151 * no two DOM nodes should share the same id. So, if the ComboBox <tt>{@link Ext.form.Field#name name}</tt> and
\r
152 * <tt>hiddenName</tt> are the same, you should specify a unique <tt>{@link #hiddenId}</tt>.</p>
\r
155 * @cfg {String} hiddenId If <tt>{@link #hiddenName}</tt> is specified, <tt>hiddenId</tt> can also be provided
\r
156 * to give the hidden field a unique id (defaults to the <tt>{@link #hiddenName}</tt>). The <tt>hiddenId</tt>
\r
157 * and combo {@link Ext.Component#id id} should be different, since no two DOM
\r
158 * nodes should share the same id.
\r
161 * @cfg {String} hiddenValue Sets the initial value of the hidden field if {@link #hiddenName} is
\r
162 * specified to contain the selected {@link #valueField}, from the Store. Defaults to the configured
\r
163 * <tt>{@link Ext.form.Field#value value}</tt>.
\r
166 * @cfg {String} listClass The CSS class to add to the predefined <tt>'x-combo-list'</tt> class
\r
167 * applied the dropdown list element (defaults to '').
\r
171 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list
\r
172 * (defaults to <tt>'x-combo-selected'</tt>)
\r
174 selectedClass : 'x-combo-selected',
\r
176 * @cfg {String} listEmptyText The empty text to display in the data view if no items are found.
\r
181 * @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always
\r
182 * get the class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
\r
183 * (defaults to <tt>'x-form-arrow-trigger'</tt> which displays a downward arrow icon).
\r
185 triggerClass : 'x-form-arrow-trigger',
\r
187 * @cfg {Boolean/String} shadow <tt>true</tt> or <tt>"sides"</tt> for the default effect, <tt>"frame"</tt> for
\r
188 * 4-way shadow, and <tt>"drop"</tt> for bottom-right
\r
192 * @cfg {String} listAlign A valid anchor position value. See <tt>{@link Ext.Element#alignTo}</tt> for details
\r
193 * on supported anchor positions (defaults to <tt>'tl-bl?'</tt>)
\r
195 listAlign : 'tl-bl?',
\r
197 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown
\r
198 * (defaults to <tt>300</tt>)
\r
202 * @cfg {Number} minHeight The minimum height in pixels of the dropdown list when the list is constrained by its
\r
203 * distance to the viewport edges (defaults to <tt>90</tt>)
\r
207 * @cfg {String} triggerAction The action to execute when the trigger is clicked.
\r
208 * <div class="mdetail-params"><ul>
\r
209 * <li><b><tt>'query'</tt></b> : <b>Default</b>
\r
210 * <p class="sub-desc">{@link #doQuery run the query} using the {@link Ext.form.Field#getRawValue raw value}.</p></li>
\r
211 * <li><b><tt>'all'</tt></b> :
\r
212 * <p class="sub-desc">{@link #doQuery run the query} specified by the <tt>{@link #allQuery}</tt> config option</p></li>
\r
214 * <p>See also <code>{@link #queryParam}</code>.</p>
\r
216 triggerAction : 'query',
\r
218 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and
\r
219 * {@link #typeAhead} activate (defaults to <tt>4</tt> if <tt>{@link #mode} = 'remote'</tt> or <tt>0</tt> if
\r
220 * <tt>{@link #mode} = 'local'</tt>, does not apply if
\r
221 * <tt>{@link Ext.form.TriggerField#editable editable} = false</tt>).
\r
225 * @cfg {Boolean} typeAhead <tt>true</tt> to populate and autoselect the remainder of the text being
\r
226 * typed after a configurable delay ({@link #typeAheadDelay}) if it matches a known value (defaults
\r
227 * to <tt>false</tt>)
\r
231 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and
\r
232 * sending the query to filter the dropdown list (defaults to <tt>500</tt> if <tt>{@link #mode} = 'remote'</tt>
\r
233 * or <tt>10</tt> if <tt>{@link #mode} = 'local'</tt>)
\r
237 * @cfg {Number} pageSize If greater than <tt>0</tt>, a {@link Ext.PagingToolbar} is displayed in the
\r
238 * footer of the dropdown list and the {@link #doQuery filter queries} will execute with page start and
\r
239 * {@link Ext.PagingToolbar#pageSize limit} parameters. Only applies when <tt>{@link #mode} = 'remote'</tt>
\r
240 * (defaults to <tt>0</tt>).
\r
244 * @cfg {Boolean} selectOnFocus <tt>true</tt> to select any existing text in the field immediately on focus.
\r
245 * Only applies when <tt>{@link Ext.form.TriggerField#editable editable} = true</tt> (defaults to
\r
248 selectOnFocus : false,
\r
250 * @cfg {String} queryParam Name of the query ({@link Ext.data.Store#baseParam baseParam} name for the store)
\r
251 * as it will be passed on the querystring (defaults to <tt>'query'</tt>)
\r
253 queryParam : 'query',
\r
255 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
\r
256 * when <tt>{@link #mode} = 'remote'</tt> (defaults to <tt>'Loading...'</tt>)
\r
258 loadingText : 'Loading...',
\r
260 * @cfg {Boolean} resizable <tt>true</tt> to add a resize handle to the bottom of the dropdown list
\r
261 * (creates an {@link Ext.Resizable} with 'se' {@link Ext.Resizable#pinned pinned} handles).
\r
262 * Defaults to <tt>false</tt>.
\r
266 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if
\r
267 * <tt>{@link #resizable} = true</tt> (defaults to <tt>8</tt>)
\r
271 * @cfg {String} allQuery The text query to send to the server to return all records for the list
\r
272 * with no filtering (defaults to '')
\r
276 * @cfg {String} mode Acceptable values are:
\r
277 * <div class="mdetail-params"><ul>
\r
278 * <li><b><tt>'remote'</tt></b> : <b>Default</b>
\r
279 * <p class="sub-desc">Automatically loads the <tt>{@link #store}</tt> the <b>first</b> time the trigger
\r
280 * is clicked. If you do not want the store to be automatically loaded the first time the trigger is
\r
281 * clicked, set to <tt>'local'</tt> and manually load the store. To force a requery of the store
\r
282 * <b>every</b> time the trigger is clicked see <tt>{@link #lastQuery}</tt>.</p></li>
\r
283 * <li><b><tt>'local'</tt></b> :
\r
284 * <p class="sub-desc">ComboBox loads local data</p>
\r
286 var combo = new Ext.form.ComboBox({
\r
287 renderTo: document.body,
\r
289 store: new Ext.data.ArrayStore({
\r
292 'myId', // numeric value is the key
\r
295 data: [[1, 'item1'], [2, 'item2']] // data is local
\r
297 valueField: 'myId',
\r
298 displayField: 'displayText',
\r
299 triggerAction: 'all'
\r
301 * </code></pre></li>
\r
306 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to <tt>70</tt>, will
\r
307 * be ignored if <tt>{@link #listWidth}</tt> has a higher value)
\r
311 * @cfg {Boolean} forceSelection <tt>true</tt> to restrict the selected value to one of the values in the list,
\r
312 * <tt>false</tt> to allow the user to set arbitrary text into the field (defaults to <tt>false</tt>)
\r
314 forceSelection : false,
\r
316 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
\r
317 * if <tt>{@link #typeAhead} = true</tt> (defaults to <tt>250</tt>)
\r
319 typeAheadDelay : 250,
\r
321 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
\r
322 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined). If this
\r
323 * default text is used, it means there is no value set and no validation will occur on this field.
\r
327 * @cfg {Boolean} lazyInit <tt>true</tt> to not initialize the list for this combo until the field is focused
\r
328 * (defaults to <tt>true</tt>)
\r
333 * The value of the match string used to filter the store. Delete this property to force a requery.
\r
336 var combo = new Ext.form.ComboBox({
\r
341 // delete the previous query in the beforequery event or set
\r
342 // combo.lastQuery = null (this will reload the store the next time it expands)
\r
343 beforequery: function(qe){
\r
344 delete qe.combo.lastQuery;
\r
349 * To make sure the filter in the store is not cleared the first time the ComboBox trigger is used
\r
350 * configure the combo with <tt>lastQuery=''</tt>. Example use:
\r
352 var combo = new Ext.form.ComboBox({
\r
355 triggerAction: 'all',
\r
359 * @property lastQuery
\r
364 initComponent : function(){
\r
365 Ext.form.ComboBox.superclass.initComponent.call(this);
\r
369 * Fires when the dropdown list is expanded
\r
370 * @param {Ext.form.ComboBox} combo This combo box
\r
375 * Fires when the dropdown list is collapsed
\r
376 * @param {Ext.form.ComboBox} combo This combo box
\r
380 * @event beforeselect
\r
381 * Fires before a list item is selected. Return false to cancel the selection.
\r
382 * @param {Ext.form.ComboBox} combo This combo box
\r
383 * @param {Ext.data.Record} record The data record returned from the underlying store
\r
384 * @param {Number} index The index of the selected item in the dropdown list
\r
389 * Fires when a list item is selected
\r
390 * @param {Ext.form.ComboBox} combo This combo box
\r
391 * @param {Ext.data.Record} record The data record returned from the underlying store
\r
392 * @param {Number} index The index of the selected item in the dropdown list
\r
396 * @event beforequery
\r
397 * Fires before all queries are processed. Return false to cancel the query or set the queryEvent's
\r
398 * cancel property to true.
\r
399 * @param {Object} queryEvent An object that has these properties:<ul>
\r
400 * <li><code>combo</code> : Ext.form.ComboBox <div class="sub-desc">This combo box</div></li>
\r
401 * <li><code>query</code> : String <div class="sub-desc">The query</div></li>
\r
402 * <li><code>forceAll</code> : Boolean <div class="sub-desc">True to force "all" query</div></li>
\r
403 * <li><code>cancel</code> : Boolean <div class="sub-desc">Set to true to cancel the query</div></li>
\r
408 if(this.transform){
\r
409 var s = Ext.getDom(this.transform);
\r
410 if(!this.hiddenName){
\r
411 this.hiddenName = s.name;
\r
414 this.mode = 'local';
\r
415 var d = [], opts = s.options;
\r
416 for(var i = 0, len = opts.length;i < len; i++){
\r
418 value = (o.hasAttribute ? o.hasAttribute('value') : o.getAttributeNode('value').specified) ? o.value : o.text;
\r
419 if(o.selected && Ext.isEmpty(this.value, true)) {
\r
420 this.value = value;
\r
422 d.push([value, o.text]);
\r
424 this.store = new Ext.data.ArrayStore({
\r
426 fields: ['value', 'text'],
\r
430 this.valueField = 'value';
\r
431 this.displayField = 'text';
\r
433 s.name = Ext.id(); // wipe out the name in case somewhere else they have a reference
\r
434 if(!this.lazyRender){
\r
435 this.target = true;
\r
436 this.el = Ext.DomHelper.insertBefore(s, this.autoCreate || this.defaultAutoCreate);
\r
437 this.render(this.el.parentNode, s);
\r
438 Ext.removeNode(s); // remove it
\r
440 Ext.removeNode(s); // remove it
\r
443 //auto-configure store from local array data
\r
444 else if(this.store){
\r
445 this.store = Ext.StoreMgr.lookup(this.store);
\r
446 if(this.store.autoCreated){
\r
447 this.displayField = this.valueField = 'field1';
\r
448 if(!this.store.expandData){
\r
449 this.displayField = 'field2';
\r
451 this.mode = 'local';
\r
455 this.selectedIndex = -1;
\r
456 if(this.mode == 'local'){
\r
457 if(!Ext.isDefined(this.initialConfig.queryDelay)){
\r
458 this.queryDelay = 10;
\r
460 if(!Ext.isDefined(this.initialConfig.minChars)){
\r
467 onRender : function(ct, position){
\r
468 Ext.form.ComboBox.superclass.onRender.call(this, ct, position);
\r
469 if(this.hiddenName){
\r
470 this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName,
\r
471 id: (this.hiddenId||this.hiddenName)}, 'before', true);
\r
473 // prevent input submission
\r
474 this.el.dom.removeAttribute('name');
\r
477 this.el.dom.setAttribute('autocomplete', 'off');
\r
480 if(!this.lazyInit){
\r
483 this.on('focus', this.initList, this, {single: true});
\r
488 initValue : function(){
\r
489 Ext.form.ComboBox.superclass.initValue.call(this);
\r
490 if(this.hiddenField){
\r
491 this.hiddenField.value =
\r
492 Ext.isDefined(this.hiddenValue) ? this.hiddenValue :
\r
493 Ext.isDefined(this.value) ? this.value : '';
\r
498 initList : function(){
\r
500 var cls = 'x-combo-list';
\r
502 this.list = new Ext.Layer({
\r
503 parentEl: this.getListParent(),
\r
504 shadow: this.shadow,
\r
505 cls: [cls, this.listClass].join(' '),
\r
509 var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
\r
510 this.list.setSize(lw, 0);
\r
511 this.list.swallowEvent('mousewheel');
\r
512 this.assetHeight = 0;
\r
513 if(this.syncFont !== false){
\r
514 this.list.setStyle('font-size', this.el.getStyle('font-size'));
\r
517 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
\r
518 this.assetHeight += this.header.getHeight();
\r
521 this.innerList = this.list.createChild({cls:cls+'-inner'});
\r
522 this.mon(this.innerList, 'mouseover', this.onViewOver, this);
\r
523 this.mon(this.innerList, 'mousemove', this.onViewMove, this);
\r
524 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
\r
527 this.footer = this.list.createChild({cls:cls+'-ft'});
\r
528 this.pageTb = new Ext.PagingToolbar({
\r
530 pageSize: this.pageSize,
\r
531 renderTo:this.footer
\r
533 this.assetHeight += this.footer.getHeight();
\r
538 * @cfg {String/Ext.XTemplate} tpl <p>The template string, or {@link Ext.XTemplate} instance to
\r
539 * use to display each item in the dropdown list. The dropdown list is displayed in a
\r
540 * DataView. See {@link #view}.</p>
\r
541 * <p>The default template string is:</p><pre><code>
\r
542 '<tpl for="."><div class="x-combo-list-item">{' + this.displayField + '}</div></tpl>'
\r
544 * <p>Override the default value to create custom UI layouts for items in the list.
\r
545 * For example:</p><pre><code>
\r
546 '<tpl for="."><div ext:qtip="{state}. {nick}" class="x-combo-list-item">{state}</div></tpl>'
\r
548 * <p>The template <b>must</b> contain one or more substitution parameters using field
\r
549 * names from the Combo's</b> {@link #store Store}. In the example above an
\r
550 * <pre>ext:qtip</pre> attribute is added to display other fields from the Store.</p>
\r
551 * <p>To preserve the default visual look of list items, add the CSS class name
\r
552 * <pre>x-combo-list-item</pre> to the template's container element.</p>
\r
553 * <p>Also see {@link #itemSelector} for additional details.</p>
\r
555 this.tpl = '<tpl for="."><div class="'+cls+'-item">{' + this.displayField + '}</div></tpl>';
\r
557 * @cfg {String} itemSelector
\r
558 * <p>A simple CSS selector (e.g. div.some-class or span:first-child) that will be
\r
559 * used to determine what nodes the {@link #view Ext.DataView} which handles the dropdown
\r
560 * display will be working with.</p>
\r
561 * <p><b>Note</b>: this setting is <b>required</b> if a custom XTemplate has been
\r
562 * specified in {@link #tpl} which assigns a class other than <pre>'x-combo-list-item'</pre>
\r
563 * to dropdown list items</b>
\r
568 * The {@link Ext.DataView DataView} used to display the ComboBox's options.
\r
569 * @type Ext.DataView
\r
571 this.view = new Ext.DataView({
\r
572 applyTo: this.innerList,
\r
574 singleSelect: true,
\r
575 selectedClass: this.selectedClass,
\r
576 itemSelector: this.itemSelector || '.' + cls + '-item',
\r
577 emptyText: this.listEmptyText
\r
580 this.mon(this.view, 'click', this.onViewClick, this);
\r
582 this.bindStore(this.store, true);
\r
584 if(this.resizable){
\r
585 this.resizer = new Ext.Resizable(this.list, {
\r
586 pinned:true, handles:'se'
\r
588 this.mon(this.resizer, 'resize', function(r, w, h){
\r
589 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
\r
590 this.listWidth = w;
\r
591 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
\r
592 this.restrictHeight();
\r
595 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
\r
601 * <p>Returns the element used to house this ComboBox's pop-up list. Defaults to the document body.</p>
\r
602 * A custom implementation may be provided as a configuration option if the floating list needs to be rendered
\r
603 * to a different Element. An example might be rendering the list inside a Menu so that clicking
\r
604 * the list does not hide the Menu:<pre><code>
\r
605 var store = new Ext.data.ArrayStore({
\r
607 fields: ['initials', 'fullname'],
\r
609 ['FF', 'Fred Flintstone'],
\r
610 ['BR', 'Barney Rubble']
\r
614 var combo = new Ext.form.ComboBox({
\r
616 displayField: 'fullname',
\r
617 emptyText: 'Select a name...',
\r
618 forceSelection: true,
\r
619 getListParent: function() {
\r
620 return this.el.up('.x-menu');
\r
622 iconCls: 'no-icon', //use iconCls if placing within menu to shift to right side of menu
\r
624 selectOnFocus: true,
\r
625 triggerAction: 'all',
\r
630 var menu = new Ext.menu.Menu({
\r
633 combo // A Field in a Menu
\r
638 getListParent : function() {
\r
639 return document.body;
\r
643 * Returns the store associated with this combo.
\r
644 * @return {Ext.data.Store} The store
\r
646 getStore : function(){
\r
651 bindStore : function(store, initial){
\r
652 if(this.store && !initial){
\r
653 if(this.store !== store && this.store.autoDestroy){
\r
654 this.store.destroy();
\r
656 this.store.un('beforeload', this.onBeforeLoad, this);
\r
657 this.store.un('load', this.onLoad, this);
\r
658 this.store.un('exception', this.collapse, this);
\r
663 this.view.bindStore(null);
\r
666 this.pageTb.bindStore(null);
\r
672 this.lastQuery = null;
\r
674 this.pageTb.bindStore(store);
\r
678 this.store = Ext.StoreMgr.lookup(store);
\r
681 beforeload: this.onBeforeLoad,
\r
683 exception: this.collapse
\r
687 this.view.bindStore(store);
\r
693 initEvents : function(){
\r
694 Ext.form.ComboBox.superclass.initEvents.call(this);
\r
696 this.keyNav = new Ext.KeyNav(this.el, {
\r
697 "up" : function(e){
\r
698 this.inKeyMode = true;
\r
702 "down" : function(e){
\r
703 if(!this.isExpanded()){
\r
704 this.onTriggerClick();
\r
706 this.inKeyMode = true;
\r
711 "enter" : function(e){
\r
712 this.onViewClick();
\r
715 "esc" : function(e){
\r
719 "tab" : function(e){
\r
720 this.onViewClick(false);
\r
726 doRelay : function(e, h, hname){
\r
727 if(hname == 'down' || this.scope.isExpanded()){
\r
728 // this MUST be called before ComboBox#fireKey()
\r
729 var relay = Ext.KeyNav.prototype.doRelay.apply(this, arguments);
\r
730 if(!Ext.isIE && Ext.EventManager.useKeydown){
\r
731 // call Combo#fireKey() for browsers which use keydown event (except IE)
\r
732 this.scope.fireKey(e);
\r
739 forceKeyDown : true,
\r
740 defaultEventAction: 'stopEvent'
\r
742 this.queryDelay = Math.max(this.queryDelay || 10,
\r
743 this.mode == 'local' ? 10 : 250);
\r
744 this.dqTask = new Ext.util.DelayedTask(this.initQuery, this);
\r
745 if(this.typeAhead){
\r
746 this.taTask = new Ext.util.DelayedTask(this.onTypeAhead, this);
\r
748 if(this.editable !== false && !this.enableKeyEvents){
\r
749 this.mon(this.el, 'keyup', this.onKeyUp, this);
\r
754 onDestroy : function(){
\r
756 this.dqTask.cancel();
\r
757 this.dqTask = null;
\r
759 this.bindStore(null);
\r
766 Ext.form.ComboBox.superclass.onDestroy.call(this);
\r
770 fireKey : function(e){
\r
771 if (!this.isExpanded()) {
\r
772 Ext.form.ComboBox.superclass.fireKey.call(this, e);
\r
777 onResize : function(w, h){
\r
778 Ext.form.ComboBox.superclass.onResize.apply(this, arguments);
\r
779 if(this.isVisible() && this.list){
\r
782 this.bufferSize = w;
\r
786 doResize: function(w){
\r
787 if(!Ext.isDefined(this.listWidth)){
\r
788 var lw = Math.max(w, this.minListWidth);
\r
789 this.list.setWidth(lw);
\r
790 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
\r
795 onEnable : function(){
\r
796 Ext.form.ComboBox.superclass.onEnable.apply(this, arguments);
\r
797 if(this.hiddenField){
\r
798 this.hiddenField.disabled = false;
\r
803 onDisable : function(){
\r
804 Ext.form.ComboBox.superclass.onDisable.apply(this, arguments);
\r
805 if(this.hiddenField){
\r
806 this.hiddenField.disabled = true;
\r
811 onBeforeLoad : function(){
\r
812 if(!this.hasFocus){
\r
815 this.innerList.update(this.loadingText ?
\r
816 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
\r
817 this.restrictHeight();
\r
818 this.selectedIndex = -1;
\r
822 onLoad : function(){
\r
823 if(!this.hasFocus){
\r
826 if(this.store.getCount() > 0 || this.listEmptyText){
\r
828 this.restrictHeight();
\r
829 if(this.lastQuery == this.allQuery){
\r
831 this.el.dom.select();
\r
833 if(!this.selectByValue(this.value, true)){
\r
834 this.select(0, true);
\r
838 if(this.typeAhead && this.lastKey != Ext.EventObject.BACKSPACE && this.lastKey != Ext.EventObject.DELETE){
\r
839 this.taTask.delay(this.typeAheadDelay);
\r
843 this.onEmptyResults();
\r
849 onTypeAhead : function(){
\r
850 if(this.store.getCount() > 0){
\r
851 var r = this.store.getAt(0);
\r
852 var newValue = r.data[this.displayField];
\r
853 var len = newValue.length;
\r
854 var selStart = this.getRawValue().length;
\r
855 if(selStart != len){
\r
856 this.setRawValue(newValue);
\r
857 this.selectText(selStart, newValue.length);
\r
863 onSelect : function(record, index){
\r
864 if(this.fireEvent('beforeselect', this, record, index) !== false){
\r
865 this.setValue(record.data[this.valueField || this.displayField]);
\r
867 this.fireEvent('select', this, record, index);
\r
872 getName: function(){
\r
873 var hf = this.hiddenField;
\r
874 return hf && hf.name ? hf.name : this.hiddenName || Ext.form.ComboBox.superclass.getName.call(this);
\r
878 * Returns the currently selected field value or empty string if no value is set.
\r
879 * @return {String} value The selected value
\r
881 getValue : function(){
\r
882 if(this.valueField){
\r
883 return Ext.isDefined(this.value) ? this.value : '';
\r
885 return Ext.form.ComboBox.superclass.getValue.call(this);
\r
890 * Clears any text/value currently set in the field
\r
892 clearValue : function(){
\r
893 if(this.hiddenField){
\r
894 this.hiddenField.value = '';
\r
896 this.setRawValue('');
\r
897 this.lastSelectionText = '';
\r
898 this.applyEmptyText();
\r
903 * Sets the specified value into the field. If the value finds a match, the corresponding record text
\r
904 * will be displayed in the field. If the value does not match the data value of an existing item,
\r
905 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
\r
906 * Otherwise the field will be blank (although the value will still be set).
\r
907 * @param {String} value The value to match
\r
908 * @return {Ext.form.Field} this
\r
910 setValue : function(v){
\r
912 if(this.valueField){
\r
913 var r = this.findRecord(this.valueField, v);
\r
915 text = r.data[this.displayField];
\r
916 }else if(Ext.isDefined(this.valueNotFoundText)){
\r
917 text = this.valueNotFoundText;
\r
920 this.lastSelectionText = text;
\r
921 if(this.hiddenField){
\r
922 this.hiddenField.value = v;
\r
924 Ext.form.ComboBox.superclass.setValue.call(this, text);
\r
930 findRecord : function(prop, value){
\r
932 if(this.store.getCount() > 0){
\r
933 this.store.each(function(r){
\r
934 if(r.data[prop] == value){
\r
944 onViewMove : function(e, t){
\r
945 this.inKeyMode = false;
\r
949 onViewOver : function(e, t){
\r
950 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
\r
953 var item = this.view.findItemFromChild(t);
\r
955 var index = this.view.indexOf(item);
\r
956 this.select(index, false);
\r
961 onViewClick : function(doFocus){
\r
962 var index = this.view.getSelectedIndexes()[0],
\r
964 r = s.getAt(index);
\r
966 this.onSelect(r, index);
\r
967 }else if(s.getCount() === 0){
\r
968 this.onEmptyResults();
\r
970 if(doFocus !== false){
\r
976 restrictHeight : function(){
\r
977 this.innerList.dom.style.height = '';
\r
978 var inner = this.innerList.dom,
\r
979 pad = this.list.getFrameWidth('tb') + (this.resizable ? this.handleHeight : 0) + this.assetHeight,
\r
980 h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight),
\r
981 ha = this.getPosition()[1]-Ext.getBody().getScroll().top,
\r
982 hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height,
\r
983 space = Math.max(ha, hb, this.minHeight || 0)-this.list.shadowOffset-pad-5;
\r
985 h = Math.min(h, space, this.maxHeight);
\r
987 this.innerList.setHeight(h);
\r
988 this.list.beginUpdate();
\r
989 this.list.setHeight(h+pad);
\r
990 this.list.alignTo(this.wrap, this.listAlign);
\r
991 this.list.endUpdate();
\r
995 onEmptyResults : function(){
\r
1000 * Returns true if the dropdown list is expanded, else false.
\r
1002 isExpanded : function(){
\r
1003 return this.list && this.list.isVisible();
\r
1007 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
\r
1008 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
\r
1009 * @param {String} value The data value of the item to select
\r
1010 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
\r
1011 * selected item if it is not currently in view (defaults to true)
\r
1012 * @return {Boolean} True if the value matched an item in the list, else false
\r
1014 selectByValue : function(v, scrollIntoView){
\r
1015 if(!Ext.isEmpty(v, true)){
\r
1016 var r = this.findRecord(this.valueField || this.displayField, v);
\r
1018 this.select(this.store.indexOf(r), scrollIntoView);
\r
1026 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
\r
1027 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
\r
1028 * @param {Number} index The zero-based index of the list item to select
\r
1029 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
\r
1030 * selected item if it is not currently in view (defaults to true)
\r
1032 select : function(index, scrollIntoView){
\r
1033 this.selectedIndex = index;
\r
1034 this.view.select(index);
\r
1035 if(scrollIntoView !== false){
\r
1036 var el = this.view.getNode(index);
\r
1038 this.innerList.scrollChildIntoView(el, false);
\r
1044 selectNext : function(){
\r
1045 var ct = this.store.getCount();
\r
1047 if(this.selectedIndex == -1){
\r
1049 }else if(this.selectedIndex < ct-1){
\r
1050 this.select(this.selectedIndex+1);
\r
1056 selectPrev : function(){
\r
1057 var ct = this.store.getCount();
\r
1059 if(this.selectedIndex == -1){
\r
1061 }else if(this.selectedIndex !== 0){
\r
1062 this.select(this.selectedIndex-1);
\r
1068 onKeyUp : function(e){
\r
1069 var k = e.getKey();
\r
1070 if(this.editable !== false && (k == e.BACKSPACE || !e.isSpecialKey())){
\r
1072 this.dqTask.delay(this.queryDelay);
\r
1074 Ext.form.ComboBox.superclass.onKeyUp.call(this, e);
\r
1078 validateBlur : function(){
\r
1079 return !this.list || !this.list.isVisible();
\r
1083 initQuery : function(){
\r
1084 this.doQuery(this.getRawValue());
\r
1088 beforeBlur : function(){
\r
1089 var val = this.getRawValue(),
\r
1090 rec = this.findRecord(this.displayField, val);
\r
1091 if(!rec && this.forceSelection){
\r
1092 if(val.length > 0 && val != this.emptyText){
\r
1093 this.el.dom.value = Ext.isDefined(this.lastSelectionText) ? this.lastSelectionText : '';
\r
1094 this.applyEmptyText();
\r
1096 this.clearValue();
\r
1100 val = rec.get(this.valueField || this.displayField);
\r
1102 this.setValue(val);
\r
1107 * Execute a query to filter the dropdown list. Fires the {@link #beforequery} event prior to performing the
\r
1108 * query allowing the query action to be canceled if needed.
\r
1109 * @param {String} query The SQL query to execute
\r
1110 * @param {Boolean} forceAll <tt>true</tt> to force the query to execute even if there are currently fewer
\r
1111 * characters in the field than the minimum specified by the <tt>{@link #minChars}</tt> config option. It
\r
1112 * also clears any filter previously saved in the current store (defaults to <tt>false</tt>)
\r
1114 doQuery : function(q, forceAll){
\r
1115 q = Ext.isEmpty(q) ? '' : q;
\r
1118 forceAll: forceAll,
\r
1122 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
\r
1126 forceAll = qe.forceAll;
\r
1127 if(forceAll === true || (q.length >= this.minChars)){
\r
1128 if(this.lastQuery !== q){
\r
1129 this.lastQuery = q;
\r
1130 if(this.mode == 'local'){
\r
1131 this.selectedIndex = -1;
\r
1133 this.store.clearFilter();
\r
1135 this.store.filter(this.displayField, q);
\r
1139 this.store.baseParams[this.queryParam] = q;
\r
1141 params: this.getParams(q)
\r
1146 this.selectedIndex = -1;
\r
1153 getParams : function(q){
\r
1155 //p[this.queryParam] = q;
\r
1156 if(this.pageSize){
\r
1158 p.limit = this.pageSize;
\r
1164 * Hides the dropdown list if it is currently expanded. Fires the {@link #collapse} event on completion.
\r
1166 collapse : function(){
\r
1167 if(!this.isExpanded()){
\r
1171 Ext.getDoc().un('mousewheel', this.collapseIf, this);
\r
1172 Ext.getDoc().un('mousedown', this.collapseIf, this);
\r
1173 this.fireEvent('collapse', this);
\r
1177 collapseIf : function(e){
\r
1178 if(!e.within(this.wrap) && !e.within(this.list)){
\r
1184 * Expands the dropdown list if it is currently hidden. Fires the {@link #expand} event on completion.
\r
1186 expand : function(){
\r
1187 if(this.isExpanded() || !this.hasFocus){
\r
1190 if(this.bufferSize){
\r
1191 this.doResize(this.bufferSize);
\r
1192 delete this.bufferSize;
\r
1194 this.list.alignTo(this.wrap, this.listAlign);
\r
1197 this.innerList.setOverflow('auto'); // necessary for FF 2.0/Mac
\r
1201 mousewheel: this.collapseIf,
\r
1202 mousedown: this.collapseIf
\r
1204 this.fireEvent('expand', this);
\r
1208 * @method onTriggerClick
\r
1212 // Implements the default empty TriggerField.onTriggerClick function
\r
1213 onTriggerClick : function(){
\r
1214 if(this.disabled){
\r
1217 if(this.isExpanded()){
\r
1222 if(this.triggerAction == 'all') {
\r
1223 this.doQuery(this.allQuery, true);
\r
1225 this.doQuery(this.getRawValue());
\r
1233 * @method autoSize
\r
1236 * @cfg {Boolean} grow @hide
\r
1239 * @cfg {Number} growMin @hide
\r
1242 * @cfg {Number} growMax @hide
\r
1246 Ext.reg('combo', Ext.form.ComboBox);