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...
54 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
57 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
62 // behavies liek a hiddne field
65 * @cfg {Number} width The width of the box that displays the selected element
72 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
76 * @cfg {String} name The hidden name of the field, often contains an comma seperated list of names
81 // private the array of items that are displayed..
83 // private - the hidden field el.
88 //validateValue : function() { return true; }, // all values are ok!
89 //onAddClick: function() { },
91 onRender : function(ct, position)
94 // create the standard hidden element
95 //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
98 // give fake names to child combo;
99 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
100 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
102 this.combo = Roo.factory(this.combo, Roo.form);
103 this.combo.onRender(ct, position);
106 this.combo.wrap.addClass('x-cbarray-grp');
108 var cbwrap = this.combo.wrap.createChild(
109 {tag: 'div', cls: 'x-cbarray-cb'},
114 this.hiddenEl = this.combo.wrap.createChild({
115 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
117 this.el = this.combo.wrap.createChild({
118 tag: 'input', type:'hidden' , name: this.name, value : ''
120 // this.el.dom.removeAttribute("name");
123 this.outerWrap = this.combo.wrap;
126 this.outerWrap.setWidth(this.width);
127 this.outerWrap.dom.removeChild(this.el.dom);
129 this.wrap.dom.appendChild(this.el.dom);
130 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
131 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
133 this.combo.trigger.setStyle('position','relative');
134 this.combo.trigger.setStyle('left', '0px');
135 this.combo.trigger.setStyle('top', '2px');
137 this.combo.el.setStyle('vertical-align', 'text-bottom');
139 //this.trigger.setStyle('vertical-align', 'top');
141 // this should use the code from combo really... on('add' ....)
145 this.adder = this.outerWrap.createChild(
146 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
148 this.adder.on('click', function(e) {
149 _t.fireEvent('adderclick', this, e);
153 //this.adder.on('click', this.onAddClick, _t);
156 this.combo.on('select', function(cb, rec, ix) {
157 this.addItem(rec.data);
160 cb.el.dom.value = '';
161 //cb.lastData = rec.data;
172 // returns hidden if it's set..
173 if (!this.rendered) {return ''};
174 return this.hiddenName ? this.hiddenName : this.name;
179 onResize: function(w, h){
182 // not sure if this is needed..
183 this.combo.onResize(w,h);
185 if(typeof w != 'number'){
186 // we do not handle it!?!?
189 var tw = this.combo.trigger.getWidth();
190 tw += this.addicon ? this.addicon.getWidth() : 0;
191 tw += this.editicon ? this.editicon.getWidth() : 0;
193 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
195 this.combo.trigger.setStyle('left', '0px');
197 if(this.list && this.listWidth === undefined){
198 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
199 this.list.setWidth(lw);
200 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
207 addItem: function(rec)
209 var idField = this.combo.valueField;
210 var displayField = this.combo.displayField;
211 if (this.items.indexOfKey(rec[idField]) > -1) {
212 //console.log("GOT " + rec.data.id);
216 var x = new Roo.form.ComboBoxArray.Item({
217 //id : rec[this.idField],
219 nameField : displayField ,
220 tipField : displayField ,
224 this.items.add(rec[idField],x);
225 // add it before the element..
226 this.updateHiddenEl();
227 x.render(this.outerWrap, this.wrap.dom);
228 // add the image handler..
231 updateHiddenEl : function()
234 if (!this.hiddenEl) {
238 var idField = this.combo.valueField;
240 this.items.each(function(f) {
241 ar.push(f.data[idField]);
244 this.hiddenEl.dom.value = ar.join(',');
250 Roo.form.ComboBoxArray.superclass.reset.call(this);
251 this.items.each(function(f) {
255 this.hiddenEl.dom.value = '';
261 return this.hiddenEl ? this.hiddenEl.dom.value : '';
263 setValue: function(v) // not a valid action - must use addItems..
265 if (typeof(v) != 'object') {
271 Roo.each(v, function(l) {
276 validateValue : function(value){
277 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
286 * @class Roo.form.ComboBoxArray.Item
287 * @extends Roo.BoxComponent
288 * A selected item in the list
289 * Fred [x] Brian [x] [Pick another |v]
293 * @param {Object} config Configuration options
296 Roo.form.ComboBoxArray.Item = function(config) {
297 config.id = Roo.id();
298 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
301 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
308 defaultAutoCreate : {
310 cls: 'x-cbarray-item',
317 src : Roo.BLANK_IMAGE_URL ,
325 onRender : function(ct, position)
327 Roo.form.Field.superclass.onRender.call(this, ct, position);
330 var cfg = this.getAutoCreate();
331 this.el = ct.createChild(cfg, position);
334 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
336 this.el.child('div').dom.innerHTML = this.cb.renderer ?
337 this.cb.renderer(this.data) :
338 String.format('{0}',this.data[this.nameField]);
341 this.el.child('div').dom.setAttribute('qtip',
342 String.format('{0}',this.data[this.tipField])
345 this.el.child('img').on('click', this.remove, this);
352 this.cb.items.remove(this);
353 this.el.child('img').un('click', this.remove, this);
355 this.cb.updateHiddenEl();