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...
52 Roo.extend(Roo.form.Hidden, Roo.form.TextField, {
57 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField
60 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
65 // behavies liek a hiddne field
68 * @cfg {Number} width The width of the box that displays the selected element
75 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
79 * @cfg {String} name The hidden name of the field, often contains an comma seperated list of names
84 // private the array of items that are displayed..
86 // private - the hidden field el.
89 boxWidth : 200, // use to set the box around the entry..
91 //validateValue : function() { return true; }, // all values are ok!
92 //onAddClick: function() { },
94 onRender : function(ct, position)
97 // give fake names to child combo;
98 Roo.form.ComboBox.superclass.onRender.call(this, ct, position);
100 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
101 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
104 this.combo = Roo.factory(this.combo,Roo.form);
107 this.combo.onRender(ct, position);
109 this.combo.wrap.addClass('x-cbarray-grp');
111 var cbwrap = this.combo.wrap.createChild(
112 {tag: 'div', cls: 'x-cbarray-cb'},
117 this.hiddenEl = this.combo.wrap.createChild({
118 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
120 // this.el.dom.removeAttribute("name");
123 this.outerWrap = this.combo.wrap;
126 this.outerWrap.setWidth(this.boxWidth);
127 this.outerWrap.dom.removeChild(this.el.dom);
129 this.wrap.dom.appendChild(this.el.dom);
130 this.outerWrap.dom.removeChild(this.trigger.dom);
131 this.combo.wrap.dom.appendChild(this.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.hiddenListName;
179 onResize: function(w, h){
180 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
182 if(typeof w != 'number'){
183 // we do not handle it!?!?
186 var tw = this.trigger.getWidth();
187 tw += this.addicon ? this.addicon.getWidth() : 0;
188 tw += this.editicon ? this.editicon.getWidth() : 0;
190 this.el.setWidth( this.adjustWidth('input', x));
192 this.trigger.setStyle('left', '0px');
194 if(this.list && this.listWidth === undefined){
195 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
196 this.list.setWidth(lw);
197 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
204 addItem: function(rec)
206 if (this.items.indexOfKey(rec[this.idField]) > -1) {
207 //console.log("GOT " + rec.data.id);
211 var x = new Roo.form.ComboBoxArray.Item({
212 //id : rec[this.idField],
214 nameField : this.nameField,
215 tipField : this.tipField,
219 this.items.add(rec[this.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.idField;
234 this.items.each(function(f) {
235 ar.push(f.data[idField]);
238 this.hiddenEl.dom.value = ar.join(',');
244 Roo.form.ComboBoxArray.superclass.reset.call(this);
245 this.items.each(function(f) {
249 this.hiddenEl.dom.value = '';
255 return this.hiddenEl ? this.hiddenEl.dom.value : '';
257 setValue: function(v) // not a valid action - must use addItems..
259 if (typeof(v) != 'object') {
263 Roo.each(v, function(l) {
268 validateValue : function(value){
269 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
278 * @class Roo.form.ComboBoxArray.Item
279 * @extends Roo.BoxComponent
280 * A selected item in the list
281 * Fred [x] Brian [x] [Pick another |v]
285 * @param {Object} config Configuration options
288 Roo.form.ComboBoxArray.Item = function(config) {
289 config.id = Roo.id();
290 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
293 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
296 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
298 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
304 onRender : function(ct, position){
305 Roo.form.Field.superclass.onRender.call(this, ct, position);
307 var cfg = this.getAutoCreate();
308 this.el = ct.createChild(cfg, position);
310 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
312 this.el.child('div').dom.innerHTML = this.cb.renderer ?
313 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
314 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
316 this.el.child('img').on('click', this.remove, this);
323 this.cb.items.remove(this);
324 this.el.child('img').un('click', this.remove, this);
326 this.cb.updateHiddenEl();