ztree显示右键树

	var rightClickX,rightClickY;

	var setting = {  
            view: {
                dblClickExpand: false  
            },  
            data: {  
                simpleData: {  
                    enable: true  
                }  
            },  
            check: {  
                enable: true  
            },  
            callback: {  
                onRightClick: OnRightClick,
                beforeRename: zTreeBeforeRename
            },
     };
  
        function OnRightClick(event, treeId, treeNode) {  
            if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {  
                zTree.cancelSelectedNode();  
                rightClickX=event.clientX;
                rightClickY=event.clientY;
                alert(rightClickX+"=="+rightClickY);
                showRMenu("root", event.clientX, event.clientY);  
            } else if (treeNode && !treeNode.noR) {
                zTree.selectNode(treeNode);  
                rightClickX=event.clientX;
                rightClickY=event.clientY;
                showRMenu("node", event.clientX, event.clientY);  
            }  
        }  
  /*
        function showRMenu(type, x, y) {  
            $("#rMenu ul").show();  
            alert("showRMenu1");
            if (type == "root") {  
                $("#m_del").hide();  
                $("#m_check").hide();  
                $("#m_unCheck").hide();  
            } else {  
                $("#m_del").show();  
                $("#m_check").show();  
                $("#m_unCheck").show();  
            }  
            rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });  
            $("body").bind("mousedown", onBodyMouseDown);  
        }  
        function hideRMenu() {  
            if (rMenu) rMenu.css({ "visibility": "hidden" });  
            $("body").unbind("mousedown", onBodyMouseDown);  
        }  
  */
        //显示右鍵  
        function showRMenu(type, x, y) {  
            $("#rMenu").show();  
            if (type == "root") {  
                $("#rMenu").hide();  
            } else {  
                $("#m_del").show();  
                $("#m_modify").show();  
            }  
            //0000000,0000001,0000002,不可以删除修改     
            zTree = $.fn.zTree.getZTreeObj("tree");  
            nodes = zTree.getSelectedNodes();  
            treeNode = nodes[0];  
            if (treeNode != null) {  
                treeID = treeNode.pId;
                if (treeID == null) {
                    $("#m_modify").hide();  
                    $("#m_del").hide();  
                }
            }  
  
            rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });  
            $("body").bind("mousedown", onBodyMouseDown);  
        }  
  
  
        //隐藏右鍵  
        function hideRMenu() {  
            if (rMenu) rMenu.css({ "visibility": "hidden" });  
            $("body").unbind("mousedown", onBodyMouseDown);  
        }  
  
  
        function onBodyMouseDown(event) {  
            if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {  
                rMenu.css({ "visibility": "hidden" });  
            }  
            if (!(event.target.id == "rMenu2" || $(event.target).parents("#rMenu2").length > 0)) {  
                $("#rMenu2").css({ "visibility": "hidden" });  
            }  
        }  
  
        //鼠标点击事件不在节点上时隐藏右键菜单     
        $(function () {  
            $("body").bind(  
            "mousedown",  
            function (event) {  
                if (!(event.target.id == "rMenu2" || $(event.target)  
                        .parents("#rMenu2").length > 0)) {  
                    $("#rMenu2").hide();  
                }  
            });  
        });  
  
        /*******************新增  start*********************/  
        //显示新增  
        function showRMenu2(x, y) {  
            $("#rMenu2").show();  
            $("#rMenu2").css({ "top": y + "px", "left": x + "px", "visibility": "visible" });  
            $("body").bind("mousedown", onBodyMouseDown);  
        }  
  
        var addCount = 1;  
        //新增树节点  
        function addTreeNode() {  
            $("#rMenu").hide();  
            //显示新增div  
            var tag = 0;
            //$("#tree").mousemove(function (e) {  
                if (tag == 0){
                showRMenu2(rightClickX, rightClickY);  
                $("#typeCode").focus();  
                tag = tag + 1;
                }
           // });  
        }  
  
  
        //新增执行后台  
        function addTree() {  
            var zTree = $.fn.zTree.getZTreeObj("tree");  
            var nodes = zTree.getSelectedNodes();  
            $.ajax({  
                type: "post",  
                url: "/Image/moduleType.do",  
                dataType: 'json',  
                data: {  
                    action: "addTreeNode",  
                    parentid: nodes[0].id,  
                    typeCode: $("#typeCode").val(),
                    typeName: encodeURI($("#typeName").val()),
                    remark: encodeURI($("#remark").val()),
                    moduleCode: $("#moduleCode").val()
                },  
                success: function (json) {  
                    //强行加载父节点  
                    var treeObj = $.fn.zTree.getZTreeObj("tree");  
                    isParent = true;
                    //var nodes = treeObj.getSelectedNodes();  
                    var nodes = treeObj.getSelectedNodes();
                    treeNode = nodes[0];
					treeNode = treeObj.addNodes(treeNode, json);

                    //treeObj.reAsyncChildNodes(null, "refresh", true);
                    if (json == 0) alert("操作失败!");  
                },  
                error: function () {  
                    alert('Error loading HTML document');  
                }  
            });  
            $("#rMenu2").css({ "visibility": "hidden" });  
            $("#typeCode").val("");  
            $("#typeName").val("");  
            $("#remark").val("");  
  
        }  
        /*******************新增  end*********************/  
  
        /*******************修改  start*********************/  
        function editName() {  
            $("#rMenu").hide();  
            zTree = $.fn.zTree.getZTreeObj("tree");  
            nodes = zTree.getSelectedNodes();  
            zTree.editName(nodes[0]);  
        }  
  
        //捕获节点编辑名称结束(Input 失去焦点 或 按下 Enter 键)之后,更新节点名称数据之前的事件回调函数,并且根据返回值确定是否允许更改名称的操作  
        function zTreeBeforeRename(treeId, treeNode, newName,codes) {  

            zTree = $.fn.zTree.getZTreeObj("tree");  
            nodes = zTree.getSelectedNodes();  
            treeId = "tree";  
            treeNode = nodes[0];  
            //newName = nodes[0].name;
            //alert(newName);
            if (newName.length == 0) {  
                alert("节点名称不能为空.");  
                return false;  
            }  
            $.ajax({  
                type: "post",  
                url: "/Image/moduleType.do",  
                dataType: 'json',  
                data: {  
                    action: "modifyTree",  
                    id: nodes[0].id,  
                    nodename: encodeURI(newName),
                    moduleCode: $("#moduleCode").val()
                },  
                success: function (json) {  
                    alert('操作成功!');  
                },  
                error: function () {  
                    alert('Error loading HTML document');  
                }  
            });  
            return true;  
        }  
        /*******************修改  end*********************/  
        function removeTreeNode() {  
            hideRMenu();  
            var nodes = zTree.getSelectedNodes();  
            if (nodes && nodes.length > 0) {  
                if (nodes[0].children && nodes[0].children.length > 0) {
                
                	var msg ="不能删除根节点!"
                	alert(msg); 
                } else {
                
                $.ajax({
        			type: "POST",       
        			url: "/Image/moduleType.do",
        			data: {
        				action: "deleteTree",
        				id: nodes[0].id,
        				moduleCode: $("#moduleCode").val()
        			},
        			success: function(msg){            
        			}
        			});
                    zTree.removeNode(nodes[0]);  
                }  
            }  
        }  
        function checkTreeNode(checked) {  
            var nodes = zTree.getSelectedNodes();  
            if (nodes && nodes.length > 0) {  
                zTree.checkNode(nodes[0], checked, true);  
            }  
            hideRMenu();  
        }  
        function resetTree() {  
            hideRMenu();  
            $.fn.zTree.init($("#tree"), setting, zNodes);  
        }  
  
        function setCheck() {  
            var zTree = $.fn.zTree.getZTreeObj("tree");  
            var nodes = zTree.transformToArray(zTree.getNodes());  
            //设置跟节点不显示复选框  
            /**

            for (var i = 0; i < nodes.length; i++) {  
                var id = nodes[i].id;  
                for (var j = 0; j < nodes.length; j++) {  
                    if (id == nodes[j].pId) {  
                        node = zTree.getNodeByParam("id", id, null);  
                        node.nocheck = true;  
                        zTree.updateNode(node);  
                    }  
                }  
            }
            */  
        }  
  
        //初始化树     
     var zTree, rMenu;    
	 var treeNodes; 
     $(function(){    
       $.ajax({  
        async : false,  
        cache:false,  
        type: 'POST',  
        dataType : "json",  
        url: "/Image/moduleType.do", //请求的action路径 
        data: {  
            action: "doGetPrivilegeTree",  
            moduleCode: $("#moduleCode").val()
        },  
        error: function () {//请求失败处理函数  
            alert('请求失败');  
        },  
        success:function(data){ //请求成功后处理函数。    
            treeNodes = data;   //把后台封装好的简单Json格式赋给treeNodes  
        }  
    });  
    $.fn.zTree.init($("#tree"), setting, treeNodes);
    zTree = $.fn.zTree.getZTreeObj("tree");
    setCheck();
    rMenu = $("#rMenu");  
});
        
        
        
        //-->  
  
        function b_submit() {
            var tree = $.fn.zTree.getZTreeObj("tree");  
            var nodes = tree.getCheckedNodes(true);  
            var nodesArr = new Array();  

            for (var i = 0; i < nodes.length; i++) {  
                nodesArr[i] = nodes[i].id;
            }
            $.ajax({
        		type: "POST",       
        		url: "/Image/moduleType.do",
        		data: {
        			action: "setChecked",
        			idarr: nodesArr,
        			moduleCode: $("#moduleCode").val()
        		},
        		success: function(msg){
        			msg="设置成功!"
        			alert(msg);   
        		}
        	});

        }
        
        function b_back(){
        url = "/Image/moduleManager.do?action=prepareQuery&sysCode="+$("#sysCode").val();
    	window.location.href = url;
        }



     
   

 
       
     
               
  • 类型代码:  
                   
               

  •            
  • 类型名称:
                   
               

  •            
  • 备  注:
                     
               

  •            

  •                
    确定

               
  •  
           
 
   
 
     
   

你可能感兴趣的:(jquery,ztree)