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.onResize(this.combo.width,0);
105 this.combo.initEvents();
107 // assigned so form know we need to do this..
108 this.store = this.combo.store;
109 this.valueField = this.combo.valueField;
110 this.displayField = this.combo.displayField ;
113 this.combo.wrap.addClass('x-cbarray-grp');
115 var cbwrap = this.combo.wrap.createChild(
116 {tag: 'div', cls: 'x-cbarray-cb'},
121 this.hiddenEl = this.combo.wrap.createChild({
122 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
124 this.el = this.combo.wrap.createChild({
125 tag: 'input', type:'hidden' , name: this.name, value : ''
127 // this.el.dom.removeAttribute("name");
130 this.outerWrap = this.combo.wrap;
133 this.outerWrap.setWidth(this.width);
134 this.outerWrap.dom.removeChild(this.el.dom);
136 this.wrap.dom.appendChild(this.el.dom);
137 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
138 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
140 this.combo.trigger.setStyle('position','relative');
141 this.combo.trigger.setStyle('left', '0px');
142 this.combo.trigger.setStyle('top', '2px');
144 this.combo.el.setStyle('vertical-align', 'text-bottom');
146 //this.trigger.setStyle('vertical-align', 'top');
148 // this should use the code from combo really... on('add' ....)
152 this.adder = this.outerWrap.createChild(
153 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
155 this.adder.on('click', function(e) {
156 _t.fireEvent('adderclick', this, e);
160 //this.adder.on('click', this.onAddClick, _t);
163 this.combo.on('select', function(cb, rec, ix) {
164 this.addItem(rec.data);
167 cb.el.dom.value = '';
168 //cb.lastData = rec.data;
179 // returns hidden if it's set..
180 if (!this.rendered) {return ''};
181 return this.hiddenName ? this.hiddenName : this.name;
186 onResize: function(w, h){
189 // not sure if this is needed..
190 //this.combo.onResize(w,h);
192 if(typeof w != 'number'){
193 // we do not handle it!?!?
196 var tw = this.combo.trigger.getWidth();
197 tw += this.addicon ? this.addicon.getWidth() : 0;
198 tw += this.editicon ? this.editicon.getWidth() : 0;
200 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
202 this.combo.trigger.setStyle('left', '0px');
204 if(this.list && this.listWidth === undefined){
205 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
206 this.list.setWidth(lw);
207 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
214 addItem: function(rec)
216 var valueField = this.combo.valueField;
217 var displayField = this.combo.displayField;
218 if (this.items.indexOfKey(rec[valueField]) > -1) {
219 //console.log("GOT " + rec.data.id);
223 var x = new Roo.form.ComboBoxArray.Item({
224 //id : rec[this.idField],
226 displayField : displayField ,
227 tipField : displayField ,
231 this.items.add(rec[valueField],x);
232 // add it before the element..
233 this.updateHiddenEl();
234 x.render(this.outerWrap, this.wrap.dom);
235 // add the image handler..
238 updateHiddenEl : function()
241 if (!this.hiddenEl) {
245 var idField = this.combo.valueField;
247 this.items.each(function(f) {
248 ar.push(f.data[idField]);
251 this.hiddenEl.dom.value = ar.join(',');
257 //Roo.form.ComboBoxArray.superclass.reset.call(this);
258 this.items.each(function(f) {
261 this.el.dom.value = '';
263 this.hiddenEl.dom.value = '';
269 return this.hiddenEl ? this.hiddenEl.dom.value : '';
271 setValue: function(v) // not a valid action - must use addItems..
278 if (this.store.isLocal && (typeof(v) == 'string')) {
279 // then we can use the store to find the values..
280 // comma seperated at present.. this needs to allow JSON based encoding..
281 this.hiddenEl.value = v;
283 Roo.each(v.split(','), function(k) {
284 Roo.log("CHECK " + this.valueField + ',' + k);
285 var li = this.store.query(this.valueField, k);
290 add[this.valueField] = k;
291 add[this.displayField] = li.item(0).data[this.displayField];
297 if (typeof(v) == 'object') {
298 // then let's assume it's an array of objects..
299 Roo.each(v, function(l) {
307 setFromData: function(v)
309 // this recieves an object, if setValues is called.
311 this.el.dom.value = v[this.displayField];
312 this.hiddenEl.dom.value = v[this.valueField];
313 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
316 var kv = v[this.valueField];
317 var dv = v[this.displayField];
318 kv = typeof(kv) != 'string' ? '' : kv;
319 dv = typeof(dv) != 'string' ? '' : dv;
322 var keys = kv.split(',');
323 var display = dv.split(',');
324 for (var i = 0 ; i < keys.length; i++) {
327 add[this.valueField] = keys[i];
328 add[this.displayField] = display[i];
336 validateValue : function(value){
337 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
346 * @class Roo.form.ComboBoxArray.Item
347 * @extends Roo.BoxComponent
348 * A selected item in the list
349 * Fred [x] Brian [x] [Pick another |v]
353 * @param {Object} config Configuration options
356 Roo.form.ComboBoxArray.Item = function(config) {
357 config.id = Roo.id();
358 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
361 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
364 displayField : false,
368 defaultAutoCreate : {
370 cls: 'x-cbarray-item',
377 src : Roo.BLANK_IMAGE_URL ,
385 onRender : function(ct, position)
387 Roo.form.Field.superclass.onRender.call(this, ct, position);
390 var cfg = this.getAutoCreate();
391 this.el = ct.createChild(cfg, position);
394 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
396 this.el.child('div').dom.innerHTML = this.cb.renderer ?
397 this.cb.renderer(this.data) :
398 String.format('{0}',this.data[this.displayField]);
401 this.el.child('div').dom.setAttribute('qtip',
402 String.format('{0}',this.data[this.tipField])
405 this.el.child('img').on('click', this.remove, this);
412 this.cb.items.remove(this);
413 this.el.child('img').un('click', this.remove, this);
415 this.cb.updateHiddenEl();