combotree.js 1.9 KB
Ext.ux.ComboTree = function(options){
	var tree_div = "tree_"+(new Date()).getTime();
	var cmbthis=this;
	
	if(!options.maxHeight){
		options.maxHeight = 200;
	};
	this.setTreeval=function(val){
        this.treeval = val;
    };
    
    var wstr = '';
    if(options.width) {
        wstr = ';width:' + (options.width-5) + ';overflow:auto';
    }

	// 以下固定
	options.editable=false;
	options.mode='local';
	options.triggerAction='all';
	options.store=[[]];
	options.tpl='<tpl for="."><div style="height:'+options.maxHeight+'px'+wstr+'"><div id="'+tree_div+'"></div></div></tpl>';
	options.selectedClass='';
	options.onSelect=Ext.emptyFn;
	options.listeners={
    	'expand':function(){
    		 this.tree.render(tree_div);
    		if (!this.tree.rendered) {
    			this.tree.render(tree_div);
    		}
    		this.el.focus();
    		Ext.ux.ComboTree.superclass.expand.apply(this, arguments);
      	}
	};
	options.tree=new Ext.tree.TreePanel({
		loader: options.loader,
		border:false,
 		root: options.root,
 		rootVisible: options.rootVisible,
 		useArrows: options.useArrows,
 		lines: options.lines,
 		listeners:{
	 		'click':function(node){
 		        cmbthis.setValue(node.text);
 		        cmbthis.setTreeval(node.id);
 		        cmbthis.collapse();
 		        
	 		}
	 	}
  	});
  	
  	if(options.treeparams) {
  		options.tree.on('beforeload', function(node){
  	    	node.getOwnerTree().loader.baseParams = options.treeparams;
  	    });
  	}
  	
    Ext.ux.ComboTree.superclass.constructor.call(this, options);
};
Ext.extend(Ext.ux.ComboTree, Ext.form.ComboBox, {
    getValue:function(){
    	if(Ext.isDefined(this.treeval)) return this.treeval;
        /*var node = this.tree.getSelectionModel().getSelectedNode();
        if(node==null) {
            return '';
        }else{
            return node.id;
        }*/
    }
});
Ext.reg('treecombo',Ext.ux.ComboTree);