2 * Copyright(c) 2010-2012, Roo J Solutions Limited
9 * @class Roo.form.ComboBoxArray
10 * @extends Roo.form.ComboBox
11 * A facebook style adder... for lists of email / people / countries etc...
12 * pick multiple items from a combo box, and shows each one.
14 * Fred [x] Brian [x] [Pick another |v]
17 * For this to work: it needs various extra information
18 * - normal combo problay has
20 * + displayField, valueField
25 * If we change from 'extends' to wrapping...
32 * Create a new ComboBoxArray.
33 * @param {Object} config Configuration options
37 Roo.form.ComboBoxArray = function(config)
40 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
42 this.items = new Roo.util.MixedCollection(false);
44 // construct the child combo...
52 Roo.extend(Roo.form.Hidden, Roo.form.TextField, {
57 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField
60 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
65 // behavies liek a hiddne field
68 * @cfg {Number} width The width of the box that displays the selected element
75 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
79 * @cfg {String} name The hidden name of the field, often contains an comma seperated list of names
84 // private the array of items that are displayed..
86 // private - the hidden field el.
89 boxWidth : 200, // use to set the box around the entry..
91 //validateValue : function() { return true; }, // all values are ok!
92 //onAddClick: function() { },
94 onRender : function(ct, position)
97 // give fake names to child combo;
98 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
100 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
101 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
103 this.combo = Roo.factory(this.combo, Roo.form);
104 this.combo.onRender(ct, position);
107 this.combo.wrap.addClass('x-cbarray-grp');
109 var cbwrap = this.combo.wrap.createChild(
110 {tag: 'div', cls: 'x-cbarray-cb'},
115 this.hiddenEl = this.combo.wrap.createChild({
116 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
118 // this.el.dom.removeAttribute("name");
121 this.outerWrap = this.combo.wrap;
124 this.outerWrap.setWidth(this.boxWidth);
125 this.outerWrap.dom.removeChild(this.el.dom);
127 this.wrap.dom.appendChild(this.el.dom);
128 this.outerWrap.dom.removeChild(this.trigger.dom);
129 this.combo.wrap.dom.appendChild(this.trigger.dom);
131 this.combo.trigger.setStyle('position','relative');
132 this.combo.trigger.setStyle('left', '0px');
133 this.combo.trigger.setStyle('top', '2px');
135 this.combo.el.setStyle('vertical-align', 'text-bottom');
137 //this.trigger.setStyle('vertical-align', 'top');
139 // this should use the code from combo really... on('add' ....)
143 this.adder = this.outerWrap.createChild(
144 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
146 this.adder.on('click', function(e) {
147 _t.fireEvent('adderclick', this, e);
151 //this.adder.on('click', this.onAddClick, _t);
154 this.combo.on('select', function(cb, rec, ix) {
155 this.addItem(rec.data);
158 cb.el.dom.value = '';
159 //cb.lastData = rec.data;
170 // returns hidden if it's set..
171 if (!this.rendered) {return ''};
172 return this.hiddenListName;
177 onResize: function(w, h){
178 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
180 if(typeof w != 'number'){
181 // we do not handle it!?!?
184 var tw = this.trigger.getWidth();
185 tw += this.addicon ? this.addicon.getWidth() : 0;
186 tw += this.editicon ? this.editicon.getWidth() : 0;
188 this.el.setWidth( this.adjustWidth('input', x));
190 this.trigger.setStyle('left', '0px');
192 if(this.list && this.listWidth === undefined){
193 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
194 this.list.setWidth(lw);
195 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
202 addItem: function(rec)
204 if (this.items.indexOfKey(rec[this.idField]) > -1) {
205 //console.log("GOT " + rec.data.id);
209 var x = new Roo.form.ComboBoxArray.Item({
210 //id : rec[this.idField],
212 nameField : this.nameField,
213 tipField : this.tipField,
217 this.items.add(rec[this.idField],x);
218 // add it before the element..
219 this.updateHiddenEl();
220 x.render(this.outerWrap, this.wrap.dom);
221 // add the image handler..
224 updateHiddenEl : function()
227 if (!this.hiddenEl) {
231 var idField = this.idField;
232 this.items.each(function(f) {
233 ar.push(f.data[idField]);
236 this.hiddenEl.dom.value = ar.join(',');
242 Roo.form.ComboBoxArray.superclass.reset.call(this);
243 this.items.each(function(f) {
247 this.hiddenEl.dom.value = '';
253 return this.hiddenEl ? this.hiddenEl.dom.value : '';
255 setValue: function(v) // not a valid action - must use addItems..
257 if (typeof(v) != 'object') {
261 Roo.each(v, function(l) {
266 validateValue : function(value){
267 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
276 * @class Roo.form.ComboBoxArray.Item
277 * @extends Roo.BoxComponent
278 * A selected item in the list
279 * Fred [x] Brian [x] [Pick another |v]
283 * @param {Object} config Configuration options
286 Roo.form.ComboBoxArray.Item = function(config) {
287 config.id = Roo.id();
288 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
291 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
294 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
296 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
302 onRender : function(ct, position){
303 Roo.form.Field.superclass.onRender.call(this, ct, position);
305 var cfg = this.getAutoCreate();
306 this.el = ct.createChild(cfg, position);
308 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
310 this.el.child('div').dom.innerHTML = this.cb.renderer ?
311 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
312 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
314 this.el.child('img').on('click', this.remove, this);
321 this.cb.items.remove(this);
322 this.el.child('img').un('click', this.remove, this);
324 this.cb.updateHiddenEl();