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;
35 if (!this.hiddenListName && this.hiddenName) {
36 this.hiddenListName = this.hiddenName + '-list';
42 Roo.extend(Roo.form.ComboBoxArray, Roo.form.ComboBox,
49 * @cfg {String} nameField The field to take the 'descriptive' display name from
53 * @cfg {String} idField The field to take the hidden 'id' data from
61 * @cfg {String} idField The field to take the hidden 'id' data from
63 hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
64 hiddenListName : false,
68 * @cfg {Number} boxWidth The width of the box that displays the selected element
70 boxWidth : 200, // use to set the box around the entry..
73 //validateValue : function() { return true; }, // all values are ok!
74 //onAddClick: function() { },
76 onRender : function(ct, position)
79 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
80 this.wrap.addClass('x-cbarray-grp');
81 var cbwrap = this.wrap.createChild(
82 {tag: 'div', cls: 'x-cbarray-cb'},
86 if (this.hiddenListName) {
88 this.hiddenEl = this.wrap.createChild({
89 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
91 // this.el.dom.removeAttribute("name");
94 this.outerWrap = this.wrap;
96 this.outerWrap.setWidth(this.boxWidth);
97 this.outerWrap.dom.removeChild(this.el.dom);
98 this.wrap.dom.appendChild(this.el.dom);
99 this.outerWrap.dom.removeChild(this.trigger.dom);
100 this.wrap.dom.appendChild(this.trigger.dom);
102 this.trigger.setStyle('position','relative');
103 this.trigger.setStyle('left', '0px');
104 this.trigger.setStyle('top', '2px');
105 this.el.setStyle('vertical-align', 'text-bottom');
107 //this.trigger.setStyle('vertical-align', 'top');
111 this.adder = this.outerWrap.createChild(
112 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
114 this.adder.on('click', function(e) {
115 _t.fireEvent('adderclick', this, e);
119 //this.adder.on('click', this.onAddClick, _t);
124 onResize: function(w, h){
125 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
127 if(typeof w != 'number'){
128 // we do not handle it!?!?
131 var tw = this.trigger.getWidth();
132 tw += this.addicon ? this.addicon.getWidth() : 0;
133 tw += this.editicon ? this.editicon.getWidth() : 0;
135 this.el.setWidth( this.adjustWidth('input', x));
137 this.trigger.setStyle('left', '0px');
139 if(this.list && this.listWidth === undefined){
140 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
141 this.list.setWidth(lw);
142 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
149 addItem: function(rec)
151 if (this.items.indexOfKey(rec[this.idField]) > -1) {
152 //console.log("GOT " + rec.data.id);
156 var x = new Roo.form.ComboBoxArray.Item({
157 //id : rec[this.idField],
159 nameField : this.nameField,
160 tipField : this.tipField,
164 this.items.add(rec[this.idField],x);
165 // add it before the element..
166 this.updateHiddenEl();
167 x.render(this.outerWrap, this.wrap.dom);
168 // add the image handler..
171 updateHiddenEl : function()
174 if (!this.hiddenEl) {
178 var idField = this.idField;
179 this.items.each(function(f) {
180 ar.push(f.data[idField]);
183 this.hiddenEl.dom.value = ar.join(',');
189 Roo.form.ComboBoxArray.superclass.reset.call(this);
190 this.items.each(function(f) {
194 this.hiddenEl.dom.value = '';
200 return this.hiddenEl ? this.hiddenEl.dom.value : '';
202 setValue: function(v) // not a valid action - must use addItems..
204 if (typeof(v) != 'object') {
208 Roo.each(v, function(l) {
213 validateValue : function(value){
214 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
223 * @class Roo.form.ComboBoxArray.Item
224 * @extends Roo.BoxComponent
225 * A selected item in the list
226 * Fred [x] Brian [x] [Pick another |v]
230 * @param {Object} config Configuration options
233 Roo.form.ComboBoxArray.Item = function(config) {
234 config.id = Roo.id();
235 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
238 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
241 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
243 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
249 onRender : function(ct, position){
250 Roo.form.Field.superclass.onRender.call(this, ct, position);
252 var cfg = this.getAutoCreate();
253 this.el = ct.createChild(cfg, position);
255 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
257 this.el.child('div').dom.innerHTML = this.cb.renderer ?
258 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
259 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
261 this.el.child('img').on('click', this.remove, this);
268 this.cb.items.remove(this);
269 this.el.child('img').un('click', this.remove, this);
271 this.cb.updateHiddenEl();