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
11 * @cfg {Boolean} autoFocus (true|false) auto focus the first item, default true
12 * @cfg {Boolean} tickable ComboBox with tickable selections (true|false), default false
13 * @cfg {String} btnPosition set the position of the trigger button (left | right) default right
15 * Create a new ComboBox.
16 * @param {Object} config Configuration options
18 Roo.bootstrap.ComboBox = function(config){
19 Roo.bootstrap.ComboBox.superclass.constructor.call(this, config);
23 * Fires when the dropdown list is expanded
24 * @param {Roo.bootstrap.ComboBox} combo This combo box
29 * Fires when the dropdown list is collapsed
30 * @param {Roo.bootstrap.ComboBox} combo This combo box
35 * Fires before a list item is selected. Return false to cancel the selection.
36 * @param {Roo.bootstrap.ComboBox} combo This combo box
37 * @param {Roo.data.Record} record The data record returned from the underlying store
38 * @param {Number} index The index of the selected item in the dropdown list
40 'beforeselect' : true,
43 * Fires when a list item is selected
44 * @param {Roo.bootstrap.ComboBox} combo This combo box
45 * @param {Roo.data.Record} record The data record returned from the underlying store (or false on clear)
46 * @param {Number} index The index of the selected item in the dropdown list
51 * Fires before all queries are processed. Return false to cancel the query or set cancel to true.
52 * The event object passed has these properties:
53 * @param {Roo.bootstrap.ComboBox} combo This combo box
54 * @param {String} query The query
55 * @param {Boolean} forceAll true to force "all" query
56 * @param {Boolean} cancel true to cancel the query
57 * @param {Object} e The query event object
62 * Fires when the 'add' icon is pressed (add a listener to enable add button)
63 * @param {Roo.bootstrap.ComboBox} combo This combo box
68 * Fires when the 'edit' icon is pressed (add a listener to enable add button)
69 * @param {Roo.bootstrap.ComboBox} combo This combo box
70 * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
75 * Fires when the remove value from the combobox array
76 * @param {Roo.bootstrap.ComboBox} combo This combo box
85 this.selectedIndex = -1;
86 if(this.mode == 'local'){
87 if(config.queryDelay === undefined){
90 if(config.minChars === undefined){
96 Roo.extend(Roo.bootstrap.ComboBox, Roo.bootstrap.TriggerField, {
99 * @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
100 * rendering into an Roo.Editor, defaults to false)
103 * @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
104 * {tag: "input", type: "text", size: "24", autocomplete: "off"})
107 * @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
110 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
111 * the dropdown list (defaults to undefined, with no header element)
115 * @cfg {String/Roo.Template} tpl The template to use to render the output
119 * @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
121 listWidth: undefined,
123 * @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
124 * mode = 'remote' or 'text' if mode = 'local')
126 displayField: undefined,
128 * @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
129 * mode = 'remote' or 'value' if mode = 'local').
130 * Note: use of a valueField requires the user make a selection
131 * in order for a value to be mapped.
133 valueField: undefined,
137 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
138 * field's data value (defaults to the underlying DOM element's name)
140 hiddenName: undefined,
142 * @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
146 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
148 selectedClass: 'active',
151 * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
155 * @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
156 * anchor positions (defaults to 'tl-bl')
160 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
164 * @cfg {String} triggerAction The action to execute when the trigger field is activated. Use 'all' to run the
165 * query specified by the allQuery config option (defaults to 'query')
167 triggerAction: 'query',
169 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
170 * (defaults to 4, does not apply if editable = false)
174 * @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
175 * delay (typeAheadDelay) if it matches a known value (defaults to false)
179 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
180 * query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
184 * @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
185 * filter queries will execute with page start and limit parameters. Only applies when mode = 'remote' (defaults to 0)
189 * @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus. Only applies
190 * when editable = true (defaults to false)
194 * @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
198 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
199 * when mode = 'remote' (defaults to 'Loading...')
201 loadingText: 'Loading...',
203 * @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
207 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
211 * @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
212 * traditional select (defaults to true)
216 * @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
220 * @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
224 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
225 * listWidth has a higher value)
229 * @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
230 * allow the user to set arbitrary text into the field (defaults to false)
232 forceSelection:false,
234 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
235 * if typeAhead = true (defaults to 250)
237 typeAheadDelay : 250,
239 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
240 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
242 valueNotFoundText : undefined,
244 * @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
249 * @cfg {Boolean} disableClear Disable showing of clear button.
251 disableClear : false,
253 * @cfg {Boolean} alwaysQuery Disable caching of results, and always send query
258 * @cfg {Boolean} multiple (true|false) ComboBobArray, default false
272 btnPosition : 'right',
274 // element that contains real text value.. (when hidden is used..)
276 getAutoCreate : function()
284 cfg = Roo.bootstrap.ComboBox.superclass.getAutoCreate.call(this);
289 * ComboBox with tickable selections
292 var align = this.labelAlign || this.parentLabelAlign();
295 cls : 'form-group roo-combobox-tickable' //input-group
301 cls : 'tickable-buttons',
306 cls : 'btn btn-link btn-edit pull-' + this.btnPosition,
313 cls : 'btn btn-link btn-ok pull-' + this.btnPosition,
320 cls : 'btn btn-link btn-cancel pull-' + this.btnPosition,
327 Roo.each(buttons.cn, function(c){
329 c.cls += ' btn-' + _this.size;
332 if (_this.disabled) {
343 cls: 'form-hidden-field'
347 cls: 'select2-choices',
351 cls: 'select2-search-field',
363 cls: 'select2-container input-group select2-container-multi',
368 cls: 'typeahead typeahead-long dropdown-menu',
369 style: 'display:none; max-height:' + this.maxHeight + 'px;'
374 if (align ==='left' && this.fieldLabel.length) {
376 Roo.log("left and has label");
382 cls : 'control-label col-sm-' + this.labelWidth,
383 html : this.fieldLabel
387 cls : "col-sm-" + (12 - this.labelWidth),
394 } else if ( this.fieldLabel.length) {
400 //cls : 'input-group-addon',
401 html : this.fieldLabel
411 Roo.log(" no label && no align");
418 ['xs','sm','md','lg'].map(function(size){
419 if (settings[size]) {
420 cfg.cls += ' col-' + size + '-' + settings[size];
429 initEvents: function()
433 throw "can not find store for combo";
435 this.store = Roo.factory(this.store, Roo.data);
438 this.initTickableEvnets();
442 Roo.bootstrap.ComboBox.superclass.initEvents.call(this);
447 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
449 this.hiddenField.dom.value =
450 this.hiddenValue !== undefined ? this.hiddenValue :
451 this.value !== undefined ? this.value : '';
453 // prevent input submission
454 this.el.dom.removeAttribute('name');
455 this.hiddenField.dom.setAttribute('name', this.hiddenName);
460 // this.el.dom.setAttribute('autocomplete', 'off');
463 var cls = 'x-combo-list';
464 this.list = this.el.select('ul.dropdown-menu',true).first();
466 //this.list = new Roo.Layer({
467 // shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
473 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
474 _this.list.setWidth(lw);
477 this.list.on('mouseover', this.onViewOver, this);
478 this.list.on('mousemove', this.onViewMove, this);
480 this.list.on('scroll', this.onViewScroll, this);
483 this.list.swallowEvent('mousewheel');
484 this.assetHeight = 0;
487 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
488 this.assetHeight += this.header.getHeight();
491 this.innerList = this.list.createChild({cls:cls+'-inner'});
492 this.innerList.on('mouseover', this.onViewOver, this);
493 this.innerList.on('mousemove', this.onViewMove, this);
494 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
496 if(this.allowBlank && !this.pageSize && !this.disableClear){
497 this.footer = this.list.createChild({cls:cls+'-ft'});
498 this.pageTb = new Roo.Toolbar(this.footer);
502 this.footer = this.list.createChild({cls:cls+'-ft'});
503 this.pageTb = new Roo.PagingToolbar(this.footer, this.store,
504 {pageSize: this.pageSize});
508 if (this.pageTb && this.allowBlank && !this.disableClear) {
510 this.pageTb.add(new Roo.Toolbar.Fill(), {
511 cls: 'x-btn-icon x-btn-clear',
517 _this.onSelect(false, -1);
522 this.assetHeight += this.footer.getHeight();
527 this.tpl = '<li><a href="#">{' + this.displayField + '}</a></li>';
530 this.view = new Roo.View(this.el.select('ul.dropdown-menu',true).first(), this.tpl, {
531 singleSelect:true, store: this.store, selectedClass: this.selectedClass
533 //this.view.wrapEl.setDisplayed(false);
534 this.view.on('click', this.onViewClick, this);
538 this.store.on('beforeload', this.onBeforeLoad, this);
539 this.store.on('load', this.onLoad, this);
540 this.store.on('loadexception', this.onLoadException, this);
543 this.resizer = new Roo.Resizable(this.list, {
544 pinned:true, handles:'se'
546 this.resizer.on('resize', function(r, w, h){
547 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
549 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
550 this.restrictHeight();
552 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
556 this.editable = true;
557 this.setEditable(false);
562 if (typeof(this.events.add.listeners) != 'undefined') {
564 this.addicon = this.wrap.createChild(
565 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-add' });
567 this.addicon.on('click', function(e) {
568 this.fireEvent('add', this);
571 if (typeof(this.events.edit.listeners) != 'undefined') {
573 this.editicon = this.wrap.createChild(
574 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-edit' });
576 this.editicon.setStyle('margin-left', '40px');
578 this.editicon.on('click', function(e) {
580 // we fire even if inothing is selected..
581 this.fireEvent('edit', this, this.lastData );
587 this.keyNav = new Roo.KeyNav(this.inputEl(), {
589 this.inKeyMode = true;
593 "down" : function(e){
594 if(!this.isExpanded()){
595 this.onTriggerClick();
597 this.inKeyMode = true;
602 "enter" : function(e){
603 // this.onViewClick();
607 if(this.fireEvent("specialkey", this, e)){
608 this.onViewClick(false);
621 if(this.fireEvent("specialkey", this, e)){
622 this.onViewClick(false);
630 doRelay : function(foo, bar, hname){
631 if(hname == 'down' || this.scope.isExpanded()){
632 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
641 this.queryDelay = Math.max(this.queryDelay || 10,
642 this.mode == 'local' ? 10 : 250);
645 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
648 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
650 if(this.editable !== false){
651 this.inputEl().on("keyup", this.onKeyUp, this);
653 if(this.forceSelection){
654 this.inputEl().on('blur', this.doForce, this);
658 this.choices = this.el.select('ul.select2-choices', true).first();
659 this.searchField = this.el.select('ul li.select2-search-field', true).first();
663 initTickableEvnets: function()
667 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
669 this.hiddenField.dom.value =
670 this.hiddenValue !== undefined ? this.hiddenValue :
671 this.value !== undefined ? this.value : '';
673 // prevent input submission
674 this.el.dom.removeAttribute('name');
675 this.hiddenField.dom.setAttribute('name', this.hiddenName);
680 this.list = this.el.select('ul.dropdown-menu',true).first();
682 this.choices = this.el.select('ul.select2-choices', true).first();
683 this.searchField = this.el.select('ul li.select2-search-field', true).first();
684 if(this.editNotList){
685 this.searchField.on("click", this.onSearchFieldClick, this, {preventDefault:true});
688 this.trigger = this.el.select('.tickable-buttons > .btn-edit', true).first();
689 this.trigger.on("click", this.onTickableTriggerClick, this, {preventDefault:true});
691 this.okBtn = this.el.select('.tickable-buttons > .btn-ok', true).first();
692 this.cancelBtn = this.el.select('.tickable-buttons > .btn-cancel', true).first();
694 this.okBtn.on('click', this.onTickableFooterButtonClick, this, this.okBtn);
695 this.cancelBtn.on('click', this.onTickableFooterButtonClick, this, this.cancelBtn);
697 this.trigger.setVisibilityMode(Roo.Element.DISPLAY);
698 this.okBtn.setVisibilityMode(Roo.Element.DISPLAY);
699 this.cancelBtn.setVisibilityMode(Roo.Element.DISPLAY);
702 this.cancelBtn.hide();
707 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
708 _this.list.setWidth(lw);
711 this.list.on('mouseover', this.onViewOver, this);
712 this.list.on('mousemove', this.onViewMove, this);
714 this.list.on('scroll', this.onViewScroll, this);
717 this.tpl = '<li class="select2-result"><div class="checkbox"><input id="{roo-id}" type="checkbox" {roo-data-checked}><label for="{roo-id}"><b>{' + this.displayField + '}</b></label></li>';
720 this.view = new Roo.View(this.el.select('ul.dropdown-menu',true).first(), this.tpl, {
721 singleSelect:true, tickable:true, parent:this, store: this.store, selectedClass: this.selectedClass
724 //this.view.wrapEl.setDisplayed(false);
725 this.view.on('click', this.onViewClick, this);
729 this.store.on('beforeload', this.onBeforeLoad, this);
730 this.store.on('load', this.onLoad, this);
731 this.store.on('loadexception', this.onLoadException, this);
733 // this.keyNav = new Roo.KeyNav(this.inputEl(), {
734 // "up" : function(e){
735 // this.inKeyMode = true;
736 // this.selectPrev();
739 // "down" : function(e){
740 // if(!this.isExpanded()){
741 // this.onTriggerClick();
743 // this.inKeyMode = true;
744 // this.selectNext();
748 // "enter" : function(e){
749 //// this.onViewClick();
753 // if(this.fireEvent("specialkey", this, e)){
754 // this.onViewClick(false);
760 // "esc" : function(e){
764 // "tab" : function(e){
767 // if(this.fireEvent("specialkey", this, e)){
768 // this.onViewClick(false);
776 // doRelay : function(foo, bar, hname){
777 // if(hname == 'down' || this.scope.isExpanded()){
778 // return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
783 // forceKeyDown: true
787 this.queryDelay = Math.max(this.queryDelay || 10,
788 this.mode == 'local' ? 10 : 250);
791 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
794 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
798 onDestroy : function(){
800 this.view.setStore(null);
801 this.view.el.removeAllListeners();
802 this.view.el.remove();
803 this.view.purgeListeners();
806 this.list.dom.innerHTML = '';
810 this.store.un('beforeload', this.onBeforeLoad, this);
811 this.store.un('load', this.onLoad, this);
812 this.store.un('loadexception', this.onLoadException, this);
814 Roo.bootstrap.ComboBox.superclass.onDestroy.call(this);
818 fireKey : function(e){
819 if(e.isNavKeyPress() && !this.list.isVisible()){
820 this.fireEvent("specialkey", this, e);
825 onResize: function(w, h){
826 // Roo.bootstrap.ComboBox.superclass.onResize.apply(this, arguments);
828 // if(typeof w != 'number'){
829 // // we do not handle it!?!?
832 // var tw = this.trigger.getWidth();
833 // // tw += this.addicon ? this.addicon.getWidth() : 0;
834 // // tw += this.editicon ? this.editicon.getWidth() : 0;
836 // this.inputEl().setWidth( this.adjustWidth('input', x));
838 // //this.trigger.setStyle('left', x+'px');
840 // if(this.list && this.listWidth === undefined){
841 // var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
842 // this.list.setWidth(lw);
843 // this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
851 * Allow or prevent the user from directly editing the field text. If false is passed,
852 * the user will only be able to select from the items defined in the dropdown list. This method
853 * is the runtime equivalent of setting the 'editable' config option at config time.
854 * @param {Boolean} value True to allow the user to directly edit the field text
856 setEditable : function(value){
857 if(value == this.editable){
860 this.editable = value;
862 this.inputEl().dom.setAttribute('readOnly', true);
863 this.inputEl().on('mousedown', this.onTriggerClick, this);
864 this.inputEl().addClass('x-combo-noedit');
866 this.inputEl().dom.setAttribute('readOnly', false);
867 this.inputEl().un('mousedown', this.onTriggerClick, this);
868 this.inputEl().removeClass('x-combo-noedit');
874 onBeforeLoad : function(combo,opts){
879 this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
881 this.restrictHeight();
882 this.selectedIndex = -1;
888 this.hasQuery = false;
894 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
898 if(this.store.getCount() > 0){
900 this.restrictHeight();
901 if(this.lastQuery == this.allQuery){
902 if(this.editable && !this.tickable){
903 this.inputEl().dom.select();
905 if(!this.selectByValue(this.value, true) && this.autoFocus){
906 this.select(0, true);
912 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
913 this.taTask.delay(this.typeAheadDelay);
917 this.onEmptyResults();
923 onLoadException : function()
925 this.hasQuery = false;
927 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
932 Roo.log(this.store.reader.jsonData);
933 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
935 //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
941 onTypeAhead : function(){
942 if(this.store.getCount() > 0){
943 var r = this.store.getAt(0);
944 var newValue = r.data[this.displayField];
945 var len = newValue.length;
946 var selStart = this.getRawValue().length;
949 this.setRawValue(newValue);
950 this.selectText(selStart, newValue.length);
956 onSelect : function(record, index){
958 if(this.fireEvent('beforeselect', this, record, index) !== false){
960 this.setFromData(index > -1 ? record.data : false);
963 this.fireEvent('select', this, record, index);
968 * Returns the currently selected field value or empty string if no value is set.
969 * @return {String} value The selected value
971 getValue : function(){
974 return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
978 return typeof this.value != 'undefined' ? this.value : '';
980 return Roo.bootstrap.ComboBox.superclass.getValue.call(this);
985 * Clears any text/value currently set in the field
987 clearValue : function(){
988 if(this.hiddenField){
989 this.hiddenField.dom.value = '';
992 this.setRawValue('');
993 this.lastSelectionText = '';
998 * Sets the specified value into the field. If the value finds a match, the corresponding record text
999 * will be displayed in the field. If the value does not match the data value of an existing item,
1000 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
1001 * Otherwise the field will be blank (although the value will still be set).
1002 * @param {String} value The value to match
1004 setValue : function(v){
1011 if(this.valueField){
1012 var r = this.findRecord(this.valueField, v);
1014 text = r.data[this.displayField];
1015 }else if(this.valueNotFoundText !== undefined){
1016 text = this.valueNotFoundText;
1019 this.lastSelectionText = text;
1020 if(this.hiddenField){
1021 this.hiddenField.dom.value = v;
1023 Roo.bootstrap.ComboBox.superclass.setValue.call(this, text);
1027 * @property {Object} the last set data for the element
1032 * Sets the value of the field based on a object which is related to the record format for the store.
1033 * @param {Object} value the value to set as. or false on reset?
1035 setFromData : function(o){
1042 var dv = ''; // display value
1043 var vv = ''; // value value..
1045 if (this.displayField) {
1046 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1048 // this is an error condition!!!
1049 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1052 if(this.valueField){
1053 vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
1056 if(this.hiddenField){
1057 this.hiddenField.dom.value = vv;
1059 this.lastSelectionText = dv;
1060 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1064 // no hidden field.. - we store the value in 'value', but still display
1065 // display field!!!!
1066 this.lastSelectionText = dv;
1067 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1074 // overridden so that last data is reset..
1075 this.setValue(this.originalValue);
1076 this.clearInvalid();
1077 this.lastData = false;
1079 this.view.clearSelections();
1083 findRecord : function(prop, value){
1085 if(this.store.getCount() > 0){
1086 this.store.each(function(r){
1087 if(r.data[prop] == value){
1099 // returns hidden if it's set..
1100 if (!this.rendered) {return ''};
1101 return !this.hiddenName && this.inputEl().dom.name ? this.inputEl().dom.name : (this.hiddenName || '');
1105 onViewMove : function(e, t){
1106 this.inKeyMode = false;
1110 onViewOver : function(e, t){
1111 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1114 var item = this.view.findItemFromChild(t);
1117 var index = this.view.indexOf(item);
1118 this.select(index, false);
1123 onViewClick : function(view, doFocus, el, e)
1125 var index = this.view.getSelectedIndexes()[0];
1127 var r = this.store.getAt(index);
1131 if(e.getTarget().nodeName.toLowerCase() != 'input'){
1138 Roo.each(this.tickItems, function(v,k){
1140 if(typeof(v) != 'undefined' && v[_this.valueField] == r.data[_this.valueField]){
1141 _this.tickItems.splice(k, 1);
1151 this.tickItems.push(r.data);
1156 this.onSelect(r, index);
1158 if(doFocus !== false && !this.blockFocus){
1159 this.inputEl().focus();
1164 restrictHeight : function(){
1165 //this.innerList.dom.style.height = '';
1166 //var inner = this.innerList.dom;
1167 //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
1168 //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
1169 //this.list.beginUpdate();
1170 //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
1171 this.list.alignTo(this.inputEl(), this.listAlign);
1172 //this.list.endUpdate();
1176 onEmptyResults : function(){
1181 * Returns true if the dropdown list is expanded, else false.
1183 isExpanded : function(){
1184 return this.list.isVisible();
1188 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1189 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1190 * @param {String} value The data value of the item to select
1191 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1192 * selected item if it is not currently in view (defaults to true)
1193 * @return {Boolean} True if the value matched an item in the list, else false
1195 selectByValue : function(v, scrollIntoView){
1196 if(v !== undefined && v !== null){
1197 var r = this.findRecord(this.valueField || this.displayField, v);
1199 this.select(this.store.indexOf(r), scrollIntoView);
1207 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1208 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1209 * @param {Number} index The zero-based index of the list item to select
1210 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1211 * selected item if it is not currently in view (defaults to true)
1213 select : function(index, scrollIntoView){
1214 this.selectedIndex = index;
1215 this.view.select(index);
1216 if(scrollIntoView !== false){
1217 var el = this.view.getNode(index);
1219 //this.innerList.scrollChildIntoView(el, false);
1226 selectNext : function(){
1227 var ct = this.store.getCount();
1229 if(this.selectedIndex == -1){
1231 }else if(this.selectedIndex < ct-1){
1232 this.select(this.selectedIndex+1);
1238 selectPrev : function(){
1239 var ct = this.store.getCount();
1241 if(this.selectedIndex == -1){
1243 }else if(this.selectedIndex != 0){
1244 this.select(this.selectedIndex-1);
1250 onKeyUp : function(e){
1251 if(this.editable !== false && !e.isSpecialKey()){
1252 this.lastKey = e.getKey();
1253 this.dqTask.delay(this.queryDelay);
1258 validateBlur : function(){
1259 return !this.list || !this.list.isVisible();
1263 initQuery : function(){
1264 this.doQuery(this.getRawValue());
1268 doForce : function(){
1269 if(this.inputEl().dom.value.length > 0){
1270 this.inputEl().dom.value =
1271 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
1277 * Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
1278 * query allowing the query action to be canceled if needed.
1279 * @param {String} query The SQL query to execute
1280 * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
1281 * in the field than the minimum specified by the minChars config option. It also clears any filter previously
1282 * saved in the current store (defaults to false)
1284 doQuery : function(q, forceAll){
1286 if(q === undefined || q === null){
1295 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1300 forceAll = qe.forceAll;
1301 if(forceAll === true || (q.length >= this.minChars)){
1303 this.hasQuery = true;
1305 if(this.lastQuery != q || this.alwaysQuery){
1307 if(this.mode == 'local'){
1308 this.selectedIndex = -1;
1310 this.store.clearFilter();
1312 this.store.filter(this.displayField, q);
1316 this.store.baseParams[this.queryParam] = q;
1318 var options = {params : this.getParams(q)};
1322 options.params.start = this.page * this.pageSize;
1325 this.store.load(options);
1327 * this code will make the page width larger, at the beginning, the list not align correctly,
1328 * we should expand the list on onLoad
1334 this.selectedIndex = -1;
1339 this.loadNext = false;
1343 getParams : function(q){
1345 //p[this.queryParam] = q;
1349 p.limit = this.pageSize;
1355 * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1357 collapse : function(){
1358 if(!this.isExpanded()){
1362 this.hasFocus = false;
1368 this.cancelBtn.hide();
1369 this.trigger.show();
1372 Roo.get(document).un('mousedown', this.collapseIf, this);
1373 Roo.get(document).un('mousewheel', this.collapseIf, this);
1374 if (!this.editable) {
1375 Roo.get(document).un('keydown', this.listKeyPress, this);
1377 this.fireEvent('collapse', this);
1381 collapseIf : function(e){
1382 var in_combo = e.within(this.el);
1383 var in_list = e.within(this.list);
1385 if (in_combo || in_list) {
1386 //e.stopPropagation();
1391 this.onTickableFooterButtonClick(e, false, false);
1399 * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1401 expand : function(){
1403 if(this.isExpanded() || !this.hasFocus){
1407 this.list.alignTo(this.inputEl(), this.listAlign);
1412 this.tickItems = Roo.apply([], this.item);
1415 this.cancelBtn.show();
1416 this.trigger.hide();
1420 Roo.get(document).on('mousedown', this.collapseIf, this);
1421 Roo.get(document).on('mousewheel', this.collapseIf, this);
1422 if (!this.editable) {
1423 Roo.get(document).on('keydown', this.listKeyPress, this);
1426 this.fireEvent('expand', this);
1430 // Implements the default empty TriggerField.onTriggerClick function
1431 onTriggerClick : function(e)
1433 Roo.log('trigger click');
1440 this.loadNext = false;
1442 if(this.isExpanded()){
1444 if (!this.blockFocus) {
1445 this.inputEl().focus();
1449 this.hasFocus = true;
1450 if(this.triggerAction == 'all') {
1451 this.doQuery(this.allQuery, true);
1453 this.doQuery(this.getRawValue());
1455 if (!this.blockFocus) {
1456 this.inputEl().focus();
1461 onTickableTriggerClick : function(e)
1468 this.loadNext = false;
1469 this.hasFocus = true;
1471 if(this.triggerAction == 'all') {
1472 this.doQuery(this.allQuery, true);
1474 this.doQuery(this.getRawValue());
1478 onSearchFieldClick : function(e)
1480 if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1485 this.loadNext = false;
1486 this.hasFocus = true;
1488 if(this.triggerAction == 'all') {
1489 this.doQuery(this.allQuery, true);
1491 this.doQuery(this.getRawValue());
1495 listKeyPress : function(e)
1497 //Roo.log('listkeypress');
1498 // scroll to first matching element based on key pres..
1499 if (e.isSpecialKey()) {
1502 var k = String.fromCharCode(e.getKey()).toUpperCase();
1505 var csel = this.view.getSelectedNodes();
1506 var cselitem = false;
1508 var ix = this.view.indexOf(csel[0]);
1509 cselitem = this.store.getAt(ix);
1510 if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1516 this.store.each(function(v) {
1518 // start at existing selection.
1519 if (cselitem.id == v.id) {
1525 if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1526 match = this.store.indexOf(v);
1532 if (match === false) {
1533 return true; // no more action?
1536 this.view.select(match);
1537 var sn = Roo.get(this.view.getSelectedNodes()[0])
1538 //sn.scrollIntoView(sn.dom.parentNode, false);
1541 onViewScroll : function(e, t){
1543 if(this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1547 this.hasQuery = true;
1549 this.loading = this.list.select('.loading', true).first();
1551 if(this.loading === null){
1552 this.list.createChild({
1554 cls: 'loading select2-more-results select2-active',
1555 html: 'Loading more results...'
1558 this.loading = this.list.select('.loading', true).first();
1560 this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1562 this.loading.hide();
1565 this.loading.show();
1570 this.loadNext = true;
1572 (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1577 addItem : function(o)
1579 var dv = ''; // display value
1581 if (this.displayField) {
1582 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1584 // this is an error condition!!!
1585 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1592 var choice = this.choices.createChild({
1594 cls: 'select2-search-choice',
1603 cls: 'select2-search-choice-close',
1608 }, this.searchField);
1610 var close = choice.select('a.select2-search-choice-close', true).first()
1612 close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
1620 this.inputEl().dom.value = '';
1624 onRemoveItem : function(e, _self, o)
1627 var index = this.item.indexOf(o.data) * 1;
1630 Roo.log('not this item?!');
1634 this.item.splice(index, 1);
1639 this.fireEvent('remove', this, e);
1643 syncValue : function()
1645 if(!this.item.length){
1652 Roo.each(this.item, function(i){
1653 if(_this.valueField){
1654 value.push(i[_this.valueField]);
1661 this.value = value.join(',');
1663 if(this.hiddenField){
1664 this.hiddenField.dom.value = this.value;
1668 clearItem : function()
1676 Roo.each(this.choices.select('>li.select2-search-choice', true).elements, function(c){
1683 inputEl: function ()
1686 return this.searchField;
1688 return this.el.select('input.form-control',true).first();
1692 onTickableFooterButtonClick : function(e, btn, el)
1696 if(btn && btn.name == 'cancel'){
1697 this.tickItems = Roo.apply([], this.item);
1706 Roo.each(this.tickItems, function(o){
1717 * @cfg {Boolean} grow
1721 * @cfg {Number} growMin
1725 * @cfg {Number} growMax