var tree= new Ext.tree.TreePanel({
id:'ptree',
region:'west',
layout:'anchor',
border:false,
rootVisible: false,
root:{},
listeners:{
render: function() {
authorityTree(tree); /*渲染树*/
},
checkchange: function(node, state) {
//选中父节点让相应的子节点选中
// node.cascade(function(node){
// node.attributes.checked = state;
// node.ui.checkbox.checked = state;
// return true;
// });
if (node.parentNode != null) {
//选中子节点让相应的父节点选中
var pNode = node.parentNode;
if (state || Fpanel.getChecked(id, pNode) == "") {
pNode.ui.toggleCheck(state);// 触发父节点被选中
pNode.attributes.checked = state;
}
}
}
}
});
/**
*初始化菜单树
*/
var authorityTree = function(tree) {
var re = new JsonRequest();
re.category = CATEGORY.READONLY;
re.params.param = {};
Ext.Ajax.request({
url:'***.json',
success: function(re, op) {
var obj = Ext.decode(re.responseText).ResponseObject;
var ns = {}, f = tree;
ns.children = obj;
var selectedNode = null, selectedId = f.selectedId;
function appendChild(node, o) {
if (node.id == selectedId) {
selectedNode = node;
}
if (o.children != null && o.children.length > 0) {
for (var a = 0; a < o.children.length; a++) {
var n = new Ext.tree.TreeNode({
id: o.children[a].id,
text: o.children[a].text,
children: o.children[a].children,
checked: false
});
node.appendChild(n);
appendChild(n, o.children[a]);
}
}
}
var r = new Ext.tree.TreeNode({
id: 'NULL',
text: '系统菜单'
});
appendChild(r, ns);
f.setRootNode(r);
if (selectedNode != null) {
f.selectPath(selectedNode.getPath());
}
tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
tree.root.expand(true);
},
failure: function() {
tree.render();
Ext.MessageBox.show({
title: '提示',
msg: '服务器忙,请稍后重试!',
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.ERROR
});
},
jsonData: re
})
}
/**
*初始化checkTree选中为false
*/
var treeCheckfalse = function(tree)
{
var nodes = tree.getChecked();
if(nodes && nodes.length){
for(var i=0;i<nodes.length;i++){
//设置UI状态为未选中状态
nodes[i].getUI().toggleCheck(false);
//设置节点属性为未选中状态
nodes[i].attributes.checked=false;
}
}
}