漂亮的树Tree控件代码

Ext.TreeComboField=Ext.extend(Ext.form.TriggerField,{      
valueField:"id",
displayField:"name",
haveShow:false,
editable:false,
onTriggerClick : function(){
if(!this.tree.rendered)
{
this.treeId = Ext.id();
panel.id = this.treeId;
this.getEl().dom.parentNode.appendChild(panel);
this.tree.render(this.treeId);
this.tree.setWidth(this.width);
this.tree.getEl().alignTo(this.getEl(), "tl-bl");
}
this.tree.show();
},
initComponent : function(){
Ext.TreeComboField.superclass.initComponent.call(this);

},
onRender : function(ct, position){
Ext.TreeComboField.superclass.onRender.call(this, ct, position);
this.tree.on("click",this.choice,this);
if(this.hiddenName){
this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id: (this.hiddenId||this.hiddenName)},
'before', true);
this.hiddenField.value =
this.hiddenValue !== undefined ? this.hiddenValue :this.value !== undefined ? this.value : '';
this.el.dom.removeAttribute('name');
}
if(!this.editable){
this.editable = true;
this.setEditable(false);
}
},
getValue : function(){
return typeof this.value != 'undefined' ? this.value : '';
},
clearValue : function(){
if(this.hiddenField){
this.hiddenField.value = '';
}
this.setRawValue('');
this.lastSelectionText = '';
this.applyEmptyText();
},
readPropertyValue:function(obj,p)
{
var v=null;
for(var o in obj)
{
if(o==p)v=obj[o];
}
return v;
},
setValue : function(obj){
if(!obj){
this.clearValue();
return;
}
var v=obj;
var text = v;
var value=this.valueField||this.displayField;
if(typeof v=="object" && this.readPropertyValue(obj,value)){
text=obj[this.displayField||this.valueField];
v=obj[value];
}
var node = this.tree.getNodeById(v);
if(node){
text = node.text;
}else if(this.valueNotFoundText !== undefined){
text = this.valueNotFoundText;
}
this.lastSelectionText = text;
if(this.hiddenField){
this.hiddenField.value = v;
}
Ext.TreeComboField.superclass.setValue.call(this, text);
this.value = v;
},
setEditable : function(value){
if(value == this.editable){
return;
}
this.editable = value;
if(!value){
this.el.dom.setAttribute('readOnly', true);
this.el.on('mousedown', this.onTriggerClick, this);
this.el.addClass('x-combo-noedit');
}else{
this.el.dom.setAttribute('readOnly', false);
this.el.un('mousedown', this.onTriggerClick, this);
this.el.removeClass('x-combo-noedit');
}
},
choice:function(node,eventObject)
{
if(node.id!="root") this.setValue(node.id);
else this.clearValue();
this.tree.hide();
},
onDestroy : function(){
if(this.tree.rendered){
this.tree.getEl().remove();
}
Ext.TreeComboField.superclass.onDestroy.call(this);
}
});
Ext.reg('treecombo', Ext.TreeComboField);

你可能感兴趣的:(ExtJS学习)