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 if (typeof(this.combo.width) != 'undefined') {
105 this.combo.onResize(this.combo.width,0);
108 this.combo.initEvents();
110 // assigned so form know we need to do this..
111 this.store = this.combo.store;
112 this.valueField = this.combo.valueField;
113 this.displayField = this.combo.displayField ;
116 this.combo.wrap.addClass('x-cbarray-grp');
118 var cbwrap = this.combo.wrap.createChild(
119 {tag: 'div', cls: 'x-cbarray-cb'},
124 this.hiddenEl = this.combo.wrap.createChild({
125 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
127 this.el = this.combo.wrap.createChild({
128 tag: 'input', type:'hidden' , name: this.name, value : ''
130 // this.el.dom.removeAttribute("name");
133 this.outerWrap = this.combo.wrap;
136 this.outerWrap.setWidth(this.width);
137 this.outerWrap.dom.removeChild(this.el.dom);
139 this.wrap.dom.appendChild(this.el.dom);
140 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
141 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
143 this.combo.trigger.setStyle('position','relative');
144 this.combo.trigger.setStyle('left', '0px');
145 this.combo.trigger.setStyle('top', '2px');
147 this.combo.el.setStyle('vertical-align', 'text-bottom');
149 //this.trigger.setStyle('vertical-align', 'top');
151 // this should use the code from combo really... on('add' ....)
155 this.adder = this.outerWrap.createChild(
156 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
158 this.adder.on('click', function(e) {
159 _t.fireEvent('adderclick', this, e);
163 //this.adder.on('click', this.onAddClick, _t);
166 this.combo.on('select', function(cb, rec, ix) {
167 this.addItem(rec.data);
170 cb.el.dom.value = '';
171 //cb.lastData = rec.data;
182 // returns hidden if it's set..
183 if (!this.rendered) {return ''};
184 return this.hiddenName ? this.hiddenName : this.name;
189 onResize: function(w, h){
192 // not sure if this is needed..
193 //this.combo.onResize(w,h);
195 if(typeof w != 'number'){
196 // we do not handle it!?!?
199 var tw = this.combo.trigger.getWidth();
200 tw += this.addicon ? this.addicon.getWidth() : 0;
201 tw += this.editicon ? this.editicon.getWidth() : 0;
203 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
205 this.combo.trigger.setStyle('left', '0px');
207 if(this.list && this.listWidth === undefined){
208 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
209 this.list.setWidth(lw);
210 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
217 addItem: function(rec)
219 var valueField = this.combo.valueField;
220 var displayField = this.combo.displayField;
221 if (this.items.indexOfKey(rec[valueField]) > -1) {
222 //console.log("GOT " + rec.data.id);
226 var x = new Roo.form.ComboBoxArray.Item({
227 //id : rec[this.idField],
229 displayField : displayField ,
230 tipField : displayField ,
234 this.items.add(rec[valueField],x);
235 // add it before the element..
236 this.updateHiddenEl();
237 x.render(this.outerWrap, this.wrap.dom);
238 // add the image handler..
241 updateHiddenEl : function()
244 if (!this.hiddenEl) {
248 var idField = this.combo.valueField;
250 this.items.each(function(f) {
251 ar.push(f.data[idField]);
254 this.hiddenEl.dom.value = ar.join(',');
260 //Roo.form.ComboBoxArray.superclass.reset.call(this);
261 this.items.each(function(f) {
264 this.el.dom.value = '';
266 this.hiddenEl.dom.value = '';
272 return this.hiddenEl ? this.hiddenEl.dom.value : '';
274 setValue: function(v) // not a valid action - must use addItems..
281 if (this.store.isLocal && (typeof(v) == 'string')) {
282 // then we can use the store to find the values..
283 // comma seperated at present.. this needs to allow JSON based encoding..
284 this.hiddenEl.value = v;
286 Roo.each(v.split(','), function(k) {
287 Roo.log("CHECK " + this.valueField + ',' + k);
288 var li = this.store.query(this.valueField, k);
293 add[this.valueField] = k;
294 add[this.displayField] = li.item(0).data[this.displayField];
300 if (typeof(v) == 'object') {
301 // then let's assume it's an array of objects..
302 Roo.each(v, function(l) {
310 setFromData: function(v)
312 // this recieves an object, if setValues is called.
314 this.el.dom.value = v[this.displayField];
315 this.hiddenEl.dom.value = v[this.valueField];
316 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
319 var kv = v[this.valueField];
320 var dv = v[this.displayField];
321 kv = typeof(kv) != 'string' ? '' : kv;
322 dv = typeof(dv) != 'string' ? '' : dv;
325 var keys = kv.split(',');
326 var display = dv.split(',');
327 for (var i = 0 ; i < keys.length; i++) {
330 add[this.valueField] = keys[i];
331 add[this.displayField] = display[i];
339 * Validates the combox array value
340 * @return {Boolean} True if the value is valid, else false
342 validate : function(){
343 if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
350 validateValue : function(value){
351 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
359 isDirty : function() {
365 var d = Roo.decode(String(this.originalValue));
367 return String(this.getValue()) !== String(this.originalValue);
370 var originalValue = [];
372 for (var i = 0; i < d.length; i++){
373 originalValue.push(d[i][this.valueField]);
376 return String(this.getValue()) !== String(originalValue.join(','));
385 * @class Roo.form.ComboBoxArray.Item
386 * @extends Roo.BoxComponent
387 * A selected item in the list
388 * Fred [x] Brian [x] [Pick another |v]
392 * @param {Object} config Configuration options
395 Roo.form.ComboBoxArray.Item = function(config) {
396 config.id = Roo.id();
397 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
400 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
403 displayField : false,
407 defaultAutoCreate : {
409 cls: 'x-cbarray-item',
416 src : Roo.BLANK_IMAGE_URL ,
424 onRender : function(ct, position)
426 Roo.form.Field.superclass.onRender.call(this, ct, position);
429 var cfg = this.getAutoCreate();
430 this.el = ct.createChild(cfg, position);
433 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
435 this.el.child('div').dom.innerHTML = this.cb.renderer ?
436 this.cb.renderer(this.data) :
437 String.format('{0}',this.data[this.displayField]);
440 this.el.child('div').dom.setAttribute('qtip',
441 String.format('{0}',this.data[this.tipField])
444 this.el.child('img').on('click', this.remove, this);
451 this.cb.items.remove(this);
452 this.el.child('img').un('click', this.remove, this);
454 this.cb.updateHiddenEl();