7 * @class Roo.bootstrap.ComboBox
8 * @extends Roo.bootstrap.TriggerField
9 * A combobox control with support for autocomplete, remote-loading, paging and many other features.
10 * @cfg {Boolean} append (true|false) default false
12 * Create a new ComboBox.
13 * @param {Object} config Configuration options
15 Roo.bootstrap.ComboBox = function(config){
16 Roo.bootstrap.ComboBox.superclass.constructor.call(this, config);
20 * Fires when the dropdown list is expanded
21 * @param {Roo.bootstrap.ComboBox} combo This combo box
26 * Fires when the dropdown list is collapsed
27 * @param {Roo.bootstrap.ComboBox} combo This combo box
32 * Fires before a list item is selected. Return false to cancel the selection.
33 * @param {Roo.bootstrap.ComboBox} combo This combo box
34 * @param {Roo.data.Record} record The data record returned from the underlying store
35 * @param {Number} index The index of the selected item in the dropdown list
37 'beforeselect' : true,
40 * Fires when a list item is selected
41 * @param {Roo.bootstrap.ComboBox} combo This combo box
42 * @param {Roo.data.Record} record The data record returned from the underlying store (or false on clear)
43 * @param {Number} index The index of the selected item in the dropdown list
48 * Fires before all queries are processed. Return false to cancel the query or set cancel to true.
49 * The event object passed has these properties:
50 * @param {Roo.bootstrap.ComboBox} combo This combo box
51 * @param {String} query The query
52 * @param {Boolean} forceAll true to force "all" query
53 * @param {Boolean} cancel true to cancel the query
54 * @param {Object} e The query event object
59 * Fires when the 'add' icon is pressed (add a listener to enable add button)
60 * @param {Roo.bootstrap.ComboBox} combo This combo box
65 * Fires when the 'edit' icon is pressed (add a listener to enable add button)
66 * @param {Roo.bootstrap.ComboBox} combo This combo box
67 * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
72 * Fires when the remove value from the combobox array
73 * @param {Roo.bootstrap.ComboBox} combo This combo box
80 this.selectedIndex = -1;
81 if(this.mode == 'local'){
82 if(config.queryDelay === undefined){
85 if(config.minChars === undefined){
91 Roo.extend(Roo.bootstrap.ComboBox, Roo.bootstrap.TriggerField, {
94 * @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
95 * rendering into an Roo.Editor, defaults to false)
98 * @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
99 * {tag: "input", type: "text", size: "24", autocomplete: "off"})
102 * @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
105 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
106 * the dropdown list (defaults to undefined, with no header element)
110 * @cfg {String/Roo.Template} tpl The template to use to render the output
114 * @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
116 listWidth: undefined,
118 * @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
119 * mode = 'remote' or 'text' if mode = 'local')
121 displayField: undefined,
123 * @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
124 * mode = 'remote' or 'value' if mode = 'local').
125 * Note: use of a valueField requires the user make a selection
126 * in order for a value to be mapped.
128 valueField: undefined,
132 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
133 * field's data value (defaults to the underlying DOM element's name)
135 hiddenName: undefined,
137 * @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
141 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
143 selectedClass: 'active',
146 * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
150 * @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
151 * anchor positions (defaults to 'tl-bl')
155 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
159 * @cfg {String} triggerAction The action to execute when the trigger field is activated. Use 'all' to run the
160 * query specified by the allQuery config option (defaults to 'query')
162 triggerAction: 'query',
164 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
165 * (defaults to 4, does not apply if editable = false)
169 * @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
170 * delay (typeAheadDelay) if it matches a known value (defaults to false)
174 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
175 * query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
179 * @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
180 * filter queries will execute with page start and limit parameters. Only applies when mode = 'remote' (defaults to 0)
184 * @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus. Only applies
185 * when editable = true (defaults to false)
189 * @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
193 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
194 * when mode = 'remote' (defaults to 'Loading...')
196 loadingText: 'Loading...',
198 * @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
202 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
206 * @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
207 * traditional select (defaults to true)
211 * @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
215 * @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
219 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
220 * listWidth has a higher value)
224 * @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
225 * allow the user to set arbitrary text into the field (defaults to false)
227 forceSelection:false,
229 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
230 * if typeAhead = true (defaults to 250)
232 typeAheadDelay : 250,
234 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
235 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
237 valueNotFoundText : undefined,
239 * @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
244 * @cfg {Boolean} disableClear Disable showing of clear button.
246 disableClear : false,
248 * @cfg {Boolean} alwaysQuery Disable caching of results, and always send query
253 * @cfg {Boolean} multiple (true|false) ComboBobArray, default false
267 // element that contains real text value.. (when hidden is used..)
270 initEvents: function(){
273 throw "can not find store for combo";
275 this.store = Roo.factory(this.store, Roo.data);
279 Roo.bootstrap.ComboBox.superclass.initEvents.call(this);
284 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
286 this.hiddenField.dom.value =
287 this.hiddenValue !== undefined ? this.hiddenValue :
288 this.value !== undefined ? this.value : '';
290 // prevent input submission
291 this.el.dom.removeAttribute('name');
292 this.hiddenField.dom.setAttribute('name', this.hiddenName);
297 // this.el.dom.setAttribute('autocomplete', 'off');
300 var cls = 'x-combo-list';
301 this.list = this.el.select('ul.dropdown-menu',true).first();
303 //this.list = new Roo.Layer({
304 // shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
307 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
308 this.list.setWidth(lw);
310 this.list.on('mouseover', this.onViewOver, this);
311 this.list.on('mousemove', this.onViewMove, this);
313 this.list.on('scroll', this.onViewScroll, this);
316 this.list.swallowEvent('mousewheel');
317 this.assetHeight = 0;
320 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
321 this.assetHeight += this.header.getHeight();
324 this.innerList = this.list.createChild({cls:cls+'-inner'});
325 this.innerList.on('mouseover', this.onViewOver, this);
326 this.innerList.on('mousemove', this.onViewMove, this);
327 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
329 if(this.allowBlank && !this.pageSize && !this.disableClear){
330 this.footer = this.list.createChild({cls:cls+'-ft'});
331 this.pageTb = new Roo.Toolbar(this.footer);
335 this.footer = this.list.createChild({cls:cls+'-ft'});
336 this.pageTb = new Roo.PagingToolbar(this.footer, this.store,
337 {pageSize: this.pageSize});
341 if (this.pageTb && this.allowBlank && !this.disableClear) {
343 this.pageTb.add(new Roo.Toolbar.Fill(), {
344 cls: 'x-btn-icon x-btn-clear',
350 _this.onSelect(false, -1);
355 this.assetHeight += this.footer.getHeight();
360 this.tpl = '<li><a href="#">{' + this.displayField + '}</a></li>';
363 this.view = new Roo.View(this.el.select('ul.dropdown-menu',true).first(), this.tpl, {
364 singleSelect:true, store: this.store, selectedClass: this.selectedClass
366 //this.view.wrapEl.setDisplayed(false);
367 this.view.on('click', this.onViewClick, this);
371 this.store.on('beforeload', this.onBeforeLoad, this);
372 this.store.on('load', this.onLoad, this);
373 this.store.on('loadexception', this.onLoadException, this);
376 this.resizer = new Roo.Resizable(this.list, {
377 pinned:true, handles:'se'
379 this.resizer.on('resize', function(r, w, h){
380 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
382 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
383 this.restrictHeight();
385 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
389 this.editable = true;
390 this.setEditable(false);
395 if (typeof(this.events.add.listeners) != 'undefined') {
397 this.addicon = this.wrap.createChild(
398 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-add' });
400 this.addicon.on('click', function(e) {
401 this.fireEvent('add', this);
404 if (typeof(this.events.edit.listeners) != 'undefined') {
406 this.editicon = this.wrap.createChild(
407 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-edit' });
409 this.editicon.setStyle('margin-left', '40px');
411 this.editicon.on('click', function(e) {
413 // we fire even if inothing is selected..
414 this.fireEvent('edit', this, this.lastData );
420 this.keyNav = new Roo.KeyNav(this.inputEl(), {
422 this.inKeyMode = true;
426 "down" : function(e){
427 if(!this.isExpanded()){
428 this.onTriggerClick();
430 this.inKeyMode = true;
435 "enter" : function(e){
447 if(this.fireEvent("specialkey", this, e)){
448 this.onViewClick(false);
456 doRelay : function(foo, bar, hname){
457 if(hname == 'down' || this.scope.isExpanded()){
458 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
467 this.queryDelay = Math.max(this.queryDelay || 10,
468 this.mode == 'local' ? 10 : 250);
471 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
474 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
476 if(this.editable !== false){
477 this.inputEl().on("keyup", this.onKeyUp, this);
479 if(this.forceSelection){
480 this.inputEl().on('blur', this.doForce, this);
484 this.choices = this.el.select('ul.select2-choices', true).first();
485 this.searchField = this.el.select('ul li.select2-search-field', true).first();
489 onDestroy : function(){
491 this.view.setStore(null);
492 this.view.el.removeAllListeners();
493 this.view.el.remove();
494 this.view.purgeListeners();
497 this.list.dom.innerHTML = '';
500 this.store.un('beforeload', this.onBeforeLoad, this);
501 this.store.un('load', this.onLoad, this);
502 this.store.un('loadexception', this.onLoadException, this);
504 Roo.bootstrap.ComboBox.superclass.onDestroy.call(this);
508 fireKey : function(e){
509 if(e.isNavKeyPress() && !this.list.isVisible()){
510 this.fireEvent("specialkey", this, e);
515 onResize: function(w, h){
516 // Roo.bootstrap.ComboBox.superclass.onResize.apply(this, arguments);
518 // if(typeof w != 'number'){
519 // // we do not handle it!?!?
522 // var tw = this.trigger.getWidth();
523 // // tw += this.addicon ? this.addicon.getWidth() : 0;
524 // // tw += this.editicon ? this.editicon.getWidth() : 0;
526 // this.inputEl().setWidth( this.adjustWidth('input', x));
528 // //this.trigger.setStyle('left', x+'px');
530 // if(this.list && this.listWidth === undefined){
531 // var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
532 // this.list.setWidth(lw);
533 // this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
541 * Allow or prevent the user from directly editing the field text. If false is passed,
542 * the user will only be able to select from the items defined in the dropdown list. This method
543 * is the runtime equivalent of setting the 'editable' config option at config time.
544 * @param {Boolean} value True to allow the user to directly edit the field text
546 setEditable : function(value){
547 if(value == this.editable){
550 this.editable = value;
552 this.inputEl().dom.setAttribute('readOnly', true);
553 this.inputEl().on('mousedown', this.onTriggerClick, this);
554 this.inputEl().addClass('x-combo-noedit');
556 this.inputEl().dom.setAttribute('readOnly', false);
557 this.inputEl().un('mousedown', this.onTriggerClick, this);
558 this.inputEl().removeClass('x-combo-noedit');
564 onBeforeLoad : function(combo,opts){
569 this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
571 this.restrictHeight();
572 this.selectedIndex = -1;
578 this.hasQuery = false;
584 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
588 if(this.store.getCount() > 0){
590 this.restrictHeight();
591 if(this.lastQuery == this.allQuery){
593 this.inputEl().dom.select();
595 if(!this.selectByValue(this.value, true)){
596 this.select(0, true);
600 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
601 this.taTask.delay(this.typeAheadDelay);
605 this.onEmptyResults();
611 onLoadException : function()
613 this.hasQuery = false;
615 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
620 Roo.log(this.store.reader.jsonData);
621 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
623 //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
629 onTypeAhead : function(){
630 if(this.store.getCount() > 0){
631 var r = this.store.getAt(0);
632 var newValue = r.data[this.displayField];
633 var len = newValue.length;
634 var selStart = this.getRawValue().length;
637 this.setRawValue(newValue);
638 this.selectText(selStart, newValue.length);
644 onSelect : function(record, index){
646 if(this.fireEvent('beforeselect', this, record, index) !== false){
648 this.setFromData(index > -1 ? record.data : false);
651 this.fireEvent('select', this, record, index);
656 * Returns the currently selected field value or empty string if no value is set.
657 * @return {String} value The selected value
659 getValue : function(){
662 return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
666 return typeof this.value != 'undefined' ? this.value : '';
668 return Roo.bootstrap.ComboBox.superclass.getValue.call(this);
673 * Clears any text/value currently set in the field
675 clearValue : function(){
676 if(this.hiddenField){
677 this.hiddenField.dom.value = '';
680 this.setRawValue('');
681 this.lastSelectionText = '';
686 * Sets the specified value into the field. If the value finds a match, the corresponding record text
687 * will be displayed in the field. If the value does not match the data value of an existing item,
688 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
689 * Otherwise the field will be blank (although the value will still be set).
690 * @param {String} value The value to match
692 setValue : function(v){
700 var r = this.findRecord(this.valueField, v);
702 text = r.data[this.displayField];
703 }else if(this.valueNotFoundText !== undefined){
704 text = this.valueNotFoundText;
707 this.lastSelectionText = text;
708 if(this.hiddenField){
709 this.hiddenField.dom.value = v;
711 Roo.bootstrap.ComboBox.superclass.setValue.call(this, text);
715 * @property {Object} the last set data for the element
720 * Sets the value of the field based on a object which is related to the record format for the store.
721 * @param {Object} value the value to set as. or false on reset?
723 setFromData : function(o){
730 var dv = ''; // display value
731 var vv = ''; // value value..
733 if (this.displayField) {
734 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
736 // this is an error condition!!!
737 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
741 vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
744 if(this.hiddenField){
745 this.hiddenField.dom.value = vv;
747 this.lastSelectionText = dv;
748 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
752 // no hidden field.. - we store the value in 'value', but still display
754 this.lastSelectionText = dv;
755 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
762 // overridden so that last data is reset..
763 this.setValue(this.originalValue);
765 this.lastData = false;
767 this.view.clearSelections();
771 findRecord : function(prop, value){
773 if(this.store.getCount() > 0){
774 this.store.each(function(r){
775 if(r.data[prop] == value){
787 // returns hidden if it's set..
788 if (!this.rendered) {return ''};
789 return !this.hiddenName && this.inputEl().dom.name ? this.inputEl().dom.name : (this.hiddenName || '');
793 onViewMove : function(e, t){
794 this.inKeyMode = false;
798 onViewOver : function(e, t){
799 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
802 var item = this.view.findItemFromChild(t);
804 var index = this.view.indexOf(item);
805 this.select(index, false);
810 onViewClick : function(doFocus)
812 var index = this.view.getSelectedIndexes()[0];
813 var r = this.store.getAt(index);
815 this.onSelect(r, index);
817 if(doFocus !== false && !this.blockFocus){
818 this.inputEl().focus();
823 restrictHeight : function(){
824 //this.innerList.dom.style.height = '';
825 //var inner = this.innerList.dom;
826 //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
827 //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
828 //this.list.beginUpdate();
829 //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
830 this.list.alignTo(this.inputEl(), this.listAlign);
831 //this.list.endUpdate();
835 onEmptyResults : function(){
840 * Returns true if the dropdown list is expanded, else false.
842 isExpanded : function(){
843 return this.list.isVisible();
847 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
848 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
849 * @param {String} value The data value of the item to select
850 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
851 * selected item if it is not currently in view (defaults to true)
852 * @return {Boolean} True if the value matched an item in the list, else false
854 selectByValue : function(v, scrollIntoView){
855 if(v !== undefined && v !== null){
856 var r = this.findRecord(this.valueField || this.displayField, v);
858 this.select(this.store.indexOf(r), scrollIntoView);
866 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
867 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
868 * @param {Number} index The zero-based index of the list item to select
869 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
870 * selected item if it is not currently in view (defaults to true)
872 select : function(index, scrollIntoView){
873 this.selectedIndex = index;
874 this.view.select(index);
875 if(scrollIntoView !== false){
876 var el = this.view.getNode(index);
878 //this.innerList.scrollChildIntoView(el, false);
885 selectNext : function(){
886 var ct = this.store.getCount();
888 if(this.selectedIndex == -1){
890 }else if(this.selectedIndex < ct-1){
891 this.select(this.selectedIndex+1);
897 selectPrev : function(){
898 var ct = this.store.getCount();
900 if(this.selectedIndex == -1){
902 }else if(this.selectedIndex != 0){
903 this.select(this.selectedIndex-1);
909 onKeyUp : function(e){
910 if(this.editable !== false && !e.isSpecialKey()){
911 this.lastKey = e.getKey();
912 this.dqTask.delay(this.queryDelay);
917 validateBlur : function(){
918 return !this.list || !this.list.isVisible();
922 initQuery : function(){
923 this.doQuery(this.getRawValue());
927 doForce : function(){
928 if(this.inputEl().dom.value.length > 0){
929 this.inputEl().dom.value =
930 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
936 * Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
937 * query allowing the query action to be canceled if needed.
938 * @param {String} query The SQL query to execute
939 * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
940 * in the field than the minimum specified by the minChars config option. It also clears any filter previously
941 * saved in the current store (defaults to false)
943 doQuery : function(q, forceAll){
945 if(q === undefined || q === null){
954 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
959 forceAll = qe.forceAll;
960 if(forceAll === true || (q.length >= this.minChars)){
962 this.hasQuery = true;
964 if(this.lastQuery != q || this.alwaysQuery){
966 if(this.mode == 'local'){
967 this.selectedIndex = -1;
969 this.store.clearFilter();
971 this.store.filter(this.displayField, q);
975 this.store.baseParams[this.queryParam] = q;
977 var options = {params : this.getParams(q)};
981 options.params.start = this.page * this.pageSize;
984 this.store.load(options);
986 * this code will make the page width larger, at the beginning, the list not align correctly,
987 * we should expand the list on onLoad
993 this.selectedIndex = -1;
998 this.loadNext = false;
1002 getParams : function(q){
1004 //p[this.queryParam] = q;
1008 p.limit = this.pageSize;
1014 * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1016 collapse : function(){
1017 if(!this.isExpanded()){
1022 Roo.get(document).un('mousedown', this.collapseIf, this);
1023 Roo.get(document).un('mousewheel', this.collapseIf, this);
1024 if (!this.editable) {
1025 Roo.get(document).un('keydown', this.listKeyPress, this);
1027 this.fireEvent('collapse', this);
1031 collapseIf : function(e){
1032 var in_combo = e.within(this.el);
1033 var in_list = e.within(this.list);
1035 if (in_combo || in_list) {
1036 //e.stopPropagation();
1045 * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1047 expand : function(){
1049 if(this.isExpanded() || !this.hasFocus){
1053 this.list.alignTo(this.inputEl(), this.listAlign);
1055 Roo.get(document).on('mousedown', this.collapseIf, this);
1056 Roo.get(document).on('mousewheel', this.collapseIf, this);
1057 if (!this.editable) {
1058 Roo.get(document).on('keydown', this.listKeyPress, this);
1061 this.fireEvent('expand', this);
1065 // Implements the default empty TriggerField.onTriggerClick function
1066 onTriggerClick : function()
1068 Roo.log('trigger click');
1075 this.loadNext = false;
1077 if(this.isExpanded()){
1079 if (!this.blockFocus) {
1080 this.inputEl().focus();
1084 this.hasFocus = true;
1085 if(this.triggerAction == 'all') {
1086 this.doQuery(this.allQuery, true);
1088 this.doQuery(this.getRawValue());
1090 if (!this.blockFocus) {
1091 this.inputEl().focus();
1095 listKeyPress : function(e)
1097 //Roo.log('listkeypress');
1098 // scroll to first matching element based on key pres..
1099 if (e.isSpecialKey()) {
1102 var k = String.fromCharCode(e.getKey()).toUpperCase();
1105 var csel = this.view.getSelectedNodes();
1106 var cselitem = false;
1108 var ix = this.view.indexOf(csel[0]);
1109 cselitem = this.store.getAt(ix);
1110 if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1116 this.store.each(function(v) {
1118 // start at existing selection.
1119 if (cselitem.id == v.id) {
1125 if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1126 match = this.store.indexOf(v);
1132 if (match === false) {
1133 return true; // no more action?
1136 this.view.select(match);
1137 var sn = Roo.get(this.view.getSelectedNodes()[0])
1138 //sn.scrollIntoView(sn.dom.parentNode, false);
1141 onViewScroll : function(e, t){
1143 if(this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1147 this.hasQuery = true;
1149 this.loading = this.list.select('.loading', true).first();
1151 if(this.loading === null){
1152 this.list.createChild({
1154 cls: 'loading select2-more-results select2-active',
1155 html: 'Loading more results...'
1158 this.loading = this.list.select('.loading', true).first();
1160 this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1162 this.loading.hide();
1165 this.loading.show();
1170 this.loadNext = true;
1172 (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1177 addItem : function(o)
1179 var dv = ''; // display value
1181 if (this.displayField) {
1182 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1184 // this is an error condition!!!
1185 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1192 var choice = this.choices.createChild({
1194 cls: 'select2-search-choice',
1203 cls: 'select2-search-choice-close',
1208 }, this.searchField);
1210 var close = choice.select('a.select2-search-choice-close', true).first()
1212 close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
1219 this.inputEl().dom.value = '';
1223 onRemoveItem : function(e, _self, o)
1226 var index = this.item.indexOf(o.data) * 1;
1229 Roo.log('not this item?!');
1233 this.item.splice(index, 1);
1238 this.fireEvent('remove', this, e);
1242 syncValue : function()
1244 if(!this.item.length){
1251 Roo.each(this.item, function(i){
1252 if(_this.valueField){
1253 value.push(i[_this.valueField]);
1260 this.value = value.join(',');
1262 if(this.hiddenField){
1263 this.hiddenField.dom.value = this.value;
1267 clearItem : function()
1275 Roo.each(this.choices.select('>li.select2-search-choice', true).elements, function(c){
1285 * @cfg {Boolean} grow
1289 * @cfg {Number} growMin
1293 * @cfg {Number} growMax