2 * Copyright(c) 2010-2012, Roo J Solutions Limited
9 * @class Roo.form.ComboBoxArray
10 * @extends Roo.form.TextField
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} hiddenName 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.
85 // private - the filed 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);
104 this.combo.initEvents();
106 // assigned so form know we need to do this..
107 this.store = this.combo.store;
108 this.valueField = this.combo.valueField;
109 this.displayField = this.combo.displayField ;
112 this.combo.wrap.addClass('x-cbarray-grp');
114 var cbwrap = this.combo.wrap.createChild(
115 {tag: 'div', cls: 'x-cbarray-cb'},
120 this.hiddenEl = this.combo.wrap.createChild({
121 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
123 this.el = this.combo.wrap.createChild({
124 tag: 'input', type:'hidden' , name: this.name, value : ''
126 // this.el.dom.removeAttribute("name");
129 this.outerWrap = this.combo.wrap;
132 this.outerWrap.setWidth(this.width);
133 this.outerWrap.dom.removeChild(this.el.dom);
135 this.wrap.dom.appendChild(this.el.dom);
136 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
137 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
139 this.combo.trigger.setStyle('position','relative');
140 this.combo.trigger.setStyle('left', '0px');
141 this.combo.trigger.setStyle('top', '2px');
143 this.combo.el.setStyle('vertical-align', 'text-bottom');
145 //this.trigger.setStyle('vertical-align', 'top');
147 // this should use the code from combo really... on('add' ....)
151 this.adder = this.outerWrap.createChild(
152 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
154 this.adder.on('click', function(e) {
155 _t.fireEvent('adderclick', this, e);
159 //this.adder.on('click', this.onAddClick, _t);
162 this.combo.on('select', function(cb, rec, ix) {
163 this.addItem(rec.data);
166 cb.el.dom.value = '';
167 //cb.lastData = rec.data;
178 // returns hidden if it's set..
179 if (!this.rendered) {return ''};
180 return this.hiddenName ? this.hiddenName : this.name;
185 onResize: function(w, h){
188 // not sure if this is needed..
189 //this.combo.onResize(w,h);
191 if(typeof w != 'number'){
192 // we do not handle it!?!?
195 var tw = this.combo.trigger.getWidth();
196 tw += this.addicon ? this.addicon.getWidth() : 0;
197 tw += this.editicon ? this.editicon.getWidth() : 0;
199 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
201 this.combo.trigger.setStyle('left', '0px');
203 if(this.list && this.listWidth === undefined){
204 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
205 this.list.setWidth(lw);
206 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
213 addItem: function(rec)
215 var valueField = this.combo.valueField;
216 var displayField = this.combo.displayField;
217 if (this.items.indexOfKey(rec[valueField]) > -1) {
218 //console.log("GOT " + rec.data.id);
222 var x = new Roo.form.ComboBoxArray.Item({
223 //id : rec[this.idField],
225 displayField : displayField ,
226 tipField : displayField ,
230 this.items.add(rec[valueField],x);
231 // add it before the element..
232 this.updateHiddenEl();
233 x.render(this.outerWrap, this.wrap.dom);
234 // add the image handler..
237 updateHiddenEl : function()
240 if (!this.hiddenEl) {
244 var idField = this.combo.valueField;
246 this.items.each(function(f) {
247 ar.push(f.data[idField]);
250 this.hiddenEl.dom.value = ar.join(',');
256 //Roo.form.ComboBoxArray.superclass.reset.call(this);
257 this.items.each(function(f) {
260 this.el.dom.value = '';
262 this.hiddenEl.dom.value = '';
268 return this.hiddenEl ? this.hiddenEl.dom.value : '';
270 setValue: function(v) // not a valid action - must use addItems..
277 if (this.store.isLocal && (typeof(v) == 'string')) {
278 // then we can use the store to find the values..
279 // comma seperated at present.. this needs to allow JSON based encoding..
280 this.hiddenEl.value = v;
282 Roo.each(v.split(','), function(k) {
283 Roo.log("CHECK " + this.valueField + ',' + k);
284 var li = this.store.query(this.valueField, k);
289 add[this.valueField] = k;
290 add[this.displayField] = li.item(0).data[this.displayField];
296 if (typeof(v) == 'object') {
297 // then let's assume it's an array of objects..
298 Roo.each(v, function(l) {
306 setFromData: function(v)
308 // this recieves an object, if setValues is called.
310 this.el.dom.value = v[this.displayField];
311 this.hiddenEl.dom.value = v[this.valueField];
312 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
315 var kv = v[this.valueField];
316 var dv = v[this.displayField];
317 kv = typeof(kv) != 'string' ? '' : kv;
318 dv = typeof(dv) != 'string' ? '' : dv;
321 var keys = kv.split(',');
322 var display = dv.split(',');
323 for (var i = 0 ; i < keys.length; i++) {
326 add[this.valueField] = keys[i];
327 add[this.displayField] = display[i];
335 validateValue : function(value){
336 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
345 * @class Roo.form.ComboBoxArray.Item
346 * @extends Roo.BoxComponent
347 * A selected item in the list
348 * Fred [x] Brian [x] [Pick another |v]
352 * @param {Object} config Configuration options
355 Roo.form.ComboBoxArray.Item = function(config) {
356 config.id = Roo.id();
357 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
360 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
363 displayField : false,
367 defaultAutoCreate : {
369 cls: 'x-cbarray-item',
376 src : Roo.BLANK_IMAGE_URL ,
384 onRender : function(ct, position)
386 Roo.form.Field.superclass.onRender.call(this, ct, position);
389 var cfg = this.getAutoCreate();
390 this.el = ct.createChild(cfg, position);
393 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
395 this.el.child('div').dom.innerHTML = this.cb.renderer ?
396 this.cb.renderer(this.data) :
397 String.format('{0}',this.data[this.displayField]);
400 this.el.child('div').dom.setAttribute('qtip',
401 String.format('{0}',this.data[this.tipField])
404 this.el.child('img').on('click', this.remove, this);
411 this.cb.items.remove(this);
412 this.el.child('img').un('click', this.remove, this);
414 this.cb.updateHiddenEl();