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 j=child.length-1;j>=0;j--)
{
if(child[j].checked)
{isTure=child[j].checked;
continue;
}
else{
isTure=false;
break;
}
}
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);//不能選了
}
}
}
}
} node
//目錄中點擊方法
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的點擊狀態函數
} async
}; 函數
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting);
setCheck();
}); url
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);
} code
這個版本修復了上次的直接點最後一個會致使父節點選中的問題,最後一個bug,雙擊子節點目錄,會致使原本已禁用的子節點又能選了,想到的方法是禁用雙擊,不過沒有找到怎麼實現。。。。。有知道的嗎,給留個言 對象