3 * Copyright(c) 2008-2011 Alan Knowles
10 * @class Roo.form.ComboNested
11 * @extends Roo.form.ComboBox
12 * A combobox for that allows selection of nested items in a list,
27 * Create a new ComboNested
28 * @param {Object} config Configuration options
30 Roo.form.ComboNested = function(config){
31 Roo.form.ComboCheck.superclass.constructor.call(this, config);
32 // should verify some data...
34 // hiddenName = required..
35 // displayField = required
36 // valudField == required
37 var req= [ 'hiddenName', 'displayField', 'valueField' ];
39 Roo.each(req, function(e) {
40 if ((typeof(_t[e]) == 'undefined' ) || !_t[e].length) {
41 throw "Roo.form.ComboNested : missing value for: " + e;
48 Roo.extend(Roo.form.ComboNested, Roo.form.ComboBox, {
51 * @config {Number} max Number of columns to show
56 list : null, // the outermost div..
57 innerLists : null, // the
61 onRender : function(ct, position)
63 Roo.form.ComboBox.superclass.onRender.call(this, ct, position); // skip parent call - got to above..
66 this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id: (this.hiddenId||this.hiddenName)},
68 this.hiddenField.value =
69 this.hiddenValue !== undefined ? this.hiddenValue :
70 this.value !== undefined ? this.value : '';
72 // prevent input submission
73 this.el.dom.removeAttribute('name');
79 this.el.dom.setAttribute('autocomplete', 'off');
82 var cls = 'x-combo-list';
84 this.list = new Roo.Layer({
85 shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
88 var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
89 this.list.setWidth(lw);
90 this.list.swallowEvent('mousewheel');
94 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
95 this.assetHeight += this.header.getHeight();
100 for (var i =0 ; i < this.maxColumns; i++) {
101 this.onRenderList( cls, i);
104 // always needs footer, as we are going to have an 'OK' button.
105 this.footer = this.list.createChild({cls:cls+'-ft'});
106 this.pageTb = new Roo.Toolbar(this.footer);
117 if ( this.allowBlank && !this.disableClear) {
119 this.pageTb.add(new Roo.Toolbar.Fill(), {
120 cls: 'x-btn-icon x-btn-clear',
126 _this.onSelect(false, -1);
131 this.assetHeight += this.footer.getHeight();
135 onRenderList : function ( cls, i)
139 ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
142 this.list.setWidth(lw); // default to '1'
144 var il = this.innerLists[i] = this.list.createChild({cls:cls+'-inner'});
145 //il.on('mouseover', this.onViewOver, this, { list: i });
146 //il.on('mousemove', this.onViewMove, this, { list: i });
148 il.setStyle({ 'overflow-x' : 'hidden'});
151 this.tpl = new Roo.Template({
152 html : '<div class="'+cls+'-item '+cls+'-item-{cn:this.isEmpty}">{' + this.displayField + '}</div>',
153 isEmpty: function (value, allValues) {
155 var dl = typeof(value.data) != 'undefined' ? value.data.length : value.length; ///json is a nested response..
156 return dl ? 'has-children' : 'no-children'
161 var store = this.store;
163 store = new Roo.data.SimpleStore({
164 //fields : this.store.reader.meta.fields,
165 reader : this.store.reader,
169 this.stores[i] = store;
171 var view = this.views[i] = new Roo.View(
177 selectedClass: this.selectedClass
180 view.getEl().setWidth(lw);
181 view.getEl().setStyle({
182 position: i < 1 ? 'relative' : 'absolute',
184 left: (i * lw ) + 'px',
185 display : i > 0 ? 'none' : 'block'
187 view.on('selectionchange', this.onSelectChange, this, {list : i });
188 view.on('dblclick', this.onDoubleClick, this, {list : i });
189 //view.on('click', this.onViewClick, this, { list : i });
191 store.on('beforeload', this.onBeforeLoad, this);
192 store.on('load', this.onLoad, this, { list : i});
193 store.on('loadexception', this.onLoadException, this);
195 // hide the other vies..
200 onResize : function() {},
202 restrictHeight : function()
205 Roo.each(this.innerLists, function(il,i) {
206 var el = this.views[i].getEl();
207 el.dom.style.height = '';
209 var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
210 // only adjust heights on other ones..
213 el.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
214 il.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
215 mh = Math.max(el.getHeight(), mh);
221 this.list.beginUpdate();
222 this.list.setHeight(mh+this.list.getFrameWidth('tb')+this.assetHeight);
223 this.list.alignTo(this.el, this.listAlign);
224 this.list.endUpdate();
229 // -- store handlers..
231 onBeforeLoad : function()
236 this.innerLists[0].update(this.loadingText ?
237 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
238 this.restrictHeight();
239 this.selectedIndex = -1;
242 onLoad : function(a,b,c,d)
249 if(this.store.getCount() > 0) {
251 this.restrictHeight();
253 this.onEmptyResults();
256 this.stores[1].loadData([]);
257 this.stores[2].loadData([]);
266 onLoadException : function()
269 Roo.log(this.store.reader.jsonData);
270 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
271 Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
279 onSelectChange : function (view, sels, opts )
281 var ix = view.getSelectedIndexes();
283 if (opts.list > this.maxColumns - 2) {
284 this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
289 this.setFromData({});
290 var str = this.stores[opts.list+1];
295 var rec = view.store.getAt(ix[0]);
297 this.setFromData(rec.data);
302 ((this.listWidth * this.maxColumns || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / this.maxColumns
305 this.stores[opts.list+1].loadDataFromChildren( rec );
306 var dl = this.stores[opts.list+1]. getTotalCount();
307 this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
308 this.views[opts.list+1].getEl().setStyle({ display : dl ? 'block' : 'none' });
309 this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
310 this.list.setWidth(lw * (opts.list + (dl ? 2 : 1)));
312 if (this.isLoading) {
313 // this.selectActive(opts.list);
317 onDoubleClick : function()
319 this.collapse(); //??
327 recordToStores : function(store, prop, value, stack)
329 var cstore = new Roo.data.SimpleStore({
330 //fields : this.store.reader.meta.fields, // we need array reader.. for
331 reader : this.store.reader,
337 if(store.getCount() < 1){
340 store.each(function(r){
341 if(r.data[prop] == value){
346 if (r.data.cn && r.data.cn.length) {
347 cstore.loadDataFromChildren( r);
348 var cret = _this.recordToStores(cstore, prop, value, stack);
349 if (cret !== false) {
358 if (record == false) {
366 * find the stack of stores that match our value.
371 selectActive : function ()
373 // if store is not loaded, then we will need to wait for that to happen first.
375 this.findRecordInStore(this.store, this.hiddenName, this.getValue(), stack);