var setting = {
check: {enable: true,nocheckInherit: true},
async: {enable: true,url:"",autoParam:["tcode"],dataFilter: filter},
callback: {onCheck:zTreeOnCheck,onClick:zTreeOnClick,onExpand: onExpand}
};
function onExpand(event, treeId, treeNode)
{
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
if(treeNode.checked)
{
for(var j=0;j<treeNode.children.length;j++)
zTree.setChkDisabled(treeNode.children[j], true);
}
}
function setCheck() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.setting.check.chkboxType = { "Y" : "", "N" : "p" };
}
function filter(treeId, parentNode, childNodes) {
return childNodes;
}
//用zTree方法勾选checkbox
function zTreeOnCheck(e, treeId, treeNode){
var isTure=false;//标志位,用来判断父节点下的子节点是否都选中了,是为true,没有为false
var checked=treeNode.checked;//获取节点的选中状态
var zTree = $.fn.zTree.getZTreeObj("treeDemo");//获取ztree对象
if(!checked&&treeNode.children!=null)//父节点取消选择
{
for(var j=0;treeNode.isParent&&j<treeNode.children.length;j++)
zTree.setChkDisabled(treeNode.children[j], false); //设置子节点可选
}
else{//父节点选择了
for(var j=0;treeNode.isParent&&treeNode.children!=null&&j<treeNode.children.length;j++)
{
zTree.checkNode(treeNode.children[j],false,false);//取消所有的子节点
zTree.setChkDisabled(treeNode.children[j], true);//设置子节点不可选
}
}
var nodes=treeNode;//把节点付给nodes
if(nodes.isParent&&checked&&treeNode.open&&nodes.length!=null)//父节点展开了,并且有子节点,选中了
{
for(var j=0;j<nodes.children.length;j++)
zTree.setChkDisabled(nodes.children[j], true);
}
var node="";
if(nodes.getParentNode()!=null)
node=zTree.getNodeByParam("id", nodes.getParentNode().id);//获取当前子节点的父节点,若有
var child=node.children;//获取父节点下的子节点
if(child!=undefined)
{
for(var i=0;i<child.length;i++)
{
if(child[i].checked)//判断父节点下子节点的选中状态
{isTure=true;}
else{isTure=false;}
}
if(isTure==true)
{//子节点都选择了
zTree.checkNode(node, true, false);//选中父节点为勾选状态
for(var i=0;i<child.length;i++)
{
zTree.checkNode(child[i],false,false);//取消所有的子节点
zTree.setChkDisabled(child[i], true);//不能选了
}
}
}
}
/*function getAllChildrenNodes(treeNode,result){
alert(treeNode.level);
if (treeNode.isParent) {
var childrenNodes = treeNode.children;
if (childrenNodes) {
for (var i = 0; i < childrenNodes.length; i++) {
result += childrenNodes[i].tcode+",";
}
}
}
return result;
} */
//目录中点击方法
function zTreeOnClick(event, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
if(treeNode.getChkDisabled)
{if(treeNode.checked)//更改当前checkbox的勾选状态
{
zTree.checkNode(treeNode,false,false);
}
else{
zTree.checkNode(treeNode,true,false);
}
zTreeOnCheck(event, treeId, treeNode);//调用checkbox的点击状态函数
}
/*else{
if(treeNode.checked)//更改当前checkbox的勾选状态
{
zTree.checkNode(treeNode,false,false);
}
else{
zTree.checkNode(treeNode,true,false);
}
zTreeOnCheck(event, treeId, treeNode);//调用checkbox的点击状态函数
}*/
};
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting);
setCheck();
});
function c()
{
var delidID="", delidName="";var node="";
var zTree = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);//获取所有选中状态的节点
for (var i = 0; i < zTree.length; i++) {
delidID += zTree[i].id + ",";
delidName += zTree[i].name + ",";
}
alert(delidID+delidName);
}
json格式为id:'1',name:'工具类',tcode:'01',isParent:true
这个的作用就是如果选择了父节点下的所有自己点,那么子节点自动取消选中状态,父节点选中,同时子节点不可选。有个小bug就是如果上来就选父节点下的最后一个节点,就会出现上述现象,目前没有发现问题所在,我在看看,解决了在发个新的,最近做项目,小弟刚看ztree两天,代码写的很不规范,还请大虾们多多指教