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.
86 boxWidth : 200, // use to set the box around the entry..
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.dom.removeAttribute("name");
120 this.outerWrap = this.combo.wrap;
123 this.outerWrap.setWidth(this.boxWidth);
124 this.outerWrap.dom.removeChild(this.el.dom);
126 this.wrap.dom.appendChild(this.el.dom);
127 this.outerWrap.dom.removeChild(this.trigger.dom);
128 this.combo.wrap.dom.appendChild(this.trigger.dom);
130 this.combo.trigger.setStyle('position','relative');
131 this.combo.trigger.setStyle('left', '0px');
132 this.combo.trigger.setStyle('top', '2px');
134 this.combo.el.setStyle('vertical-align', 'text-bottom');
136 //this.trigger.setStyle('vertical-align', 'top');
138 // this should use the code from combo really... on('add' ....)
142 this.adder = this.outerWrap.createChild(
143 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
145 this.adder.on('click', function(e) {
146 _t.fireEvent('adderclick', this, e);
150 //this.adder.on('click', this.onAddClick, _t);
153 this.combo.on('select', function(cb, rec, ix) {
154 this.addItem(rec.data);
157 cb.el.dom.value = '';
158 //cb.lastData = rec.data;
169 // returns hidden if it's set..
170 if (!this.rendered) {return ''};
171 return this.hiddenListName;
176 onResize: function(w, h){
178 this.combo.onResize(w,h);
180 if(typeof w != 'number'){
181 // we do not handle it!?!?
184 var tw = this.combo.trigger.getWidth();
185 tw += this.addicon ? this.addicon.getWidth() : 0;
186 tw += this.editicon ? this.editicon.getWidth() : 0;
188 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
190 this.combo.trigger.setStyle('left', '0px');
192 if(this.list && this.listWidth === undefined){
193 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
194 this.list.setWidth(lw);
195 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
202 addItem: function(rec)
204 var idField = this.combo.valueField;
205 var displayField = this.combo.displayField;
206 if (this.items.indexOfKey(rec[idField]) > -1) {
207 //console.log("GOT " + rec.data.id);
211 var x = new Roo.form.ComboBoxArray.Item({
212 //id : rec[this.idField],
214 nameField : displayField ,
215 tipField : displayField ,
219 this.items.add(rec[idField],x);
220 // add it before the element..
221 this.updateHiddenEl();
222 x.render(this.outerWrap, this.wrap.dom);
223 // add the image handler..
226 updateHiddenEl : function()
229 if (!this.hiddenEl) {
233 var idField = this.combo.valueField;
235 this.items.each(function(f) {
236 ar.push(f.data[idField]);
239 this.hiddenEl.dom.value = ar.join(',');
245 Roo.form.ComboBoxArray.superclass.reset.call(this);
246 this.items.each(function(f) {
250 this.hiddenEl.dom.value = '';
256 return this.hiddenEl ? this.hiddenEl.dom.value : '';
258 setValue: function(v) // not a valid action - must use addItems..
260 if (typeof(v) != 'object') {
264 Roo.each(v, function(l) {
269 validateValue : function(value){
270 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
279 * @class Roo.form.ComboBoxArray.Item
280 * @extends Roo.BoxComponent
281 * A selected item in the list
282 * Fred [x] Brian [x] [Pick another |v]
286 * @param {Object} config Configuration options
289 Roo.form.ComboBoxArray.Item = function(config) {
290 config.id = Roo.id();
291 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
294 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
301 defaultAutoCreate : {
303 cls: 'x-cbarray-item',
310 src : Roo.BLANK_IMAGE_URL ,
318 onRender : function(ct, position)
320 Roo.form.Field.superclass.onRender.call(this, ct, position);
323 var cfg = this.getAutoCreate();
324 this.el = ct.createChild(cfg, position);
327 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
329 this.el.child('div').dom.innerHTML = this.cb.renderer ?
330 this.cb.renderer(this.data) :
331 String.format('{0}',this.data[this.nameField]);
334 this.el.child('div').dom.setAttribute('qtip',
335 String.format('{0}',this.data[this.tipField])
338 this.el.child('img').on('click', this.remove, this);
345 this.cb.items.remove(this);
346 this.el.child('img').un('click', this.remove, this);
348 this.cb.updateHiddenEl();