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,
158 reader : this.store.reader,
162 this.stores[i] = store;
166 var view = this.views[i] = new Roo.View(
172 selectedClass: this.selectedClass
175 view.getEl().setWidth(lw);
176 view.getEl().setStyle({
177 position: i < 1 ? 'relative' : 'absolute',
179 left: (i * lw ) + 'px',
180 display : i > 0 ? 'none' : 'block'
182 view.on('selectionchange', this.onSelectChange, this, {list : i });
183 view.on('dblclick', this.onDoubleClick, this, {list : i });
184 //view.on('click', this.onViewClick, this, { list : i });
186 store.on('beforeload', this.onBeforeLoad, this);
187 store.on('load', this.onLoad, this, { list : i});
188 store.on('loadexception', this.onLoadException, this);
190 // hide the other vies..
195 onResize : function() {},
197 restrictHeight : function()
200 Roo.each(this.innerLists, function(il,i) {
201 var el = this.views[i].getEl();
202 el.dom.style.height = '';
204 var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
205 // only adjust heights on other ones..
208 el.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
209 il.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
210 mh = Math.max(el.getHeight(), mh);
216 this.list.beginUpdate();
217 this.list.setHeight(mh+this.list.getFrameWidth('tb')+this.assetHeight);
218 this.list.alignTo(this.el, this.listAlign);
219 this.list.endUpdate();
224 // -- store handlers..
226 onBeforeLoad : function()
231 this.innerLists[0].update(this.loadingText ?
232 '<div class="loading-indicator">'+this.loadingText+'</div>' : '');
233 this.restrictHeight();
234 this.selectedIndex = -1;
237 onLoad : function(a,b,c,d)
244 if(this.store.getCount() > 0) {
246 this.restrictHeight();
248 this.onEmptyResults();
251 this.stores[1].loadData([]);
252 this.stores[2].loadData([]);
261 onLoadException : function()
264 Roo.log(this.store.reader.jsonData);
265 if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
266 Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
274 onSelectChange : function (view, sels, opts )
276 var ix = view.getSelectedIndexes();
281 this.setFromData(ix.length ? view.store.getAt(ix[0]).data : {});
286 this.setFromData({});
287 this.stores[opts.list+1].loadData( [] );
291 var rec = view.store.getAt(ix[0]);
292 this.setFromData(rec.data);
295 ((this.listWidth * 3 || Math.max(this.wrap.getWidth(), this.minListWidth)) - this.list.getFrameWidth('lr')) / 3
297 var data = typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn;
298 var dl = typeof(data.data.cn) ? data.data.total : data.length; ///json is a nested response..
299 this.stores[opts.list+1].loadData( data );
300 this.views[opts.list+1].getEl().setHeight( this.innerLists[0].getHeight());
301 this.views[opts.list+1].getEl().setStyle({ display : dl ? 'block' : 'none' });
302 this.innerLists[opts.list+1].setHeight( this.innerLists[0].getHeight());
303 this.list.setWidth(lw * (opts.list + (dl ? 2 : 1)));
305 onDoubleClick : function()
307 this.collapse(); //??
312 findRecord : function (prop,value)
314 return this.findRecordInStore(this.store, prop,value);
318 findRecordInStore : function(store, prop, value)
320 var cstore = new Roo.data.SimpleStore({
321 //fields : this.store.reader.meta.fields, // we need array reader.. for
322 reader : this.store.reader,
327 if(store.getCount() > 0){
328 store.each(function(r){
329 if(r.data[prop] == value){
333 if (r.data.cn && r.data.cn.length) {
334 cstore.loadData( r.data.cn);
335 var cret = _this.findRecordInStore(cstore, prop, value);
336 if (cret !== false) {