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,
45 * @cfg {String} nameField The field to take the 'descriptive' display name from
49 * @cfg {String} idField The field to take the hidden 'id' data from
57 hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
58 hiddenListName : false,
62 * @cfg {Number} boxWidth The width of the box that displays the selected element
64 boxWidth : 200, // use to set the box around the entry..
67 //validateValue : function() { return true; }, // all values are ok!
68 //onAddClick: function() { },
70 onRender : function(ct, position)
73 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
74 this.wrap.addClass('p-cblist-grp');
75 var cbwrap = this.wrap.createChild(
76 {tag: 'div', cls: 'p-cblist-cb'},
79 if (this.hiddenListName) {
81 this.hiddenEl = this.wrap.createChild({
82 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
84 // this.el.dom.removeAttribute("name");
87 this.outerWrap = this.wrap;
89 this.outerWrap.setWidth(this.boxWidth);
90 this.outerWrap.dom.removeChild(this.el.dom);
91 this.wrap.dom.appendChild(this.el.dom);
92 this.outerWrap.dom.removeChild(this.trigger.dom);
93 this.wrap.dom.appendChild(this.trigger.dom);
95 this.trigger.setStyle('position','relative');
96 this.trigger.setStyle('left', '0px');
97 this.trigger.setStyle('top', '2px');
98 this.el.setStyle('vertical-align', 'text-bottom');
100 //this.trigger.setStyle('vertical-align', 'top');
104 this.adder = this.outerWrap.createChild(
105 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
107 this.adder.on('click', function(e) {
108 _t.fireEvent('adderclick', this, e);
112 //this.adder.on('click', this.onAddClick, _t);
117 onResize: function(w, h){
118 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
120 if(typeof w != 'number'){
121 // we do not handle it!?!?
124 var tw = this.trigger.getWidth();
125 tw += this.addicon ? this.addicon.getWidth() : 0;
126 tw += this.editicon ? this.editicon.getWidth() : 0;
128 this.el.setWidth( this.adjustWidth('input', x));
130 this.trigger.setStyle('left', '0px');
132 if(this.list && this.listWidth === undefined){
133 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
134 this.list.setWidth(lw);
135 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
142 addItem: function(rec)
144 if (this.items.indexOfKey(rec[this.idField]) > -1) {
145 //console.log("GOT " + rec.data.id);
149 var x = new Roo.form.ComboBoxArray.Item({
150 //id : rec[this.idField],
152 nameField : this.nameField,
153 tipField : this.tipField,
157 this.items.add(rec[this.idField],x);
158 // add it before the element..
159 this.updateHiddenEl();
160 x.render(this.outerWrap, this.wrap.dom);
161 // add the image handler..
164 updateHiddenEl : function()
167 if (!this.hiddenEl) {
171 var idField = this.idField;
172 this.items.each(function(f) {
173 ar.push(f.data[idField]);
176 this.hiddenEl.dom.value = ar.join(',');
182 Roo.form.ComboBoxArray.superclass.reset.call(this);
183 this.items.each(function(f) {
187 this.hiddenEl.dom.value = '';
193 return this.hiddenEl ? this.hiddenEl.dom.value : '';
195 setValue: function(v) // not a valid action - must use addItems..
197 if (typeof(v) != 'object') {
201 Roo.each(v, function(l) {
206 validateValue : function(value){
207 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
216 * @class Roo.form.ComboBoxArray.Item
217 * @extends Roo.BoxComponent
218 * A selected item in the list
219 * Fred [x] Brian [x] [Pick another |v]
223 * @param {Object} config Configuration options
226 Roo.form.ComboBoxArray.Item = function(config) {
227 config.id = Roo.id();
228 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
231 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
234 defaultAutoCreate : {tag: 'div', cls: 'p-cblist-item', cn : [
236 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
242 onRender : function(ct, position){
243 Roo.form.Field.superclass.onRender.call(this, ct, position);
245 var cfg = this.getAutoCreate();
246 this.el = ct.createChild(cfg, position);
248 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
250 this.el.child('div').dom.innerHTML = this.cb.renderer ?
251 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
252 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
254 this.el.child('img').on('click', this.remove, this);
261 this.cb.items.remove(this);
262 this.el.child('img').un('click', this.remove, this);
264 this.cb.updateHiddenEl();