通过后台定义获取顶级节点

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> ZTREE DEMO - select menu</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../css/demo.css" type="text/css">
<link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.exedit.js"></script>
<SCRIPT type="text/javascript">
<!--
var setting = {
view: {
dblClickExpand: false
},
check: {
enable: true
},
callback: {
onRightClick: OnRightClick
}
};

var zNodes =[
{id:1, name:"无右键菜单 1", open:true, noR:true,
children:[
{id:11, name:"节点 1-1", noR:true},
{id:12, name:"节点 1-2", noR:true}

]},
{id:2, name:"右键操作 2", open:true,
children:[
{id:21, name:"节点 2-1"},
{id:22, name:"节点 2-2"},
{id:23, name:"节点 2-3"},
{id:24, name:"节点 2-4"}
]},
{id:3, name:"右键操作 3", open:true,
children:[
{id:31, name:"节点 3-1"},
{id:32, name:"节点 3-2"},
{id:33, name:"节点 3-3"},
{id:34, name:"节点 3-4"}
]}
];

/*function OnRightClick(event, treeId, treeNode) {
var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
var sNodes = treeObj.getSelectedNodes();

if (sNodes.length > 0) {
var parentTId = sNodes[0].parentTId;
if(parentTId==null){
showRMenu();
}

}

}
*/
function OnRightClick(event, treeId, treeNode) {
var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
var sNodes = treeObj.getSelectedNodes();

if (sNodes.length > 0) {
var parentTId = sNodes[0].parentTId;
//我门后台人员把所有的顶级节点设置id为0;这样轻而易举就可以拿到顶层节点了
if(treeNode.pid==0){
showRMenu();
}

}

}
function showRMenu(event) {
var e = event || window.event;
var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var x = e.pageX || e.clientX + scrollX;
var y = e.pageY || e.clientY + scrollY;
rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
$("body").bind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event){
if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
rMenu.css({"visibility" : "hidden"});
}
}
function shoucang() {

}

$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
zTree = $.fn.zTree.getZTreeObj("treeDemo");
rMenu = $("#rMenu");
});
//-->
</SCRIPT>
<style type="text/css">
div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
div#rMenu ul li{
margin: 1px 0;
padding: 0 5px;
cursor: pointer;
list-style: none outside none;
background-color: #DFDFDF;
}
</style>
</HEAD>

<BODY>


<ul id="treeDemo" class="ztree"></ul>

<div id="rMenu">
<ul>
<li id="m_add" onclick="shoucang();">收藏</li>
</ul>
</div>
</BODY>
</HTML>

posted @ 2017-11-22 09:48  一只爱study的girl  阅读(293)  评论(0编辑  收藏  举报