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...
48 this.on('select', function(cb, rec, ix) {
49 this.addItem(rec.data);
51 this.el.dom.value = '';
52 //cb.lastData = rec.data;
61 Roo.extend(Roo.form.ComboBoxArray, Roo.form.Field
64 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
72 * @cfg {String} nameField The field to take the 'descriptive' display name from
76 * @cfg {String} idField The field to take the hidden 'id' data from
84 //hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
86 * @cfg {String} hiddenListName The field to take the array of hidden 'id' data that is selected
88 hiddenListName : false,
92 * @cfg {Number} boxWidth The width of the box that displays the selected element
94 boxWidth : 200, // use to set the box around the entry..
97 //validateValue : function() { return true; }, // all values are ok!
98 //onAddClick: function() { },
100 onRender : function(ct, position)
102 if (!this.hiddenName) {
103 Roo.log("ERROR - ComboBox used without a hidden Name..");
106 this.hiddenListName = this.hiddenName;
107 //kludge the naming for the wrapped combo..
108 this.hiddenName += '-lastselected';
110 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
111 this.wrap.addClass('x-cbarray-grp');
112 var cbwrap = this.wrap.createChild(
113 {tag: 'div', cls: 'x-cbarray-cb'},
118 this.hiddenEl = this.wrap.createChild({
119 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
121 // this.el.dom.removeAttribute("name");
124 this.outerWrap = this.wrap;
126 this.outerWrap.setWidth(this.boxWidth);
127 this.outerWrap.dom.removeChild(this.el.dom);
128 this.wrap.dom.appendChild(this.el.dom);
129 this.outerWrap.dom.removeChild(this.trigger.dom);
130 this.wrap.dom.appendChild(this.trigger.dom);
132 this.trigger.setStyle('position','relative');
133 this.trigger.setStyle('left', '0px');
134 this.trigger.setStyle('top', '2px');
135 this.el.setStyle('vertical-align', 'text-bottom');
137 //this.trigger.setStyle('vertical-align', 'top');
139 // this should use the code from combo really... on('add' ....)
143 this.adder = this.outerWrap.createChild(
144 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
146 this.adder.on('click', function(e) {
147 _t.fireEvent('adderclick', this, e);
151 //this.adder.on('click', this.onAddClick, _t);
158 // returns hidden if it's set..
159 if (!this.rendered) {return ''};
160 return this.hiddenListName;
165 onResize: function(w, h){
166 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
168 if(typeof w != 'number'){
169 // we do not handle it!?!?
172 var tw = this.trigger.getWidth();
173 tw += this.addicon ? this.addicon.getWidth() : 0;
174 tw += this.editicon ? this.editicon.getWidth() : 0;
176 this.el.setWidth( this.adjustWidth('input', x));
178 this.trigger.setStyle('left', '0px');
180 if(this.list && this.listWidth === undefined){
181 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
182 this.list.setWidth(lw);
183 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
190 addItem: function(rec)
192 if (this.items.indexOfKey(rec[this.idField]) > -1) {
193 //console.log("GOT " + rec.data.id);
197 var x = new Roo.form.ComboBoxArray.Item({
198 //id : rec[this.idField],
200 nameField : this.nameField,
201 tipField : this.tipField,
205 this.items.add(rec[this.idField],x);
206 // add it before the element..
207 this.updateHiddenEl();
208 x.render(this.outerWrap, this.wrap.dom);
209 // add the image handler..
212 updateHiddenEl : function()
215 if (!this.hiddenEl) {
219 var idField = this.idField;
220 this.items.each(function(f) {
221 ar.push(f.data[idField]);
224 this.hiddenEl.dom.value = ar.join(',');
230 Roo.form.ComboBoxArray.superclass.reset.call(this);
231 this.items.each(function(f) {
235 this.hiddenEl.dom.value = '';
241 return this.hiddenEl ? this.hiddenEl.dom.value : '';
243 setValue: function(v) // not a valid action - must use addItems..
245 if (typeof(v) != 'object') {
249 Roo.each(v, function(l) {
254 validateValue : function(value){
255 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
264 * @class Roo.form.ComboBoxArray.Item
265 * @extends Roo.BoxComponent
266 * A selected item in the list
267 * Fred [x] Brian [x] [Pick another |v]
271 * @param {Object} config Configuration options
274 Roo.form.ComboBoxArray.Item = function(config) {
275 config.id = Roo.id();
276 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
279 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
282 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
284 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
290 onRender : function(ct, position){
291 Roo.form.Field.superclass.onRender.call(this, ct, position);
293 var cfg = this.getAutoCreate();
294 this.el = ct.createChild(cfg, position);
296 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
298 this.el.child('div').dom.innerHTML = this.cb.renderer ?
299 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
300 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
302 this.el.child('img').on('click', this.remove, this);
309 this.cb.items.remove(this);
310 this.el.child('img').un('click', this.remove, this);
312 this.cb.updateHiddenEl();