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;
36 if (!this.hiddenListName && this.hiddenName) {
37 this.hiddenListName = this.hiddenName + '-list';
43 Roo.extend(Roo.form.ComboBoxArray, Roo.form.ComboBox,
50 * @cfg {String} nameField The field to take the 'descriptive' display name from
54 * @cfg {String} idField The field to take the hidden 'id' data from
62 //hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
64 * @cfg {String} hiddenListName The field to take the array of hidden 'id' data that is selected
66 hiddenListName : false,
70 * @cfg {Number} boxWidth The width of the box that displays the selected element
72 boxWidth : 200, // use to set the box around the entry..
75 //validateValue : function() { return true; }, // all values are ok!
76 //onAddClick: function() { },
78 onRender : function(ct, position)
81 this.hiddenListName = this.hiddenName;
82 this.hiddenName += '-lastselected';
84 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
85 this.wrap.addClass('x-cbarray-grp');
86 var cbwrap = this.wrap.createChild(
87 {tag: 'div', cls: 'x-cbarray-cb'},
92 this.hiddenEl = this.wrap.createChild({
93 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
95 // this.el.dom.removeAttribute("name");
98 this.outerWrap = this.wrap;
100 this.outerWrap.setWidth(this.boxWidth);
101 this.outerWrap.dom.removeChild(this.el.dom);
102 this.wrap.dom.appendChild(this.el.dom);
103 this.outerWrap.dom.removeChild(this.trigger.dom);
104 this.wrap.dom.appendChild(this.trigger.dom);
106 this.trigger.setStyle('position','relative');
107 this.trigger.setStyle('left', '0px');
108 this.trigger.setStyle('top', '2px');
109 this.el.setStyle('vertical-align', 'text-bottom');
111 //this.trigger.setStyle('vertical-align', 'top');
115 this.adder = this.outerWrap.createChild(
116 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
118 this.adder.on('click', function(e) {
119 _t.fireEvent('adderclick', this, e);
123 //this.adder.on('click', this.onAddClick, _t);
128 onResize: function(w, h){
129 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
131 if(typeof w != 'number'){
132 // we do not handle it!?!?
135 var tw = this.trigger.getWidth();
136 tw += this.addicon ? this.addicon.getWidth() : 0;
137 tw += this.editicon ? this.editicon.getWidth() : 0;
139 this.el.setWidth( this.adjustWidth('input', x));
141 this.trigger.setStyle('left', '0px');
143 if(this.list && this.listWidth === undefined){
144 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
145 this.list.setWidth(lw);
146 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
153 addItem: function(rec)
155 if (this.items.indexOfKey(rec[this.idField]) > -1) {
156 //console.log("GOT " + rec.data.id);
160 var x = new Roo.form.ComboBoxArray.Item({
161 //id : rec[this.idField],
163 nameField : this.nameField,
164 tipField : this.tipField,
168 this.items.add(rec[this.idField],x);
169 // add it before the element..
170 this.updateHiddenEl();
171 x.render(this.outerWrap, this.wrap.dom);
172 // add the image handler..
175 updateHiddenEl : function()
178 if (!this.hiddenEl) {
182 var idField = this.idField;
183 this.items.each(function(f) {
184 ar.push(f.data[idField]);
187 this.hiddenEl.dom.value = ar.join(',');
193 Roo.form.ComboBoxArray.superclass.reset.call(this);
194 this.items.each(function(f) {
198 this.hiddenEl.dom.value = '';
204 return this.hiddenEl ? this.hiddenEl.dom.value : '';
206 setValue: function(v) // not a valid action - must use addItems..
208 if (typeof(v) != 'object') {
212 Roo.each(v, function(l) {
217 validateValue : function(value){
218 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
227 * @class Roo.form.ComboBoxArray.Item
228 * @extends Roo.BoxComponent
229 * A selected item in the list
230 * Fred [x] Brian [x] [Pick another |v]
234 * @param {Object} config Configuration options
237 Roo.form.ComboBoxArray.Item = function(config) {
238 config.id = Roo.id();
239 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
242 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
245 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
247 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
253 onRender : function(ct, position){
254 Roo.form.Field.superclass.onRender.call(this, ct, position);
256 var cfg = this.getAutoCreate();
257 this.el = ct.createChild(cfg, position);
259 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
261 this.el.child('div').dom.innerHTML = this.cb.renderer ?
262 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
263 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
265 this.el.child('img').on('click', this.remove, this);
272 this.cb.items.remove(this);
273 this.el.child('img').un('click', this.remove, this);
275 this.cb.updateHiddenEl();