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 {String} btnPosition set the position of the trigger button (left | right) default right
16 * Create a new ComboBox.
17 * @param {Object} config Configuration options
19 Roo.bootstrap.ComboBox = function(config){
20 Roo.bootstrap.ComboBox.superclass.constructor.call(this, config);
24 * Fires when the dropdown list is expanded
25 * @param {Roo.bootstrap.ComboBox} combo This combo box
30 * Fires when the dropdown list is collapsed
31 * @param {Roo.bootstrap.ComboBox} combo This combo box
36 * Fires before a list item is selected. Return false to cancel the selection.
37 * @param {Roo.bootstrap.ComboBox} combo This combo box
38 * @param {Roo.data.Record} record The data record returned from the underlying store
39 * @param {Number} index The index of the selected item in the dropdown list
41 'beforeselect' : true,
44 * Fires when a list item is selected
45 * @param {Roo.bootstrap.ComboBox} combo This combo box
46 * @param {Roo.data.Record} record The data record returned from the underlying store (or false on clear)
47 * @param {Number} index The index of the selected item in the dropdown list
52 * Fires before all queries are processed. Return false to cancel the query or set cancel to true.
53 * The event object passed has these properties:
54 * @param {Roo.bootstrap.ComboBox} combo This combo box
55 * @param {String} query The query
56 * @param {Boolean} forceAll true to force "all" query
57 * @param {Boolean} cancel true to cancel the query
58 * @param {Object} e The query event object
63 * Fires when the 'add' icon is pressed (add a listener to enable add button)
64 * @param {Roo.bootstrap.ComboBox} combo This combo box
69 * Fires when the 'edit' icon is pressed (add a listener to enable add button)
70 * @param {Roo.bootstrap.ComboBox} combo This combo box
71 * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
76 * Fires when the remove value from the combobox array
77 * @param {Roo.bootstrap.ComboBox} combo This combo box
86 this.selectedIndex = -1;
87 if(this.mode == 'local'){
88 if(config.queryDelay === undefined){
91 if(config.minChars === undefined){
97 Roo.extend(Roo.bootstrap.ComboBox, Roo.bootstrap.TriggerField, {
100 * @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
101 * rendering into an Roo.Editor, defaults to false)
104 * @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
105 * {tag: "input", type: "text", size: "24", autocomplete: "off"})
108 * @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
111 * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
112 * the dropdown list (defaults to undefined, with no header element)
116 * @cfg {String/Roo.Template} tpl The template to use to render the output
120 * @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
122 listWidth: undefined,
124 * @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
125 * mode = 'remote' or 'text' if mode = 'local')
127 displayField: undefined,
129 * @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
130 * mode = 'remote' or 'value' if mode = 'local').
131 * Note: use of a valueField requires the user make a selection
132 * in order for a value to be mapped.
134 valueField: undefined,
138 * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
139 * field's data value (defaults to the underlying DOM element's name)
141 hiddenName: undefined,
143 * @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
147 * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
149 selectedClass: 'active',
152 * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
156 * @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
157 * anchor positions (defaults to 'tl-bl')
161 * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
165 * @cfg {String} triggerAction The action to execute when the trigger field is activated. Use 'all' to run the
166 * query specified by the allQuery config option (defaults to 'query')
168 triggerAction: 'query',
170 * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
171 * (defaults to 4, does not apply if editable = false)
175 * @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
176 * delay (typeAheadDelay) if it matches a known value (defaults to false)
180 * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
181 * query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
185 * @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
186 * filter queries will execute with page start and limit parameters. Only applies when mode = 'remote' (defaults to 0)
190 * @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus. Only applies
191 * when editable = true (defaults to false)
195 * @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
199 * @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
200 * when mode = 'remote' (defaults to 'Loading...')
202 loadingText: 'Loading...',
204 * @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
208 * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
212 * @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
213 * traditional select (defaults to true)
217 * @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
221 * @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
225 * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
226 * listWidth has a higher value)
230 * @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
231 * allow the user to set arbitrary text into the field (defaults to false)
233 forceSelection:false,
235 * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
236 * if typeAhead = true (defaults to 250)
238 typeAheadDelay : 250,
240 * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
241 * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
243 valueNotFoundText : undefined,
245 * @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
250 * @cfg {Boolean} disableClear Disable showing of clear button.
252 disableClear : false,
254 * @cfg {Boolean} alwaysQuery Disable caching of results, and always send query
259 * @cfg {Boolean} multiple (true|false) ComboBobArray, default false
273 btnPosition : 'right',
275 // element that contains real text value.. (when hidden is used..)
277 getAutoCreate : function()
285 cfg = Roo.bootstrap.ComboBox.superclass.getAutoCreate.call(this);
290 * ComboBox with tickable selections
293 var align = this.labelAlign || this.parentLabelAlign();
296 cls : 'form-group roo-combobox-tickable' //input-group
302 cls : 'tickable-buttons',
307 cls : 'btn btn-link btn-edit pull-' + this.btnPosition,
314 cls : 'btn btn-link btn-ok pull-' + this.btnPosition,
321 cls : 'btn btn-link btn-cancel pull-' + this.btnPosition,
328 Roo.each(buttons.cn, function(c){
330 c.cls += ' btn-' + _this.size;
333 if (_this.disabled) {
344 cls: 'form-hidden-field'
348 cls: 'select2-choices',
352 cls: 'select2-search-field',
364 cls: 'select2-container input-group select2-container-multi',
369 // cls: 'typeahead typeahead-long dropdown-menu',
370 // style: 'display:none; max-height:' + this.maxHeight + 'px;'
375 if (align ==='left' && this.fieldLabel.length) {
377 Roo.log("left and has label");
383 cls : 'control-label col-sm-' + this.labelWidth,
384 html : this.fieldLabel
388 cls : "col-sm-" + (12 - this.labelWidth),
395 } else if ( this.fieldLabel.length) {
401 //cls : 'input-group-addon',
402 html : this.fieldLabel
412 Roo.log(" no label && no align");
419 ['xs','sm','md','lg'].map(function(size){
420 if (settings[size]) {
421 cfg.cls += ' col-' + size + '-' + settings[size];
430 initEvents: function()
434 throw "can not find store for combo";
436 this.store = Roo.factory(this.store, Roo.data);
439 this.initTickableEvnets();
443 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';
465 //this.list = new Roo.Layer({
466 // shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
472 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
473 _this.list.setWidth(lw);
476 this.list.on('mouseover', this.onViewOver, this);
477 this.list.on('mousemove', this.onViewMove, this);
479 this.list.on('scroll', this.onViewScroll, this);
482 this.list.swallowEvent('mousewheel');
483 this.assetHeight = 0;
486 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
487 this.assetHeight += this.header.getHeight();
490 this.innerList = this.list.createChild({cls:cls+'-inner'});
491 this.innerList.on('mouseover', this.onViewOver, this);
492 this.innerList.on('mousemove', this.onViewMove, this);
493 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
495 if(this.allowBlank && !this.pageSize && !this.disableClear){
496 this.footer = this.list.createChild({cls:cls+'-ft'});
497 this.pageTb = new Roo.Toolbar(this.footer);
501 this.footer = this.list.createChild({cls:cls+'-ft'});
502 this.pageTb = new Roo.PagingToolbar(this.footer, this.store,
503 {pageSize: this.pageSize});
507 if (this.pageTb && this.allowBlank && !this.disableClear) {
509 this.pageTb.add(new Roo.Toolbar.Fill(), {
510 cls: 'x-btn-icon x-btn-clear',
516 _this.onSelect(false, -1);
521 this.assetHeight += this.footer.getHeight();
526 this.tpl = '<li><a href="#">{' + this.displayField + '}</a></li>';
529 this.view = new Roo.View(this.list, this.tpl, {
530 singleSelect:true, store: this.store, selectedClass: this.selectedClass
532 //this.view.wrapEl.setDisplayed(false);
533 this.view.on('click', this.onViewClick, this);
537 this.store.on('beforeload', this.onBeforeLoad, this);
538 this.store.on('load', this.onLoad, this);
539 this.store.on('loadexception', this.onLoadException, this);
542 this.resizer = new Roo.Resizable(this.list, {
543 pinned:true, handles:'se'
545 this.resizer.on('resize', function(r, w, h){
546 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
548 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
549 this.restrictHeight();
551 this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
555 this.editable = true;
556 this.setEditable(false);
561 if (typeof(this.events.add.listeners) != 'undefined') {
563 this.addicon = this.wrap.createChild(
564 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-add' });
566 this.addicon.on('click', function(e) {
567 this.fireEvent('add', this);
570 if (typeof(this.events.edit.listeners) != 'undefined') {
572 this.editicon = this.wrap.createChild(
573 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-edit' });
575 this.editicon.setStyle('margin-left', '40px');
577 this.editicon.on('click', function(e) {
579 // we fire even if inothing is selected..
580 this.fireEvent('edit', this, this.lastData );
586 this.keyNav = new Roo.KeyNav(this.inputEl(), {
588 this.inKeyMode = true;
592 "down" : function(e){
593 if(!this.isExpanded()){
594 this.onTriggerClick();
596 this.inKeyMode = true;
601 "enter" : function(e){
602 // this.onViewClick();
606 if(this.fireEvent("specialkey", this, e)){
607 this.onViewClick(false);
620 if(this.fireEvent("specialkey", this, e)){
621 this.onViewClick(false);
629 doRelay : function(foo, bar, hname){
630 if(hname == 'down' || this.scope.isExpanded()){
631 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
640 this.queryDelay = Math.max(this.queryDelay || 10,
641 this.mode == 'local' ? 10 : 250);
644 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
647 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
649 if(this.editable !== false){
650 this.inputEl().on("keyup", this.onKeyUp, this);
652 if(this.forceSelection){
653 this.inputEl().on('blur', this.doForce, this);
657 this.choices = this.el.select('ul.select2-choices', true).first();
658 this.searchField = this.el.select('ul li.select2-search-field', true).first();
662 initTickableEvnets: function()
668 this.hiddenField = this.el.select('input.form-hidden-field',true).first();
670 this.hiddenField.dom.value =
671 this.hiddenValue !== undefined ? this.hiddenValue :
672 this.value !== undefined ? this.value : '';
674 // prevent input submission
675 this.el.dom.removeAttribute('name');
676 this.hiddenField.dom.setAttribute('name', this.hiddenName);
681 // this.list = this.el.select('ul.dropdown-menu',true).first();
683 this.choices = this.el.select('ul.select2-choices', true).first();
684 this.searchField = this.el.select('ul li.select2-search-field', true).first();
685 if(this.triggerList){
686 this.searchField.on("click", this.onSearchFieldClick, this, {preventDefault:true});
689 this.trigger = this.el.select('.tickable-buttons > .btn-edit', true).first();
690 this.trigger.on("click", this.onTickableTriggerClick, this, {preventDefault:true});
692 this.okBtn = this.el.select('.tickable-buttons > .btn-ok', true).first();
693 this.cancelBtn = this.el.select('.tickable-buttons > .btn-cancel', true).first();
695 this.okBtn.on('click', this.onTickableFooterButtonClick, this, this.okBtn);
696 this.cancelBtn.on('click', this.onTickableFooterButtonClick, this, this.cancelBtn);
698 this.trigger.setVisibilityMode(Roo.Element.DISPLAY);
699 this.okBtn.setVisibilityMode(Roo.Element.DISPLAY);
700 this.cancelBtn.setVisibilityMode(Roo.Element.DISPLAY);
703 this.cancelBtn.hide();
708 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
709 _this.list.setWidth(lw);
712 this.list.on('mouseover', this.onViewOver, this);
713 this.list.on('mousemove', this.onViewMove, this);
715 this.list.on('scroll', this.onViewScroll, this);
718 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>';
721 this.view = new Roo.View(this.list, this.tpl, {
722 singleSelect:true, tickable:true, parent:this, store: this.store, selectedClass: this.selectedClass
725 //this.view.wrapEl.setDisplayed(false);
726 this.view.on('click', this.onViewClick, this);
730 this.store.on('beforeload', this.onBeforeLoad, this);
731 this.store.on('load', this.onLoad, this);
732 this.store.on('loadexception', this.onLoadException, this);
734 // this.keyNav = new Roo.KeyNav(this.inputEl(), {
735 // "up" : function(e){
736 // this.inKeyMode = true;
737 // this.selectPrev();
740 // "down" : function(e){
741 // if(!this.isExpanded()){
742 // this.onTriggerClick();
744 // this.inKeyMode = true;
745 // this.selectNext();
749 // "enter" : function(e){
750 //// this.onViewClick();
754 // if(this.fireEvent("specialkey", this, e)){
755 // this.onViewClick(false);
761 // "esc" : function(e){
765 // "tab" : function(e){
768 // if(this.fireEvent("specialkey", this, e)){
769 // this.onViewClick(false);
777 // doRelay : function(foo, bar, hname){
778 // if(hname == 'down' || this.scope.isExpanded()){
779 // return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
784 // forceKeyDown: true
788 this.queryDelay = Math.max(this.queryDelay || 10,
789 this.mode == 'local' ? 10 : 250);
792 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
795 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
799 onDestroy : function(){
801 this.view.setStore(null);
802 this.view.el.removeAllListeners();
803 this.view.el.remove();
804 this.view.purgeListeners();
807 this.list.dom.innerHTML = '';
811 this.store.un('beforeload', this.onBeforeLoad, this);
812 this.store.un('load', this.onLoad, this);
813 this.store.un('loadexception', this.onLoadException, this);
815 Roo.bootstrap.ComboBox.superclass.onDestroy.call(this);
819 fireKey : function(e){
820 if(e.isNavKeyPress() && !this.list.isVisible()){
821 this.fireEvent("specialkey", this, e);
826 onResize: function(w, h){
827 // Roo.bootstrap.ComboBox.superclass.onResize.apply(this, arguments);
829 // if(typeof w != 'number'){
830 // // we do not handle it!?!?
833 // var tw = this.trigger.getWidth();
834 // // tw += this.addicon ? this.addicon.getWidth() : 0;
835 // // tw += this.editicon ? this.editicon.getWidth() : 0;
837 // this.inputEl().setWidth( this.adjustWidth('input', x));
839 // //this.trigger.setStyle('left', x+'px');
841 // if(this.list && this.listWidth === undefined){
842 // var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
843 // this.list.setWidth(lw);
844 // this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
852 * Allow or prevent the user from directly editing the field text. If false is passed,
853 * the user will only be able to select from the items defined in the dropdown list. This method
854 * is the runtime equivalent of setting the 'editable' config option at config time.
855 * @param {Boolean} value True to allow the user to directly edit the field text
857 setEditable : function(value){
858 if(value == this.editable){
861 this.editable = value;
863 this.inputEl().dom.setAttribute('readOnly', true);
864 this.inputEl().on('mousedown', this.onTriggerClick, this);
865 this.inputEl().addClass('x-combo-noedit');
867 this.inputEl().dom.setAttribute('readOnly', false);
868 this.inputEl().un('mousedown', this.onTriggerClick, this);
869 this.inputEl().removeClass('x-combo-noedit');
875 onBeforeLoad : function(combo,opts){
880 this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
882 // this.restrictHeight();
883 this.selectedIndex = -1;
889 this.hasQuery = false;
895 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
899 if(this.store.getCount() > 0){
901 // this.restrictHeight();
902 if(this.lastQuery == this.allQuery){
903 if(this.editable && !this.tickable){
904 this.inputEl().dom.select();
906 if(!this.selectByValue(this.value, true) && this.autoFocus){
907 this.select(0, true);
913 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
914 this.taTask.delay(this.typeAheadDelay);
918 this.onEmptyResults();
924 onLoadException : function()
926 this.hasQuery = false;
928 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
933 Roo.log(this.store.reader.jsonData);
934 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
936 //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
942 onTypeAhead : function(){
943 if(this.store.getCount() > 0){
944 var r = this.store.getAt(0);
945 var newValue = r.data[this.displayField];
946 var len = newValue.length;
947 var selStart = this.getRawValue().length;
950 this.setRawValue(newValue);
951 this.selectText(selStart, newValue.length);
957 onSelect : function(record, index){
959 if(this.fireEvent('beforeselect', this, record, index) !== false){
961 this.setFromData(index > -1 ? record.data : false);
964 this.fireEvent('select', this, record, index);
969 * Returns the currently selected field value or empty string if no value is set.
970 * @return {String} value The selected value
972 getValue : function(){
975 return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
979 return typeof this.value != 'undefined' ? this.value : '';
981 return Roo.bootstrap.ComboBox.superclass.getValue.call(this);
986 * Clears any text/value currently set in the field
988 clearValue : function(){
989 if(this.hiddenField){
990 this.hiddenField.dom.value = '';
993 this.setRawValue('');
994 this.lastSelectionText = '';
999 * Sets the specified value into the field. If the value finds a match, the corresponding record text
1000 * will be displayed in the field. If the value does not match the data value of an existing item,
1001 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
1002 * Otherwise the field will be blank (although the value will still be set).
1003 * @param {String} value The value to match
1005 setValue : function(v){
1012 if(this.valueField){
1013 var r = this.findRecord(this.valueField, v);
1015 text = r.data[this.displayField];
1016 }else if(this.valueNotFoundText !== undefined){
1017 text = this.valueNotFoundText;
1020 this.lastSelectionText = text;
1021 if(this.hiddenField){
1022 this.hiddenField.dom.value = v;
1024 Roo.bootstrap.ComboBox.superclass.setValue.call(this, text);
1028 * @property {Object} the last set data for the element
1033 * Sets the value of the field based on a object which is related to the record format for the store.
1034 * @param {Object} value the value to set as. or false on reset?
1036 setFromData : function(o){
1039 // if(typeof o.display_name !== 'string'){
1040 // for(var i=0;i<o.display_name.length;i++){
1041 // this.addItem({'id':o.id[i],'display_name':o.display_name[i]});
1048 var dv = ''; // display value
1049 var vv = ''; // value value..
1051 if (this.displayField) {
1052 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1054 // this is an error condition!!!
1055 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1058 if(this.valueField){
1059 vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
1062 if(this.hiddenField){
1063 this.hiddenField.dom.value = vv;
1065 this.lastSelectionText = dv;
1066 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1070 // no hidden field.. - we store the value in 'value', but still display
1071 // display field!!!!
1072 this.lastSelectionText = dv;
1073 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1080 // overridden so that last data is reset..
1081 this.setValue(this.originalValue);
1082 this.clearInvalid();
1083 this.lastData = false;
1085 this.view.clearSelections();
1089 findRecord : function(prop, value){
1091 if(this.store.getCount() > 0){
1092 this.store.each(function(r){
1093 if(r.data[prop] == value){
1105 // returns hidden if it's set..
1106 if (!this.rendered) {return ''};
1107 return !this.hiddenName && this.inputEl().dom.name ? this.inputEl().dom.name : (this.hiddenName || '');
1111 onViewMove : function(e, t){
1112 this.inKeyMode = false;
1116 onViewOver : function(e, t){
1117 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1120 var item = this.view.findItemFromChild(t);
1123 var index = this.view.indexOf(item);
1124 this.select(index, false);
1129 onViewClick : function(view, doFocus, el, e)
1131 var index = this.view.getSelectedIndexes()[0];
1133 var r = this.store.getAt(index);
1137 if(e.getTarget().nodeName.toLowerCase() != 'input'){
1144 Roo.each(this.tickItems, function(v,k){
1146 if(typeof(v) != 'undefined' && v[_this.valueField] == r.data[_this.valueField]){
1147 _this.tickItems.splice(k, 1);
1157 this.tickItems.push(r.data);
1162 this.onSelect(r, index);
1164 if(doFocus !== false && !this.blockFocus){
1165 this.inputEl().focus();
1170 restrictHeight : function(){
1171 //this.innerList.dom.style.height = '';
1172 //var inner = this.innerList.dom;
1173 //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
1174 //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
1175 //this.list.beginUpdate();
1176 //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
1177 this.list.alignTo(this.inputEl(), this.listAlign);
1178 this.list.alignTo(this.inputEl(), this.listAlign);
1179 //this.list.endUpdate();
1183 onEmptyResults : function(){
1188 * Returns true if the dropdown list is expanded, else false.
1190 isExpanded : function(){
1191 return this.list.isVisible();
1195 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1196 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1197 * @param {String} value The data value of the item to select
1198 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1199 * selected item if it is not currently in view (defaults to true)
1200 * @return {Boolean} True if the value matched an item in the list, else false
1202 selectByValue : function(v, scrollIntoView){
1203 if(v !== undefined && v !== null){
1204 var r = this.findRecord(this.valueField || this.displayField, v);
1206 this.select(this.store.indexOf(r), scrollIntoView);
1214 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1215 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1216 * @param {Number} index The zero-based index of the list item to select
1217 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1218 * selected item if it is not currently in view (defaults to true)
1220 select : function(index, scrollIntoView){
1221 this.selectedIndex = index;
1222 this.view.select(index);
1223 if(scrollIntoView !== false){
1224 var el = this.view.getNode(index);
1225 if(el && !this.multiple && !this.tickable){
1226 this.list.scrollChildIntoView(el, false);
1232 selectNext : function(){
1233 var ct = this.store.getCount();
1235 if(this.selectedIndex == -1){
1237 }else if(this.selectedIndex < ct-1){
1238 this.select(this.selectedIndex+1);
1244 selectPrev : function(){
1245 var ct = this.store.getCount();
1247 if(this.selectedIndex == -1){
1249 }else if(this.selectedIndex != 0){
1250 this.select(this.selectedIndex-1);
1256 onKeyUp : function(e){
1257 if(this.editable !== false && !e.isSpecialKey()){
1258 this.lastKey = e.getKey();
1259 this.dqTask.delay(this.queryDelay);
1264 validateBlur : function(){
1265 return !this.list || !this.list.isVisible();
1269 initQuery : function(){
1270 this.doQuery(this.getRawValue());
1274 doForce : function(){
1275 if(this.inputEl().dom.value.length > 0){
1276 this.inputEl().dom.value =
1277 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
1283 * Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
1284 * query allowing the query action to be canceled if needed.
1285 * @param {String} query The SQL query to execute
1286 * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
1287 * in the field than the minimum specified by the minChars config option. It also clears any filter previously
1288 * saved in the current store (defaults to false)
1290 doQuery : function(q, forceAll){
1292 if(q === undefined || q === null){
1301 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1306 forceAll = qe.forceAll;
1307 if(forceAll === true || (q.length >= this.minChars)){
1309 this.hasQuery = true;
1311 if(this.lastQuery != q || this.alwaysQuery){
1313 if(this.mode == 'local'){
1314 this.selectedIndex = -1;
1316 this.store.clearFilter();
1318 this.store.filter(this.displayField, q);
1322 this.store.baseParams[this.queryParam] = q;
1324 var options = {params : this.getParams(q)};
1328 options.params.start = this.page * this.pageSize;
1331 this.store.load(options);
1333 * this code will make the page width larger, at the beginning, the list not align correctly,
1334 * we should expand the list on onLoad
1340 this.selectedIndex = -1;
1345 this.loadNext = false;
1349 getParams : function(q){
1351 //p[this.queryParam] = q;
1355 p.limit = this.pageSize;
1361 * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1363 collapse : function(){
1364 if(!this.isExpanded()){
1368 this.hasFocus = false;
1374 this.cancelBtn.hide();
1375 this.trigger.show();
1378 Roo.get(document).un('mousedown', this.collapseIf, this);
1379 Roo.get(document).un('mousewheel', this.collapseIf, this);
1380 if (!this.editable) {
1381 Roo.get(document).un('keydown', this.listKeyPress, this);
1383 this.fireEvent('collapse', this);
1387 collapseIf : function(e){
1388 var in_combo = e.within(this.el);
1389 var in_list = e.within(this.list);
1390 var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
1392 if (in_combo || in_list || is_list) {
1393 //e.stopPropagation();
1398 this.onTickableFooterButtonClick(e, false, false);
1406 * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1408 expand : function(){
1410 if(this.isExpanded() || !this.hasFocus){
1417 this.restrictHeight();
1421 this.tickItems = Roo.apply([], this.item);
1424 this.cancelBtn.show();
1425 this.trigger.hide();
1429 Roo.get(document).on('mousedown', this.collapseIf, this);
1430 Roo.get(document).on('mousewheel', this.collapseIf, this);
1431 if (!this.editable) {
1432 Roo.get(document).on('keydown', this.listKeyPress, this);
1435 this.fireEvent('expand', this);
1439 // Implements the default empty TriggerField.onTriggerClick function
1440 onTriggerClick : function(e)
1442 Roo.log('trigger click');
1444 if(this.disabled || !this.triggerList){
1449 this.loadNext = false;
1451 if(this.isExpanded()){
1453 if (!this.blockFocus) {
1454 this.inputEl().focus();
1458 this.hasFocus = true;
1459 if(this.triggerAction == 'all') {
1460 this.doQuery(this.allQuery, true);
1462 this.doQuery(this.getRawValue());
1464 if (!this.blockFocus) {
1465 this.inputEl().focus();
1470 onTickableTriggerClick : function(e)
1477 this.loadNext = false;
1478 this.hasFocus = true;
1480 if(this.triggerAction == 'all') {
1481 this.doQuery(this.allQuery, true);
1483 this.doQuery(this.getRawValue());
1487 onSearchFieldClick : function(e)
1489 if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1494 this.loadNext = false;
1495 this.hasFocus = true;
1497 if(this.triggerAction == 'all') {
1498 this.doQuery(this.allQuery, true);
1500 this.doQuery(this.getRawValue());
1504 listKeyPress : function(e)
1506 //Roo.log('listkeypress');
1507 // scroll to first matching element based on key pres..
1508 if (e.isSpecialKey()) {
1511 var k = String.fromCharCode(e.getKey()).toUpperCase();
1514 var csel = this.view.getSelectedNodes();
1515 var cselitem = false;
1517 var ix = this.view.indexOf(csel[0]);
1518 cselitem = this.store.getAt(ix);
1519 if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1525 this.store.each(function(v) {
1527 // start at existing selection.
1528 if (cselitem.id == v.id) {
1534 if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1535 match = this.store.indexOf(v);
1541 if (match === false) {
1542 return true; // no more action?
1545 this.view.select(match);
1546 var sn = Roo.get(this.view.getSelectedNodes()[0])
1547 //sn.scrollIntoView(sn.dom.parentNode, false);
1550 onViewScroll : function(e, t){
1552 if(this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1556 this.hasQuery = true;
1558 this.loading = this.list.select('.loading', true).first();
1560 if(this.loading === null){
1561 this.list.createChild({
1563 cls: 'loading select2-more-results select2-active',
1564 html: 'Loading more results...'
1567 this.loading = this.list.select('.loading', true).first();
1569 this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1571 this.loading.hide();
1574 this.loading.show();
1579 this.loadNext = true;
1581 (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1586 addItem : function(o)
1588 var dv = ''; // display value
1590 if (this.displayField) {
1591 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1593 // this is an error condition!!!
1594 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1601 var choice = this.choices.createChild({
1603 cls: 'select2-search-choice',
1612 cls: 'select2-search-choice-close',
1617 }, this.searchField);
1619 var close = choice.select('a.select2-search-choice-close', true).first()
1621 close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
1629 this.inputEl().dom.value = '';
1633 onRemoveItem : function(e, _self, o)
1636 var index = this.item.indexOf(o.data) * 1;
1639 Roo.log('not this item?!');
1643 this.item.splice(index, 1);
1648 this.fireEvent('remove', this, e);
1652 syncValue : function()
1654 if(!this.item.length){
1661 Roo.each(this.item, function(i){
1662 if(_this.valueField){
1663 value.push(i[_this.valueField]);
1670 this.value = value.join(',');
1672 if(this.hiddenField){
1673 this.hiddenField.dom.value = this.value;
1677 clearItem : function()
1685 Roo.each(this.choices.select('>li.select2-search-choice', true).elements, function(c){
1692 inputEl: function ()
1695 return this.searchField;
1697 return this.el.select('input.form-control',true).first();
1701 onTickableFooterButtonClick : function(e, btn, el)
1705 if(btn && btn.name == 'cancel'){
1706 this.tickItems = Roo.apply([], this.item);
1715 Roo.each(this.tickItems, function(o){
1726 * @cfg {Boolean} grow
1730 * @cfg {Number} growMin
1734 * @cfg {Number} growMax