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();
685 this.searchField.on("click", this.onSearchFieldClick, this, {preventDefault:true});
687 this.trigger = this.el.select('.tickable-buttons > .btn-edit', true).first();
688 this.trigger.on("click", this.onTickableTriggerClick, this, {preventDefault:true});
690 this.okBtn = this.el.select('.tickable-buttons > .btn-ok', true).first();
691 this.cancelBtn = this.el.select('.tickable-buttons > .btn-cancel', true).first();
693 this.okBtn.on('click', this.onTickableFooterButtonClick, this, this.okBtn);
694 this.cancelBtn.on('click', this.onTickableFooterButtonClick, this, this.cancelBtn);
696 this.trigger.setVisibilityMode(Roo.Element.DISPLAY);
697 this.okBtn.setVisibilityMode(Roo.Element.DISPLAY);
698 this.cancelBtn.setVisibilityMode(Roo.Element.DISPLAY);
701 this.cancelBtn.hide();
706 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
707 _this.list.setWidth(lw);
710 this.list.on('mouseover', this.onViewOver, this);
711 this.list.on('mousemove', this.onViewMove, this);
713 this.list.on('scroll', this.onViewScroll, this);
716 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>';
719 this.view = new Roo.View(this.el.select('ul.dropdown-menu',true).first(), this.tpl, {
720 singleSelect:true, tickable:true, parent:this, store: this.store, selectedClass: this.selectedClass
723 //this.view.wrapEl.setDisplayed(false);
724 this.view.on('click', this.onViewClick, this);
728 this.store.on('beforeload', this.onBeforeLoad, this);
729 this.store.on('load', this.onLoad, this);
730 this.store.on('loadexception', this.onLoadException, this);
732 // this.keyNav = new Roo.KeyNav(this.inputEl(), {
733 // "up" : function(e){
734 // this.inKeyMode = true;
735 // this.selectPrev();
738 // "down" : function(e){
739 // if(!this.isExpanded()){
740 // this.onTriggerClick();
742 // this.inKeyMode = true;
743 // this.selectNext();
747 // "enter" : function(e){
748 //// this.onViewClick();
752 // if(this.fireEvent("specialkey", this, e)){
753 // this.onViewClick(false);
759 // "esc" : function(e){
763 // "tab" : function(e){
766 // if(this.fireEvent("specialkey", this, e)){
767 // this.onViewClick(false);
775 // doRelay : function(foo, bar, hname){
776 // if(hname == 'down' || this.scope.isExpanded()){
777 // return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
782 // forceKeyDown: true
786 this.queryDelay = Math.max(this.queryDelay || 10,
787 this.mode == 'local' ? 10 : 250);
790 this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
793 this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
797 onDestroy : function(){
799 this.view.setStore(null);
800 this.view.el.removeAllListeners();
801 this.view.el.remove();
802 this.view.purgeListeners();
805 this.list.dom.innerHTML = '';
809 this.store.un('beforeload', this.onBeforeLoad, this);
810 this.store.un('load', this.onLoad, this);
811 this.store.un('loadexception', this.onLoadException, this);
813 Roo.bootstrap.ComboBox.superclass.onDestroy.call(this);
817 fireKey : function(e){
818 if(e.isNavKeyPress() && !this.list.isVisible()){
819 this.fireEvent("specialkey", this, e);
824 onResize: function(w, h){
825 // Roo.bootstrap.ComboBox.superclass.onResize.apply(this, arguments);
827 // if(typeof w != 'number'){
828 // // we do not handle it!?!?
831 // var tw = this.trigger.getWidth();
832 // // tw += this.addicon ? this.addicon.getWidth() : 0;
833 // // tw += this.editicon ? this.editicon.getWidth() : 0;
835 // this.inputEl().setWidth( this.adjustWidth('input', x));
837 // //this.trigger.setStyle('left', x+'px');
839 // if(this.list && this.listWidth === undefined){
840 // var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
841 // this.list.setWidth(lw);
842 // this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
850 * Allow or prevent the user from directly editing the field text. If false is passed,
851 * the user will only be able to select from the items defined in the dropdown list. This method
852 * is the runtime equivalent of setting the 'editable' config option at config time.
853 * @param {Boolean} value True to allow the user to directly edit the field text
855 setEditable : function(value){
856 if(value == this.editable){
859 this.editable = value;
861 this.inputEl().dom.setAttribute('readOnly', true);
862 this.inputEl().on('mousedown', this.onTriggerClick, this);
863 this.inputEl().addClass('x-combo-noedit');
865 this.inputEl().dom.setAttribute('readOnly', false);
866 this.inputEl().un('mousedown', this.onTriggerClick, this);
867 this.inputEl().removeClass('x-combo-noedit');
873 onBeforeLoad : function(combo,opts){
878 this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
880 this.restrictHeight();
881 this.selectedIndex = -1;
887 this.hasQuery = false;
893 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
897 if(this.store.getCount() > 0){
899 this.restrictHeight();
900 if(this.lastQuery == this.allQuery){
901 if(this.editable && !this.tickable){
902 this.inputEl().dom.select();
904 if(!this.selectByValue(this.value, true) && this.autoFocus){
905 this.select(0, true);
911 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
912 this.taTask.delay(this.typeAheadDelay);
916 this.onEmptyResults();
922 onLoadException : function()
924 this.hasQuery = false;
926 if(typeof(this.loading) !== 'undefined' && this.loading !== null){
931 Roo.log(this.store.reader.jsonData);
932 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
934 //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
940 onTypeAhead : function(){
941 if(this.store.getCount() > 0){
942 var r = this.store.getAt(0);
943 var newValue = r.data[this.displayField];
944 var len = newValue.length;
945 var selStart = this.getRawValue().length;
948 this.setRawValue(newValue);
949 this.selectText(selStart, newValue.length);
955 onSelect : function(record, index){
957 if(this.fireEvent('beforeselect', this, record, index) !== false){
959 this.setFromData(index > -1 ? record.data : false);
962 this.fireEvent('select', this, record, index);
967 * Returns the currently selected field value or empty string if no value is set.
968 * @return {String} value The selected value
970 getValue : function(){
973 return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
977 return typeof this.value != 'undefined' ? this.value : '';
979 return Roo.bootstrap.ComboBox.superclass.getValue.call(this);
984 * Clears any text/value currently set in the field
986 clearValue : function(){
987 if(this.hiddenField){
988 this.hiddenField.dom.value = '';
991 this.setRawValue('');
992 this.lastSelectionText = '';
997 * Sets the specified value into the field. If the value finds a match, the corresponding record text
998 * will be displayed in the field. If the value does not match the data value of an existing item,
999 * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
1000 * Otherwise the field will be blank (although the value will still be set).
1001 * @param {String} value The value to match
1003 setValue : function(v){
1010 if(this.valueField){
1011 var r = this.findRecord(this.valueField, v);
1013 text = r.data[this.displayField];
1014 }else if(this.valueNotFoundText !== undefined){
1015 text = this.valueNotFoundText;
1018 this.lastSelectionText = text;
1019 if(this.hiddenField){
1020 this.hiddenField.dom.value = v;
1022 Roo.bootstrap.ComboBox.superclass.setValue.call(this, text);
1026 * @property {Object} the last set data for the element
1031 * Sets the value of the field based on a object which is related to the record format for the store.
1032 * @param {Object} value the value to set as. or false on reset?
1034 setFromData : function(o){
1041 var dv = ''; // display value
1042 var vv = ''; // value value..
1044 if (this.displayField) {
1045 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1047 // this is an error condition!!!
1048 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1051 if(this.valueField){
1052 vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
1055 if(this.hiddenField){
1056 this.hiddenField.dom.value = vv;
1058 this.lastSelectionText = dv;
1059 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1063 // no hidden field.. - we store the value in 'value', but still display
1064 // display field!!!!
1065 this.lastSelectionText = dv;
1066 Roo.bootstrap.ComboBox.superclass.setValue.call(this, dv);
1073 // overridden so that last data is reset..
1074 this.setValue(this.originalValue);
1075 this.clearInvalid();
1076 this.lastData = false;
1078 this.view.clearSelections();
1082 findRecord : function(prop, value){
1084 if(this.store.getCount() > 0){
1085 this.store.each(function(r){
1086 if(r.data[prop] == value){
1098 // returns hidden if it's set..
1099 if (!this.rendered) {return ''};
1100 return !this.hiddenName && this.inputEl().dom.name ? this.inputEl().dom.name : (this.hiddenName || '');
1104 onViewMove : function(e, t){
1105 this.inKeyMode = false;
1109 onViewOver : function(e, t){
1110 if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1113 var item = this.view.findItemFromChild(t);
1116 var index = this.view.indexOf(item);
1117 this.select(index, false);
1122 onViewClick : function(view, doFocus, el, e)
1124 var index = this.view.getSelectedIndexes()[0];
1126 var r = this.store.getAt(index);
1130 if(e.getTarget().nodeName.toLowerCase() != 'input'){
1137 Roo.each(this.tickItems, function(v,k){
1139 if(typeof(v) != 'undefined' && v[_this.valueField] == r.data[_this.valueField]){
1140 _this.tickItems.splice(k, 1);
1150 this.tickItems.push(r.data);
1155 this.onSelect(r, index);
1157 if(doFocus !== false && !this.blockFocus){
1158 this.inputEl().focus();
1163 restrictHeight : function(){
1164 //this.innerList.dom.style.height = '';
1165 //var inner = this.innerList.dom;
1166 //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
1167 //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
1168 //this.list.beginUpdate();
1169 //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
1170 this.list.alignTo(this.inputEl(), this.listAlign);
1171 //this.list.endUpdate();
1175 onEmptyResults : function(){
1180 * Returns true if the dropdown list is expanded, else false.
1182 isExpanded : function(){
1183 return this.list.isVisible();
1187 * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1188 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1189 * @param {String} value The data value of the item to select
1190 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1191 * selected item if it is not currently in view (defaults to true)
1192 * @return {Boolean} True if the value matched an item in the list, else false
1194 selectByValue : function(v, scrollIntoView){
1195 if(v !== undefined && v !== null){
1196 var r = this.findRecord(this.valueField || this.displayField, v);
1198 this.select(this.store.indexOf(r), scrollIntoView);
1206 * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1207 * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1208 * @param {Number} index The zero-based index of the list item to select
1209 * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1210 * selected item if it is not currently in view (defaults to true)
1212 select : function(index, scrollIntoView){
1213 this.selectedIndex = index;
1214 this.view.select(index);
1215 if(scrollIntoView !== false){
1216 var el = this.view.getNode(index);
1218 //this.innerList.scrollChildIntoView(el, false);
1225 selectNext : function(){
1226 var ct = this.store.getCount();
1228 if(this.selectedIndex == -1){
1230 }else if(this.selectedIndex < ct-1){
1231 this.select(this.selectedIndex+1);
1237 selectPrev : function(){
1238 var ct = this.store.getCount();
1240 if(this.selectedIndex == -1){
1242 }else if(this.selectedIndex != 0){
1243 this.select(this.selectedIndex-1);
1249 onKeyUp : function(e){
1250 if(this.editable !== false && !e.isSpecialKey()){
1251 this.lastKey = e.getKey();
1252 this.dqTask.delay(this.queryDelay);
1257 validateBlur : function(){
1258 return !this.list || !this.list.isVisible();
1262 initQuery : function(){
1263 this.doQuery(this.getRawValue());
1267 doForce : function(){
1268 if(this.inputEl().dom.value.length > 0){
1269 this.inputEl().dom.value =
1270 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
1276 * Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
1277 * query allowing the query action to be canceled if needed.
1278 * @param {String} query The SQL query to execute
1279 * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
1280 * in the field than the minimum specified by the minChars config option. It also clears any filter previously
1281 * saved in the current store (defaults to false)
1283 doQuery : function(q, forceAll){
1285 if(q === undefined || q === null){
1294 if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1299 forceAll = qe.forceAll;
1300 if(forceAll === true || (q.length >= this.minChars)){
1302 this.hasQuery = true;
1304 if(this.lastQuery != q || this.alwaysQuery){
1306 if(this.mode == 'local'){
1307 this.selectedIndex = -1;
1309 this.store.clearFilter();
1311 this.store.filter(this.displayField, q);
1315 this.store.baseParams[this.queryParam] = q;
1317 var options = {params : this.getParams(q)};
1321 options.params.start = this.page * this.pageSize;
1324 this.store.load(options);
1326 * this code will make the page width larger, at the beginning, the list not align correctly,
1327 * we should expand the list on onLoad
1333 this.selectedIndex = -1;
1338 this.loadNext = false;
1342 getParams : function(q){
1344 //p[this.queryParam] = q;
1348 p.limit = this.pageSize;
1354 * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1356 collapse : function(){
1357 if(!this.isExpanded()){
1361 this.hasFocus = false;
1367 this.cancelBtn.hide();
1368 this.trigger.show();
1371 Roo.get(document).un('mousedown', this.collapseIf, this);
1372 Roo.get(document).un('mousewheel', this.collapseIf, this);
1373 if (!this.editable) {
1374 Roo.get(document).un('keydown', this.listKeyPress, this);
1376 this.fireEvent('collapse', this);
1380 collapseIf : function(e){
1381 var in_combo = e.within(this.el);
1382 var in_list = e.within(this.list);
1384 if (in_combo || in_list) {
1385 //e.stopPropagation();
1390 this.onTickableFooterButtonClick(e, false, false);
1398 * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1400 expand : function(){
1402 if(this.isExpanded() || !this.hasFocus){
1406 this.list.alignTo(this.inputEl(), this.listAlign);
1411 this.tickItems = Roo.apply([], this.item);
1414 this.cancelBtn.show();
1415 this.trigger.hide();
1419 Roo.get(document).on('mousedown', this.collapseIf, this);
1420 Roo.get(document).on('mousewheel', this.collapseIf, this);
1421 if (!this.editable) {
1422 Roo.get(document).on('keydown', this.listKeyPress, this);
1425 this.fireEvent('expand', this);
1429 // Implements the default empty TriggerField.onTriggerClick function
1430 onTriggerClick : function(e)
1432 Roo.log('trigger click');
1439 this.loadNext = false;
1441 if(this.isExpanded()){
1443 if (!this.blockFocus) {
1444 this.inputEl().focus();
1448 this.hasFocus = true;
1449 if(this.triggerAction == 'all') {
1450 this.doQuery(this.allQuery, true);
1452 this.doQuery(this.getRawValue());
1454 if (!this.blockFocus) {
1455 this.inputEl().focus();
1460 onTickableTriggerClick : function(e)
1467 this.loadNext = false;
1468 this.hasFocus = true;
1470 if(this.triggerAction == 'all') {
1471 this.doQuery(this.allQuery, true);
1473 this.doQuery(this.getRawValue());
1477 onSearchFieldClick : function(e)
1479 if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1484 this.loadNext = false;
1485 this.hasFocus = true;
1487 if(this.triggerAction == 'all') {
1488 this.doQuery(this.allQuery, true);
1490 this.doQuery(this.getRawValue());
1494 listKeyPress : function(e)
1496 //Roo.log('listkeypress');
1497 // scroll to first matching element based on key pres..
1498 if (e.isSpecialKey()) {
1501 var k = String.fromCharCode(e.getKey()).toUpperCase();
1504 var csel = this.view.getSelectedNodes();
1505 var cselitem = false;
1507 var ix = this.view.indexOf(csel[0]);
1508 cselitem = this.store.getAt(ix);
1509 if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1515 this.store.each(function(v) {
1517 // start at existing selection.
1518 if (cselitem.id == v.id) {
1524 if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1525 match = this.store.indexOf(v);
1531 if (match === false) {
1532 return true; // no more action?
1535 this.view.select(match);
1536 var sn = Roo.get(this.view.getSelectedNodes()[0])
1537 //sn.scrollIntoView(sn.dom.parentNode, false);
1540 onViewScroll : function(e, t){
1542 if(this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1546 this.hasQuery = true;
1548 this.loading = this.list.select('.loading', true).first();
1550 if(this.loading === null){
1551 this.list.createChild({
1553 cls: 'loading select2-more-results select2-active',
1554 html: 'Loading more results...'
1557 this.loading = this.list.select('.loading', true).first();
1559 this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1561 this.loading.hide();
1564 this.loading.show();
1569 this.loadNext = true;
1571 (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1576 addItem : function(o)
1578 var dv = ''; // display value
1580 if (this.displayField) {
1581 dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1583 // this is an error condition!!!
1584 Roo.log('no displayField value set for '+ (this.name ? this.name : this.id));
1591 var choice = this.choices.createChild({
1593 cls: 'select2-search-choice',
1602 cls: 'select2-search-choice-close',
1607 }, this.searchField);
1609 var close = choice.select('a.select2-search-choice-close', true).first()
1611 close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
1619 this.inputEl().dom.value = '';
1623 onRemoveItem : function(e, _self, o)
1626 var index = this.item.indexOf(o.data) * 1;
1629 Roo.log('not this item?!');
1633 this.item.splice(index, 1);
1638 this.fireEvent('remove', this, e);
1642 syncValue : function()
1644 if(!this.item.length){
1651 Roo.each(this.item, function(i){
1652 if(_this.valueField){
1653 value.push(i[_this.valueField]);
1660 this.value = value.join(',');
1662 if(this.hiddenField){
1663 this.hiddenField.dom.value = this.value;
1667 clearItem : function()
1675 Roo.each(this.choices.select('>li.select2-search-choice', true).elements, function(c){
1682 inputEl: function ()
1685 return this.searchField;
1687 return this.el.select('input.form-control',true).first();
1691 onTickableFooterButtonClick : function(e, btn, el)
1695 if(btn && btn.name == 'cancel'){
1696 this.tickItems = Roo.apply([], this.item);
1705 Roo.each(this.tickItems, function(o){
1716 * @cfg {Boolean} grow
1720 * @cfg {Number} growMin
1724 * @cfg {Number} growMax