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 //hiddenName : false, // set this if you want a , sperated list of values in it for form posting..
63 * @cfg {String} hiddenListName The field to take the array of hidden 'id' data that is selected
65 hiddenListName : false,
69 * @cfg {Number} boxWidth The width of the box that displays the selected element
71 boxWidth : 200, // use to set the box around the entry..
74 //validateValue : function() { return true; }, // all values are ok!
75 //onAddClick: function() { },
77 onRender : function(ct, position)
80 Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
81 this.wrap.addClass('x-cbarray-grp');
82 var cbwrap = this.wrap.createChild(
83 {tag: 'div', cls: 'x-cbarray-cb'},
87 if (this.hiddenListName) {
89 this.hiddenEl = this.wrap.createChild({
90 tag: 'input', type:'hidden' , name: this.hiddenListName, value : ''
92 // this.el.dom.removeAttribute("name");
95 this.outerWrap = this.wrap;
97 this.outerWrap.setWidth(this.boxWidth);
98 this.outerWrap.dom.removeChild(this.el.dom);
99 this.wrap.dom.appendChild(this.el.dom);
100 this.outerWrap.dom.removeChild(this.trigger.dom);
101 this.wrap.dom.appendChild(this.trigger.dom);
103 this.trigger.setStyle('position','relative');
104 this.trigger.setStyle('left', '0px');
105 this.trigger.setStyle('top', '2px');
106 this.el.setStyle('vertical-align', 'text-bottom');
108 //this.trigger.setStyle('vertical-align', 'top');
112 this.adder = this.outerWrap.createChild(
113 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
115 this.adder.on('click', function(e) {
116 _t.fireEvent('adderclick', this, e);
120 //this.adder.on('click', this.onAddClick, _t);
125 onResize: function(w, h){
126 Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
128 if(typeof w != 'number'){
129 // we do not handle it!?!?
132 var tw = this.trigger.getWidth();
133 tw += this.addicon ? this.addicon.getWidth() : 0;
134 tw += this.editicon ? this.editicon.getWidth() : 0;
136 this.el.setWidth( this.adjustWidth('input', x));
138 this.trigger.setStyle('left', '0px');
140 if(this.list && this.listWidth === undefined){
141 var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
142 this.list.setWidth(lw);
143 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
150 addItem: function(rec)
152 if (this.items.indexOfKey(rec[this.idField]) > -1) {
153 //console.log("GOT " + rec.data.id);
157 var x = new Roo.form.ComboBoxArray.Item({
158 //id : rec[this.idField],
160 nameField : this.nameField,
161 tipField : this.tipField,
165 this.items.add(rec[this.idField],x);
166 // add it before the element..
167 this.updateHiddenEl();
168 x.render(this.outerWrap, this.wrap.dom);
169 // add the image handler..
172 updateHiddenEl : function()
175 if (!this.hiddenEl) {
179 var idField = this.idField;
180 this.items.each(function(f) {
181 ar.push(f.data[idField]);
184 this.hiddenEl.dom.value = ar.join(',');
190 Roo.form.ComboBoxArray.superclass.reset.call(this);
191 this.items.each(function(f) {
195 this.hiddenEl.dom.value = '';
201 return this.hiddenEl ? this.hiddenEl.dom.value : '';
203 setValue: function(v) // not a valid action - must use addItems..
205 if (typeof(v) != 'object') {
209 Roo.each(v, function(l) {
214 validateValue : function(value){
215 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
224 * @class Roo.form.ComboBoxArray.Item
225 * @extends Roo.BoxComponent
226 * A selected item in the list
227 * Fred [x] Brian [x] [Pick another |v]
231 * @param {Object} config Configuration options
234 Roo.form.ComboBoxArray.Item = function(config) {
235 config.id = Roo.id();
236 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
239 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
242 defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [
244 { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
250 onRender : function(ct, position){
251 Roo.form.Field.superclass.onRender.call(this, ct, position);
253 var cfg = this.getAutoCreate();
254 this.el = ct.createChild(cfg, position);
256 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
258 this.el.child('div').dom.innerHTML = this.cb.renderer ?
259 this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
260 this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
262 this.el.child('img').on('click', this.remove, this);
269 this.cb.items.remove(this);
270 this.el.child('img').un('click', this.remove, this);
272 this.cb.updateHiddenEl();