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 * Create a new ComboBoxArray.
18 * @param {Object} config Configuration options
22 Roo.form.ComboBoxArray = function(config)
25 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
26 this.items = new Roo.util.MixedCollection(false);
27 this.on('select', function(cb, rec, ix) {
28 this.addItem(rec.data);
30 this.el.dom.value = '';
31 //cb.lastData = rec.data;
38 Roo.extend(Roo.form.ComboBoxArray, Roo.form.ComboBox,
51 hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
52 hiddenListName : false,
56 * @cfg {Number} boxWidth The width of the box that displays the selected element
58 boxWidth : 200, // use to set the box around the entry..
61 //validateValue : function() { return true; }, // all values are ok!
62 //onAddClick: function() { },
64 onRender : function(ct, position)
67 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
68 this.wrap.addClass('p-cblist-grp');
69 var cbwrap = this.wrap.createChild(
70 {tag: 'div', cls: 'p-cblist-cb'},
73 if (this.hiddenListName) {
75 this.hiddenEl = this.wrap.createChild({
76 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
78 // this.el.dom.removeAttribute("name");
81 this.outerWrap = this.wrap;
83 this.outerWrap.setWidth(this.boxWidth);
84 this.outerWrap.dom.removeChild(this.el.dom);
85 this.wrap.dom.appendChild(this.el.dom);
86 this.outerWrap.dom.removeChild(this.trigger.dom);
87 this.wrap.dom.appendChild(this.trigger.dom);
89 this.trigger.setStyle('position','relative');
90 this.trigger.setStyle('left', '0px');
91 this.trigger.setStyle('top', '2px');
92 this.el.setStyle('vertical-align', 'text-bottom');
94 //this.trigger.setStyle('vertical-align', 'top');
98 this.adder = this.outerWrap.createChild(
99 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
101 this.adder.on('click', function(e) {
102 _t.fireEvent('adderclick', this, e);
106 //this.adder.on('click', this.onAddClick, _t);
111 onResize: function(w, h){
112 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
114 if(typeof w != 'number'){
115 // we do not handle it!?!?
118 var tw = this.trigger.getWidth();
119 tw += this.addicon ? this.addicon.getWidth() : 0;
120 tw += this.editicon ? this.editicon.getWidth() : 0;
122 this.el.setWidth( this.adjustWidth('input', x));
124 this.trigger.setStyle('left', '0px');
126 if(this.list && this.listWidth === undefined){
127 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
128 this.list.setWidth(lw);
129 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
136 addItem: function(rec)
138 if (this.items.indexOfKey(rec[this.idField]) > -1) {
139 //console.log("GOT " + rec.data.id);
143 var x = new Roo.form.ComboBoxArray.Item({
144 //id : rec[this.idField],
146 nameField : this.nameField,
147 tipField : this.tipField,
151 this.items.add(rec[this.idField],x);
152 // add it before the element..
153 this.updateHiddenEl();
154 x.render(this.outerWrap, this.wrap.dom);
155 // add the image handler..
158 updateHiddenEl : function()
161 if (!this.hiddenEl) {
165 var idField = this.idField;
166 this.items.each(function(f) {
167 ar.push(f.data[idField]);
170 this.hiddenEl.dom.value = ar.join(',');
176 Roo.form.ComboBoxArray.superclass.reset.call(this);
177 this.items.each(function(f) {
181 this.hiddenEl.dom.value = '';
187 return this.hiddenEl ? this.hiddenEl.dom.value : '';
189 setValue: function(v) // not a valid action - must use addItems..
191 if (typeof(v) != 'object') {
195 Roo.each(v, function(l) {
200 validateValue : function(value){
201 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
210 * @class Roo.form.ComboBoxArray.Item
211 * @extends Roo.BoxComponent
212 * A selected item in the list
213 * Fred [x] Brian [x] [Pick another |v]
217 * @param {Object} config Configuration options
220 Roo.form.ComboBoxArray.Item = function(config) {
221 config.id = Roo.id();
222 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
225 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
228 defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [
230 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
236 onRender : function(ct, position){
237 Roo.form.Field.superclass.onRender.call(this, ct, position);
239 var cfg = this.getAutoCreate();
240 this.el = ct.createChild(cfg, position);
242 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
244 this.el.child('div').dom.innerHTML = this.cb.renderer ?
245 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
246 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
248 this.el.child('img').on('click', this.remove, this);
255 this.cb.items.remove(this);
256 this.el.child('img').un('click', this.remove, this);
258 this.cb.updateHiddenEl();