<script>
var setting = {
view: {
selectedMulti: false
},
data: {
simpleData: {
enable: true
}
},
check: {
enable: true,
autoCheckTrigger: true
},
callback: {
onCheck: checkedNode
},
edit: {
enable: false
}
};
var objTree;
var checkedUserData; //已经在角色里面的旧用户信息
function initTree(zNode, userData) {
//清空选中人员框的数据
clearCheckName();
var dataNode = eval(zNode);
checkedUserData = JSON.parse(userData);
$.fn.zTree.init($("#treeDemo"), setting, dataNode);
objTree = $.fn.zTree.getZTreeObj("treeDemo");
objTree.onCheck = checkedNode;
var nodes = {};
nodes = objTree.getNodes();
//console.log(nodes[0].children.length);
changeNode(nodes[0]);
$('#modalTree').modal();
};
function checkedNode() {
var nodes = objTree.getCheckedNodes(true);
if (nodes.length < 1)
return;
var strName = "";
for (var i = 0; i < nodes.length; i++) {
var t = nodes[i];
if (nodes[i].type == "user") {
strName = strName + nodes[i].id + ";";
}
}
$("#checkedName").text(strName);
// console.log(strName);
// alert("ddd");
};
function getCheckName() {
return $("#checkedName").text();
};
function clearCheckName() {
return $("#checkedName").text("");
};
function changeNode(root) {
// console.log(root.getNodes().length);
// if(root.getNodes().length>0){
var ns = root.children;
for (var i = 0; i < ns.length; i++) {
if (ns[i].isParent && ns[i].children.length > 0) {
changeNode(ns[i]);
}
var temp = ns[i];
if (temp.type == "user") {
temp.iconSkin = "user";
objTree.updateNode(temp);
//更改选中的状态
console.log("isExistInUsers " + isExistInUsers(temp.id, checkedUserData));
if (isExistInUsers(temp.id, checkedUserData)) {
//选中节点
objTree.checkNode(temp, true, true);
//把父节点设为打开状态
objTree.expandNode(temp.getParentNode(), true, true, true, true);
}
// objTree.checkNode(temp,true);
}
}
// }
};
/**
* 判断用户是否已经被选中
*/
function isExistInUsers(userName, users) {
console.log("users的长度:" + users.length);
// console.log("users的内容:" + users);
if (users.length > 0) {
for (var i = 0; i < users.length; i++) {
if (userName == users[i].userName) {
return true;
}
}
}
return false;
};
</script>
var setting = {
view: {
selectedMulti: false
},
data: {
simpleData: {
enable: true
}
},
check: {
enable: true,
autoCheckTrigger: true
},
callback: {
onCheck: checkedNode
},
edit: {
enable: false
}
};
var objTree;
var checkedUserData; //已经在角色里面的旧用户信息
function initTree(zNode, userData) {
//清空选中人员框的数据
clearCheckName();
var dataNode = eval(zNode);
checkedUserData = JSON.parse(userData);
$.fn.zTree.init($("#treeDemo"), setting, dataNode);
objTree = $.fn.zTree.getZTreeObj("treeDemo");
objTree.onCheck = checkedNode;
var nodes = {};
nodes = objTree.getNodes();
//console.log(nodes[0].children.length);
changeNode(nodes[0]);
$('#modalTree').modal();
};
function checkedNode() {
var nodes = objTree.getCheckedNodes(true);
if (nodes.length < 1)
return;
var strName = "";
for (var i = 0; i < nodes.length; i++) {
var t = nodes[i];
if (nodes[i].type == "user") {
strName = strName + nodes[i].id + ";";
}
}
$("#checkedName").text(strName);
// console.log(strName);
// alert("ddd");
};
function getCheckName() {
return $("#checkedName").text();
};
function clearCheckName() {
return $("#checkedName").text("");
};
function changeNode(root) {
// console.log(root.getNodes().length);
// if(root.getNodes().length>0){
var ns = root.children;
for (var i = 0; i < ns.length; i++) {
if (ns[i].isParent && ns[i].children.length > 0) {
changeNode(ns[i]);
}
var temp = ns[i];
if (temp.type == "user") {
temp.iconSkin = "user";
objTree.updateNode(temp);
//更改选中的状态
console.log("isExistInUsers " + isExistInUsers(temp.id, checkedUserData));
if (isExistInUsers(temp.id, checkedUserData)) {
//选中节点
objTree.checkNode(temp, true, true);
//把父节点设为打开状态
objTree.expandNode(temp.getParentNode(), true, true, true, true);
}
// objTree.checkNode(temp,true);
}
}
// }
};
/**
* 判断用户是否已经被选中
*/
function isExistInUsers(userName, users) {
console.log("users的长度:" + users.length);
// console.log("users的内容:" + users);
if (users.length > 0) {
for (var i = 0; i < users.length; i++) {
if (userName == users[i].userName) {
return true;
}
}
}
return false;
};
</script>