复选框tree:选中父节点时自动选中子节点;选中子节点时自动选择父节点!
var tree = new Ext.tree.TreePanel({
id:'tree',
title: '系统菜单',
height: 300,
width: 600,
useArrows:true,
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true,
rootVisible: false,
frame: true,
root: {
nodeType: 'async'
},
loader : new Ext.tree.TreeLoader( {
dataUrl: '../../jsp/system/getTree1.jsp',
listeners:{
"beforeload":function(treeloader,node){
treeloader.baseParams={
ven_code:Ext.getCmp('ven_code_id').getValue()
};
}
}
})
});
//选中父节点时自动选中子节点
tree.on('checkchange', function(node, checked) {
var parentNode = node.parentNode;
if(parentNode !== null) {
parentCheck(parentNode,checked);
}
node.expand();
node.attributes.checked = checked;
node.eachChild(function(child) {
child.ui.toggleCheck(checked);
child.attributes.checked = checked;
child.fireEvent('checkchange', child, checked);
});
}, tree);
//判断是否有子节点被选中
var childHasChecked = function(node){
var childNodes = node.childNodes;
if(childNodes || childNodes.length>0){
for(var i=0;i<childNodes.length;i++){
if(childNodes[i].getUI().checkbox.checked)
return true;
}
}
return false;
};
//级联选中父节点
var parentCheck = function(node ,checked){
var checkbox = node.getUI().checkbox;
if(typeof checkbox == 'undefined')
return false;
if(!(checked ^ checkbox.checked))
return false;
if(!checked && childHasChecked(node))
return false;
checkbox.checked = checked;
node.attributes.checked = checked;
node.getUI().checkbox.indeterminate = checked; //半选中状态
node.getOwnerTree().fireEvent('check', node, checked);
var parentNode = node.parentNode;
if( parentNode !== null){
parentCheck(parentNode,checked);
}
};