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
24 * field : [Selected x] [.....V]
29 Roo.form.ComboBoxArray = function(config){
31 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
32 this.items = new Roo.util.MixedCollection(false);
33 this.on('select', function(cb, rec, ix) {
34 this.addItem(rec.data);
36 this.el.dom.value = '';
37 //cb.lastData = rec.data;
44 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,
54 boxWidth : 200, // use to set the box around the entry..
57 //validateValue : function() { return true; }, // all values are ok!
58 //onAddClick: function() { },
60 onRender : function(ct, position)
63 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
64 this.wrap.addClass('p-cblist-grp');
65 var cbwrap = this.wrap.createChild(
66 {tag: 'div', cls: 'p-cblist-cb'},
69 if (this.hiddenListName) {
71 this.hiddenEl = this.wrap.createChild({
72 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
74 // this.el.dom.removeAttribute("name");
77 this.outerWrap = this.wrap;
79 this.outerWrap.setWidth(this.boxWidth);
80 this.outerWrap.dom.removeChild(this.el.dom);
81 this.wrap.dom.appendChild(this.el.dom);
82 this.outerWrap.dom.removeChild(this.trigger.dom);
83 this.wrap.dom.appendChild(this.trigger.dom);
85 this.trigger.setStyle('position','relative');
86 this.trigger.setStyle('left', '0px');
87 this.trigger.setStyle('top', '2px');
88 this.el.setStyle('vertical-align', 'text-bottom');
90 //this.trigger.setStyle('vertical-align', 'top');
94 this.adder = this.outerWrap.createChild(
95 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
97 this.adder.on('click', function(e) {
98 _t.fireEvent('adderclick', this, e);
102 //this.adder.on('click', this.onAddClick, _t);
107 onResize: function(w, h){
108 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
110 if(typeof w != 'number'){
111 // we do not handle it!?!?
114 var tw = this.trigger.getWidth();
115 tw += this.addicon ? this.addicon.getWidth() : 0;
116 tw += this.editicon ? this.editicon.getWidth() : 0;
118 this.el.setWidth( this.adjustWidth('input', x));
120 this.trigger.setStyle('left', '0px');
122 if(this.list && this.listWidth === undefined){
123 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
124 this.list.setWidth(lw);
125 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
132 addItem: function(rec)
134 if (this.items.indexOfKey(rec[this.idField]) > -1) {
135 //console.log("GOT " + rec.data.id);
139 var x = new Roo.form.ComboBoxArray.Item({
140 //id : rec[this.idField],
142 nameField : this.nameField,
143 tipField : this.tipField,
147 this.items.add(rec[this.idField],x);
148 // add it before the element..
149 this.updateHiddenEl();
150 x.render(this.outerWrap, this.wrap.dom);
151 // add the image handler..
154 updateHiddenEl : function()
157 if (!this.hiddenEl) {
161 var idField = this.idField;
162 this.items.each(function(f) {
163 ar.push(f.data[idField]);
166 this.hiddenEl.dom.value = ar.join(',');
172 Roo.form.ComboBoxArray.superclass.reset.call(this);
173 this.items.each(function(f) {
177 this.hiddenEl.dom.value = '';
183 return this.hiddenEl ? this.hiddenEl.dom.value : '';
185 setValue: function(v) // not a valid action - must use addItems..
187 if (typeof(v) != 'object') {
191 Roo.each(v, function(l) {
196 validateValue : function(value){
197 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
203 Roo.form.ComboBoxArray.Item = function(config) {
204 config.id = Roo.id();
205 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
208 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
211 defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [
213 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
219 onRender : function(ct, position){
220 Roo.form.Field.superclass.onRender.call(this, ct, position);
222 var cfg = this.getAutoCreate();
223 this.el = ct.createChild(cfg, position);
225 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
227 this.el.child('div').dom.innerHTML = this.cb.renderer ?
228 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
229 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
231 this.el.child('img').on('click', this.remove, this);
238 this.cb.items.remove(this);
239 this.el.child('img').un('click', this.remove, this);
241 this.cb.updateHiddenEl();