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 {Boolean} triggerList trigger show the list or not (true|false) default true
14 * @cfg {Boolean} showToggleBtn show toggle button or not (true|false) default true
15 * @cfg {String} btnPosition set the position of the trigger button (left | right) default right
17 * Create a new ComboBox.
18 * @param {Object} config Configuration options
20 Roo.bootstrap.ComboBox = function(config){
21 Roo.bootstrap.ComboBox.superclass.constructor.call(this, config);
25 * Fires when the dropdown list is expanded
26 * @param {Roo.bootstrap.ComboBox} combo This combo box
31 * Fires when the dropdown list is collapsed
32 * @param {Roo.bootstrap.ComboBox} combo This combo box
37 * Fires before a list item is selected. Return false to cancel the selection.
38 * @param {Roo.bootstrap.ComboBox} combo This combo box
39 * @param {Roo.data.Record} record The data record returned from the underlying store
40 * @param {Number} index The index of the selected item in the dropdown list
42 'beforeselect' : true,
45 * Fires when a list item is selected
46 * @param {Roo.bootstrap.ComboBox} combo This combo box
47 * @param {Roo.data.Record} record The data record returned from the underlying store (or false on clear)
48 * @param {Number} index The index of the selected item in the dropdown list
53 * Fires before all queries are processed. Return false to cancel the query or set cancel to true.
54 * The event object passed has these properties:
55 * @param {Roo.bootstrap.ComboBox} combo This combo box
56 * @param {String} query The query
57 * @param {Boolean} forceAll true to force "all" query
58 * @param {Boolean} cancel true to cancel the query
59 * @param {Object} e The query event object
64 * Fires when the 'add' icon is pressed (add a listener to enable add button)
65 * @param {Roo.bootstrap.ComboBox} combo This combo box
70 * Fires when the 'edit' icon is pressed (add a listener to enable add button)
71 * @param {Roo.bootstrap.ComboBox} combo This combo box
72 * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
77 * Fires when the remove value from the combobox array
78 * @param {Roo.bootstrap.ComboBox} combo This combo box
87 this.selectedIndex = -1;
88 if(this.mode == 'local'){
89 if(config.queryDelay === undefined){
92 if(config.minChars === undefined){
98 Roo.extend(Roo.bootstrap.ComboBox, Roo.bootstrap.TriggerField, {
101 * @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
102 * rendering into an Roo.Editor, defaults to false)
105 * @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
106 * {tag: "input", type: "text", size: "24", autocomplete: "off"})
109 * @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
112 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
113 * the dropdown list (defaults to undefined, with no header element)
117 * @cfg {String/Roo.Template} tpl The template to use to render the output
121 * @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
123 listWidth: undefined,
125 * @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
126 * mode = 'remote' or 'text' if mode = 'local')
128 displayField: undefined,
130 * @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
131 * mode = 'remote' or 'value' if mode = 'local').
132 * Note: use of a valueField requires the user make a selection
133 * in order for a value to be mapped.
135 valueField: undefined,
139 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
140 * field's data value (defaults to the underlying DOM element's name)
142 hiddenName: undefined,
144 * @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
148 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
150 selectedClass: 'active',
153 * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
157 * @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
158 * anchor positions (defaults to 'tl-bl')
162 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
166 * @cfg {String} triggerAction The action to execute when the trigger field is activated. Use 'all' to run the
167 * query specified by the allQuery config option (defaults to 'query')
169 triggerAction: 'query',
171 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
172 * (defaults to 4, does not apply if editable = false)
176 * @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
177 * delay (typeAheadDelay) if it matches a known value (defaults to false)
181 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
182 * query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
186 * @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
187 * filter queries will execute with page start and limit parameters. Only applies when mode = 'remote' (defaults to 0)
191 * @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus. Only applies
192 * when editable = true (defaults to false)
196 * @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
200 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
201 * when mode = 'remote' (defaults to 'Loading...')
203 loadingText: 'Loading...',
205 * @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
209 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
213 * @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
214 * traditional select (defaults to true)
218 * @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
222 * @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
226 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
227 * listWidth has a higher value)
231 * @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
232 * allow the user to set arbitrary text into the field (defaults to false)
234 forceSelection:false,
236 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
237 * if typeAhead = true (defaults to 250)
239 typeAheadDelay : 250,
241 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
242 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
244 valueNotFoundText : undefined,
246 * @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
251 * @cfg {Boolean} disableClear Disable showing of clear button.
253 disableClear : false,
255 * @cfg {Boolean} alwaysQuery Disable caching of results, and always send query
260 * @cfg {Boolean} multiple (true|false) ComboBobArray, default false
274 btnPosition : 'right',
276 showToggleBtn : true,
277 // element that contains real text value.. (when hidden is used..)
279 getAutoCreate : function()
287 cfg = Roo.bootstrap.ComboBox.superclass.getAutoCreate.call(this);
292 * ComboBox with tickable selections
295 var align = this.labelAlign || this.parentLabelAlign();
298 cls : 'form-group roo-combobox-tickable' //input-group
304 cls : 'tickable-buttons',
309 cls : 'btn btn-link btn-edit pull-' + this.btnPosition,
316 cls : 'btn btn-link btn-ok pull-' + this.btnPosition,
323 cls : 'btn btn-link btn-cancel pull-' + this.btnPosition,
330 Roo.each(buttons.cn, function(c){
332 c.cls += ' btn-' + _this.size;
335 if (_this.disabled) {
346 cls: 'form-hidden-field'
350 cls: 'select2-choices',
354 cls: 'select2-search-field',
366 cls: 'select2-container input-group select2-container-multi',
371 // cls: 'typeahead typeahead-long dropdown-menu',
372 // style: 'display:none; max-height:' + this.maxHeight + 'px;'
377 if (align ==='left' && this.fieldLabel.length) {
379 Roo.log("left and has label");
385 cls : 'control-label col-sm-' + this.labelWidth,
386 html : this.fieldLabel
390 cls : "col-sm-" + (12 - this.labelWidth),
397 } else if ( this.fieldLabel.length) {
403 //cls : 'input-group-addon',
404 html : this.fieldLabel
414 Roo.log(" no label && no align");
421 ['xs','sm','md','lg'].map(function(size){
422 if (settings[size]) {
423 cfg.cls += ' col-' + size + '-' + settings[size];
432 initEvents: function()
436 throw "can not find store for combo";
438 this.store = Roo.factory(this.store, Roo.data);
441 this.initTickableEvents();
445 Roo.bootstrap.ComboBox.superclass.initEvents.call(this);
449 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
451 this.hiddenField.dom.value =
452 this.hiddenValue !== undefined ? this.hiddenValue :
453 this.value !== undefined ? this.value : '';
455 // prevent input submission
456 this.el.dom.removeAttribute('name');
457 this.hiddenField.dom.setAttribute('name', this.hiddenName);
462 // this.el.dom.setAttribute('autocomplete', 'off');
465 var cls = 'x-combo-list';
467 //this.list = new Roo.Layer({
468 // shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
474 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
475 _this.list.setWidth(lw);
478 this.list.on('mouseover', this.onViewOver, this);
479 this.list.on('mousemove', this.onViewMove, this);
481 this.list.on('scroll', this.onViewScroll, this);
484 this.list.swallowEvent('mousewheel');
485 this.assetHeight = 0;
488 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
489 this.assetHeight += this.header.getHeight();
492 this.innerList = this.list.createChild({cls:cls+'-inner'});
493 this.innerList.on('mouseover', this.onViewOver, this);
494 this.innerList.on('mousemove', this.onViewMove, this);
495 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
497 if(this.allowBlank && !this.pageSize && !this.disableClear){
498 this.footer = this.list.createChild({cls:cls+'-ft'});
499 this.pageTb = new Roo.Toolbar(this.footer);
503 this.footer = this.list.createChild({cls:cls+'-ft'});
504 this.pageTb = new Roo.PagingToolbar(this.footer, this.store,
505 {pageSize: this.pageSize});
509 if (this.pageTb && this.allowBlank && !this.disableClear) {
511 this.pageTb.add(new Roo.Toolbar.Fill(), {
512 cls: 'x-btn-icon x-btn-clear',
518 _this.onSelect(false, -1);
523 this.assetHeight += this.footer.getHeight();
528 this.tpl = '<li><a href="#">{' + this.displayField + '}</a></li>';
531 this.view = new Roo.View(this.list, this.tpl, {
532 singleSelect:true, store: this.store, selectedClass: this.selectedClass
534 //this.view.wrapEl.setDisplayed(false);
535 this.view.on('click', this.onViewClick, this);
539 this.store.on('beforeload', this.onBeforeLoad, this);
540 this.store.on('load', this.onLoad, this);
541 this.store.on('loadexception', this.onLoadException, this);
544 this.resizer = new Roo.Resizable(this.list, {
545 pinned:true, handles:'se'
547 this.resizer.on('resize', function(r, w, h){
548 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
550 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
551 this.restrictHeight();
553 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
557 this.editable = true;
558 this.setEditable(false);
563 if (typeof(this.events.add.listeners) != 'undefined') {
565 this.addicon = this.wrap.createChild(
566 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-add' });
568 this.addicon.on('click', function(e) {
569 this.fireEvent('add', this);
572 if (typeof(this.events.edit.listeners) != 'undefined') {
574 this.editicon = this.wrap.createChild(
575 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-edit' });
577 this.editicon.setStyle('margin-left', '40px');
579 this.editicon.on('click', function(e) {
581 // we fire even if inothing is selected..
582 this.fireEvent('edit', this, this.lastData );
588 this.keyNav = new Roo.KeyNav(this.inputEl(), {
590 this.inKeyMode = true;
594 "down" : function(e){
595 if(!this.isExpanded()){
596 this.onTriggerClick();
598 this.inKeyMode = true;
603 "enter" : function(e){
604 // this.onViewClick();
608 if(this.fireEvent("specialkey", this, e)){
609 this.onViewClick(false);
622 if(this.fireEvent("specialkey", this, e)){
623 this.onViewClick(false);
631 doRelay : function(foo, bar, hname){
632 if(hname == 'down' || this.scope.isExpanded()){
633 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
642 this.queryDelay = Math.max(this.queryDelay || 10,
643 this.mode == 'local' ? 10 : 250);
646 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
649 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
651 if(this.editable !== false){
652 this.inputEl().on("keyup", this.onKeyUp, this);
654 if(this.forceSelection){
655 this.inputEl().on('blur', this.doForce, this);
659 this.choices = this.el.select('ul.select2-choices', true).first();
660 this.searchField = this.el.select('ul li.select2-search-field', true).first();
664 initTickableEvents: function()
670 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
672 this.hiddenField.dom.value =
673 this.hiddenValue !== undefined ? this.hiddenValue :
674 this.value !== undefined ? this.value : '';
676 // prevent input submission
677 this.el.dom.removeAttribute('name');
678 this.hiddenField.dom.setAttribute('name', this.hiddenName);
683 // this.list = this.el.select('ul.dropdown-menu',true).first();
685 this.choices = this.el.select('ul.select2-choices', true).first();
686 this.searchField = this.el.select('ul li.select2-search-field', true).first();
687 if(this.triggerList){
688 this.searchField.on("click", this.onSearchFieldClick, this, {preventDefault:true});
691 this.trigger = this.el.select('.tickable-buttons > .btn-edit', true).first();
692 this.trigger.on("click", this.onTickableTriggerClick, this, {preventDefault:true});
694 this.okBtn = this.el.select('.tickable-buttons > .btn-ok', true).first();
695 this.cancelBtn = this.el.select('.tickable-buttons > .btn-cancel', true).first();
697 this.okBtn.on('click', this.onTickableFooterButtonClick, this, this.okBtn);
698 this.cancelBtn.on('click', this.onTickableFooterButtonClick, this, this.cancelBtn);
700 this.trigger.setVisibilityMode(Roo.Element.DISPLAY);
701 this.okBtn.setVisibilityMode(Roo.Element.DISPLAY);
702 this.cancelBtn.setVisibilityMode(Roo.Element.DISPLAY);
705 this.cancelBtn.hide();
710 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
711 _this.list.setWidth(lw);
714 this.list.on('mouseover', this.onViewOver, this);
715 this.list.on('mousemove', this.onViewMove, this);
717 this.list.on('scroll', this.onViewScroll, this);
720 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>';
723 this.view = new Roo.View(this.list, this.tpl, {
724 singleSelect:true, tickable:true, parent:this, store: this.store, selectedClass: this.selectedClass
727 //this.view.wrapEl.setDisplayed(false);
728 this.view.on('click', this.onViewClick, this);
732 this.store.on('beforeload', this.onBeforeLoad, this);
733 this.store.on('load', this.onLoad, this);
734 this.store.on('loadexception', this.onLoadException, this);
736 // this.keyNav = new Roo.KeyNav(this.inputEl(), {
737 // "up" : function(e){
738 // this.inKeyMode = true;
739 // this.selectPrev();
742 // "down" : function(e){
743 // if(!this.isExpanded()){
744 // this.onTriggerClick();
746 // this.inKeyMode = true;
747 // this.selectNext();
751 // "enter" : function(e){
752 //// this.onViewClick();
756 // if(this.fireEvent("specialkey", this, e)){
757 // this.onViewClick(false);
763 // "esc" : function(e){
767 // "tab" : function(e){
770 // if(this.fireEvent("specialkey", this, e)){
771 // this.onViewClick(false);
779 // doRelay : function(foo, bar, hname){
780 // if(hname == 'down' || this.scope.isExpanded()){
781 // return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
786 // forceKeyDown: true
790 this.queryDelay = Math.max(this.queryDelay || 10,
791 this.mode == 'local' ? 10 : 250);
794 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
797 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
801 onDestroy : function(){
803 this.view.setStore(null);
804 this.view.el.removeAllListeners();
805 this.view.el.remove();
806 this.view.purgeListeners();
809 this.list.dom.innerHTML = '';
813 this.store.un('beforeload', this.onBeforeLoad, this);
814 this.store.un('load', this.onLoad, this);
815 this.store.un('loadexception', this.onLoadException, this);
817 Roo.bootstrap.ComboBox.superclass.onDestroy.call(this);
821 fireKey : function(e){
822 if(e.isNavKeyPress() && !this.list.isVisible()){
823 this.fireEvent("specialkey", this, e);
828 onResize: function(w, h){
829 // Roo.bootstrap.ComboBox.superclass.onResize.apply(this, arguments);
831 // if(typeof w != 'number'){
832 // // we do not handle it!?!?
835 // var tw = this.trigger.getWidth();
836 // // tw += this.addicon ? this.addicon.getWidth() : 0;
837 // // tw += this.editicon ? this.editicon.getWidth() : 0;
839 // this.inputEl().setWidth( this.adjustWidth('input', x));
841 // //this.trigger.setStyle('left', x+'px');
843 // if(this.list && this.listWidth === undefined){
844 // var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
845 // this.list.setWidth(lw);
846 // this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
854 * Allow or prevent the user from directly editing the field text. If false is passed,
855 * the user will only be able to select from the items defined in the dropdown list. This method
856 * is the runtime equivalent of setting the 'editable' config option at config time.
857 * @param {Boolean} value True to allow the user to directly edit the field text
859 setEditable : function(value){
860 if(value == this.editable){
863 this.editable = value;
865 this.inputEl().dom.setAttribute('readOnly', true);
866 this.inputEl().on('mousedown', this.onTriggerClick, this);
867 this.inputEl().addClass('x-combo-noedit');
869 this.inputEl().dom.setAttribute('readOnly', false);
870 this.inputEl().un('mousedown', this.onTriggerClick, this);
871 this.inputEl().removeClass('x-combo-noedit');
877 onBeforeLoad : function(combo,opts){
882 this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
884 // this.restrictHeight();
885 this.selectedIndex = -1;
891 this.hasQuery = false;
897 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
901 if(this.store.getCount() > 0){
903 // this.restrictHeight();
904 if(this.lastQuery == this.allQuery){
905 if(this.editable && !this.tickable){
906 this.inputEl().dom.select();
908 if(!this.selectByValue(this.value, true) && this.autoFocus){
909 this.select(0, true);
915 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
916 this.taTask.delay(this.typeAheadDelay);
920 this.onEmptyResults();
926 onLoadException : function()
928 this.hasQuery = false;
930 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
935 Roo.log(this.store.reader.jsonData);
936 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
938 //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
944 onTypeAhead : function(){
945 if(this.store.getCount() > 0){
946 var r = this.store.getAt(0);
947 var newValue = r.data[this.displayField];
948 var len = newValue.length;
949 var selStart = this.getRawValue().length;
952 this.setRawValue(newValue);
953 this.selectText(selStart, newValue.length);
959 onSelect : function(record, index){
961 if(this.fireEvent('beforeselect', this, record, index) !== false){
963 this.setFromData(index > -1 ? record.data : false);
966 this.fireEvent('select', this, record, index);
971 * Returns the currently selected field value or empty string if no value is set.
972 * @return {String} value The selected value
974 getValue : function(){
977 return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
981 return typeof this.value != 'undefined' ? this.value : '';
983 return Roo.bootstrap.ComboBox.superclass.getValue.call(this);
988 * Clears any text/value currently set in the field
990 clearValue : function(){
991 if(this.hiddenField){
992 this.hiddenField.dom.value = '';
995 this.setRawValue('');
996 this.lastSelectionText = '';
1001 * Sets the specified value into the field. If the value finds a match, the corresponding record text
1002 * will be displayed in the field. If the value does not match the data value of an existing item,
1003 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
1004 * Otherwise the field will be blank (although the value will still be set).
1005 * @param {String} value The value to match
1007 setValue : function(v){
1014 if(this.valueField){
1015 var r = this.findRecord(this.valueField, v);
1017 text = r.data[this.displayField];
1018 }else if(this.valueNotFoundText !== undefined){
1019 text = this.valueNotFoundText;
1022 this.lastSelectionText = text;
1023 if(this.hiddenField){
1024 this.hiddenField.dom.value = v;
1026 Roo.bootstrap.ComboBox.superclass.setValue.call(this, text);
1030 * @property {Object} the last set data for the element
1035 * Sets the value of the field based on a object which is related to the record format for the store.
1036 * @param {Object} value the value to set as. or false on reset?
1038 setFromData : function(o){
1041 if(typeof o.display_name !== 'string'){
1042 for(var i=0;i<o.display_name.length;i++){
1043 this.addItem({'id':o.id[i],'display_name':o.display_name[i]});
1051 var dv = ''; // display value
1052 var vv = ''; // value value..
1054 if (this.displayField) {
1055 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1057 // this is an error condition!!!
1058 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1061 if(this.valueField){
1062 vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
1065 if(this.hiddenField){
1066 this.hiddenField.dom.value = vv;
1068 this.lastSelectionText = dv;
1069 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1073 // no hidden field.. - we store the value in 'value', but still display
1074 // display field!!!!
1075 this.lastSelectionText = dv;
1076 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1083 // overridden so that last data is reset..
1084 this.setValue(this.originalValue);
1085 this.clearInvalid();
1086 this.lastData = false;
1088 this.view.clearSelections();
1092 findRecord : function(prop, value){
1094 if(this.store.getCount() > 0){
1095 this.store.each(function(r){
1096 if(r.data[prop] == value){
1108 // returns hidden if it's set..
1109 if (!this.rendered) {return ''};
1110 return !this.hiddenName && this.inputEl().dom.name ? this.inputEl().dom.name : (this.hiddenName || '');
1114 onViewMove : function(e, t){
1115 this.inKeyMode = false;
1119 onViewOver : function(e, t){
1120 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1123 var item = this.view.findItemFromChild(t);
1126 var index = this.view.indexOf(item);
1127 this.select(index, false);
1132 onViewClick : function(view, doFocus, el, e)
1134 var index = this.view.getSelectedIndexes()[0];
1136 var r = this.store.getAt(index);
1140 if(e.getTarget().nodeName.toLowerCase() != 'input'){
1147 Roo.each(this.tickItems, function(v,k){
1149 if(typeof(v) != 'undefined' && v[_this.valueField] == r.data[_this.valueField]){
1150 _this.tickItems.splice(k, 1);
1160 this.tickItems.push(r.data);
1165 this.onSelect(r, index);
1167 if(doFocus !== false && !this.blockFocus){
1168 this.inputEl().focus();
1173 restrictHeight : function(){
1174 //this.innerList.dom.style.height = '';
1175 //var inner = this.innerList.dom;
1176 //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
1177 //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
1178 //this.list.beginUpdate();
1179 //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
1180 this.list.alignTo(this.inputEl(), this.listAlign);
1181 this.list.alignTo(this.inputEl(), this.listAlign);
1182 //this.list.endUpdate();
1186 onEmptyResults : function(){
1191 * Returns true if the dropdown list is expanded, else false.
1193 isExpanded : function(){
1194 return this.list.isVisible();
1198 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1199 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1200 * @param {String} value The data value of the item to select
1201 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1202 * selected item if it is not currently in view (defaults to true)
1203 * @return {Boolean} True if the value matched an item in the list, else false
1205 selectByValue : function(v, scrollIntoView){
1206 if(v !== undefined && v !== null){
1207 var r = this.findRecord(this.valueField || this.displayField, v);
1209 this.select(this.store.indexOf(r), scrollIntoView);
1217 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1218 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1219 * @param {Number} index The zero-based index of the list item to select
1220 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1221 * selected item if it is not currently in view (defaults to true)
1223 select : function(index, scrollIntoView){
1224 this.selectedIndex = index;
1225 this.view.select(index);
1226 if(scrollIntoView !== false){
1227 var el = this.view.getNode(index);
1228 if(el && !this.multiple && !this.tickable){
1229 this.list.scrollChildIntoView(el, false);
1235 selectNext : function(){
1236 var ct = this.store.getCount();
1238 if(this.selectedIndex == -1){
1240 }else if(this.selectedIndex < ct-1){
1241 this.select(this.selectedIndex+1);
1247 selectPrev : function(){
1248 var ct = this.store.getCount();
1250 if(this.selectedIndex == -1){
1252 }else if(this.selectedIndex != 0){
1253 this.select(this.selectedIndex-1);
1259 onKeyUp : function(e){
1260 if(this.editable !== false && !e.isSpecialKey()){
1261 this.lastKey = e.getKey();
1262 this.dqTask.delay(this.queryDelay);
1267 validateBlur : function(){
1268 return !this.list || !this.list.isVisible();
1272 initQuery : function(){
1273 this.doQuery(this.getRawValue());
1277 doForce : function(){
1278 if(this.inputEl().dom.value.length > 0){
1279 this.inputEl().dom.value =
1280 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
1286 * Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
1287 * query allowing the query action to be canceled if needed.
1288 * @param {String} query The SQL query to execute
1289 * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
1290 * in the field than the minimum specified by the minChars config option. It also clears any filter previously
1291 * saved in the current store (defaults to false)
1293 doQuery : function(q, forceAll){
1295 if(q === undefined || q === null){
1304 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1309 forceAll = qe.forceAll;
1310 if(forceAll === true || (q.length >= this.minChars)){
1312 this.hasQuery = true;
1314 if(this.lastQuery != q || this.alwaysQuery){
1316 if(this.mode == 'local'){
1317 this.selectedIndex = -1;
1319 this.store.clearFilter();
1321 this.store.filter(this.displayField, q);
1325 this.store.baseParams[this.queryParam] = q;
1327 var options = {params : this.getParams(q)};
1331 options.params.start = this.page * this.pageSize;
1334 this.store.load(options);
1336 * this code will make the page width larger, at the beginning, the list not align correctly,
1337 * we should expand the list on onLoad
1343 this.selectedIndex = -1;
1348 this.loadNext = false;
1352 getParams : function(q){
1354 //p[this.queryParam] = q;
1358 p.limit = this.pageSize;
1364 * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1366 collapse : function(){
1367 if(!this.isExpanded()){
1371 this.hasFocus = false;
1377 this.cancelBtn.hide();
1378 this.trigger.show();
1381 Roo.get(document).un('mousedown', this.collapseIf, this);
1382 Roo.get(document).un('mousewheel', this.collapseIf, this);
1383 if (!this.editable) {
1384 Roo.get(document).un('keydown', this.listKeyPress, this);
1386 this.fireEvent('collapse', this);
1390 collapseIf : function(e){
1391 var in_combo = e.within(this.el);
1392 var in_list = e.within(this.list);
1393 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
1395 if (in_combo || in_list || is_list) {
1396 //e.stopPropagation();
1401 this.onTickableFooterButtonClick(e, false, false);
1409 * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1411 expand : function(){
1413 if(this.isExpanded() || !this.hasFocus){
1417 var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
1418 this.list.setWidth(lw);
1425 this.restrictHeight();
1429 this.tickItems = Roo.apply([], this.item);
1432 this.cancelBtn.show();
1433 this.trigger.hide();
1437 Roo.get(document).on('mousedown', this.collapseIf, this);
1438 Roo.get(document).on('mousewheel', this.collapseIf, this);
1439 if (!this.editable) {
1440 Roo.get(document).on('keydown', this.listKeyPress, this);
1443 this.fireEvent('expand', this);
1447 // Implements the default empty TriggerField.onTriggerClick function
1448 onTriggerClick : function(e)
1450 Roo.log('trigger click');
1452 if(this.disabled || !this.triggerList){
1457 this.loadNext = false;
1459 if(this.isExpanded()){
1461 if (!this.blockFocus) {
1462 this.inputEl().focus();
1466 this.hasFocus = true;
1467 if(this.triggerAction == 'all') {
1468 this.doQuery(this.allQuery, true);
1470 this.doQuery(this.getRawValue());
1472 if (!this.blockFocus) {
1473 this.inputEl().focus();
1478 onTickableTriggerClick : function(e)
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 onSearchFieldClick : function(e)
1497 if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1502 this.loadNext = false;
1503 this.hasFocus = true;
1505 if(this.triggerAction == 'all') {
1506 this.doQuery(this.allQuery, true);
1508 this.doQuery(this.getRawValue());
1512 listKeyPress : function(e)
1514 //Roo.log('listkeypress');
1515 // scroll to first matching element based on key pres..
1516 if (e.isSpecialKey()) {
1519 var k = String.fromCharCode(e.getKey()).toUpperCase();
1522 var csel = this.view.getSelectedNodes();
1523 var cselitem = false;
1525 var ix = this.view.indexOf(csel[0]);
1526 cselitem = this.store.getAt(ix);
1527 if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1533 this.store.each(function(v) {
1535 // start at existing selection.
1536 if (cselitem.id == v.id) {
1542 if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1543 match = this.store.indexOf(v);
1549 if (match === false) {
1550 return true; // no more action?
1553 this.view.select(match);
1554 var sn = Roo.get(this.view.getSelectedNodes()[0])
1555 //sn.scrollIntoView(sn.dom.parentNode, false);
1558 onViewScroll : function(e, t){
1560 if(this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1564 this.hasQuery = true;
1566 this.loading = this.list.select('.loading', true).first();
1568 if(this.loading === null){
1569 this.list.createChild({
1571 cls: 'loading select2-more-results select2-active',
1572 html: 'Loading more results...'
1575 this.loading = this.list.select('.loading', true).first();
1577 this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1579 this.loading.hide();
1582 this.loading.show();
1587 this.loadNext = true;
1589 (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1594 addItem : function(o)
1596 var dv = ''; // display value
1598 if (this.displayField) {
1599 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1601 // this is an error condition!!!
1602 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1609 var choice = this.choices.createChild({
1611 cls: 'select2-search-choice',
1620 cls: 'select2-search-choice-close',
1625 }, this.searchField);
1627 var close = choice.select('a.select2-search-choice-close', true).first()
1629 close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
1637 this.inputEl().dom.value = '';
1641 onRemoveItem : function(e, _self, o)
1644 var index = this.item.indexOf(o.data) * 1;
1647 Roo.log('not this item?!');
1651 this.item.splice(index, 1);
1656 this.fireEvent('remove', this, e);
1660 syncValue : function()
1662 if(!this.item.length){
1669 Roo.each(this.item, function(i){
1670 if(_this.valueField){
1671 value.push(i[_this.valueField]);
1678 this.value = value.join(',');
1680 if(this.hiddenField){
1681 this.hiddenField.dom.value = this.value;
1685 clearItem : function()
1693 Roo.each(this.choices.select('>li.select2-search-choice', true).elements, function(c){
1700 inputEl: function ()
1703 return this.searchField;
1705 return this.el.select('input.form-control',true).first();
1709 onTickableFooterButtonClick : function(e, btn, el)
1713 if(btn && btn.name == 'cancel'){
1714 this.tickItems = Roo.apply([], this.item);
1723 Roo.each(this.tickItems, function(o){
1734 * @cfg {Boolean} grow
1738 * @cfg {Number} growMin
1742 * @cfg {Number} growMax