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 list : null, // the outermost div..
52 innerLists : null, // the
56 onRender : function(ct, position)
58 Roo.form.ComboBox.superclass.onRender.call(this, ct, position); // skip parent call - got to above..
61 this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id: (this.hiddenId||this.hiddenName)},
63 this.hiddenField.value =
64 this.hiddenValue !== undefined ? this.hiddenValue :
65 this.value !== undefined ? this.value : '';
67 // prevent input submission
68 this.el.dom.removeAttribute('name');
74 this.el.dom.setAttribute('autocomplete', 'off');
77 var cls = 'x-combo-list';
79 this.list = new Roo.Layer({
80 shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
83 var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
84 this.list.setWidth(lw);
85 this.list.swallowEvent('mousewheel');
89 this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
90 this.assetHeight += this.header.getHeight();
95 for (var i =0 ; i < 3; i++) {
96 this.onRenderList( cls, i);
99 // always needs footer, as we are going to have an 'OK' button.
100 this.footer = this.list.createChild({cls:cls+'-ft'});
101 this.pageTb = new Roo.Toolbar(this.footer);
112 if ( this.allowBlank && !this.disableClear) {
114 this.pageTb.add(new Roo.Toolbar.Fill(), {
115 cls: 'x-btn-icon x-btn-clear',
121 _this.onSelect(false, -1);
126 this.assetHeight += this.footer.getHeight();
130 onRenderList : function ( cls, i)
134 ((this.listWidth * 3 || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / 3
137 this.list.setWidth(lw); // default to '1'
139 var il = this.innerLists[i] = this.list.createChild({cls:cls+'-inner'});
140 //il.on('mouseover', this.onViewOver, this, { list: i });
141 //il.on('mousemove', this.onViewMove, this, { list: i });
143 il.setStyle({ 'overflow-x' : 'hidden'});
146 this.tpl = new Roo.Template({
147 html : '<div class="'+cls+'-item '+cls+'-item-{cn:this.isEmpty}">{' + this.displayField + '}</div>',
148 isEmpty: function (value, allValues) {
149 return value && value.length ? 'has-children' : 'no-children'
154 var store = this.store;
156 store = new Roo.data.SimpleStore({
157 fields : this.store.reader.meta.fields,
161 this.stores[i] = store;
165 var view = this.views[i] = new Roo.View(
171 selectedClass: this.selectedClass
174 view.getEl().setWidth(lw);
175 view.getEl().setStyle({
176 position: i < 1 ? 'relative' : 'absolute',
178 left: (i * lw ) + 'px',
179 display : i > 0 ? 'none' : 'block'
181 view.on('selectionchange', this.onSelectChange, this, {list : i });
182 view.on('dblclick', this.onDoubleClick, this, {list : i });
183 //view.on('click', this.onViewClick, this, { list : i });
185 store.on('beforeload', this.onBeforeLoad, this);
186 store.on('load', this.onLoad, this, { list : i});
187 store.on('loadexception', this.onLoadException, this);
189 // hide the other vies..
194 onResize : function() {},
196 restrictHeight : function()
199 Roo.each(this.innerLists, function(il,i) {
200 var el = this.views[i].getEl();
201 el.dom.style.height = '';
203 var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
204 // only adjust heights on other ones..
207 el.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
208 il.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
209 mh = Math.max(el.getHeight(), mh);
215 this.list.beginUpdate();
216 this.list.setHeight(mh+this.list.getFrameWidth('tb')+this.assetHeight);
217 this.list.alignTo(this.el, this.listAlign);
218 this.list.endUpdate();
223 // -- store handlers..
225 onBeforeLoad : function()
230 this.innerLists[0].update(this.loadingText ?
231 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
232 this.restrictHeight();
233 this.selectedIndex = -1;
236 onLoad : function(a,b,c,d)
243 if(this.store.getCount() > 0) {
245 this.restrictHeight();
247 this.onEmptyResults();
250 this.stores[1].loadData([]);
251 this.stores[2].loadData([]);
260 onLoadException : function()
263 Roo.log(this.store.reader.jsonData);
264 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
265 Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
273 onSelectChange : function (view, sels, opts )
275 var ix = view.getSelectedIndexes();
280 this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
285 this.setFromData({});
286 this.stores[opts.list+1].loadData( [] );
290 var rec = view.store.getAt(ix[0]);
291 this.setFromData(rec.data);
294 ((this.listWidth * 3 || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / 3
296 var data = typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn;
297 this.stores[opts.list+1].loadData( data );
298 this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
299 this.views[opts.list+1].getEl().setStyle({ display : data .length ? 'block' : 'none' });
300 this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
301 this.list.setWidth(lw * (opts.list + (data.length ? 2 : 1)));
303 onDoubleClick : function()
305 this.collapse(); //??
310 findRecord : function (prop,value)
312 return this.findRecordInStore(this.store, prop,value);
316 findRecordInStore : function(store, prop, value)
318 var cstore = new Roo.data.SimpleStore({
319 fields : this.store.reader.meta.fields, // we need array reader.. for
324 if(store.getCount() > 0){
325 store.each(function(r){
326 if(r.data[prop] == value){
330 if (r.data.cn && r.data.cn.length) {
331 cstore.loadData( r.data.cn);
332 var cret = _this.findRecordInStore(cstore, prop, value);
333 if (cret !== false) {