开发者社区> 问答> 正文

关于zTree插件删除节点的问题

我的beforeRemove和onRemove为什么不能弹出?我哪里有问题吗?

var zTree, rMenu;
 
function OnRightClick(event, treeId, treeNode) {
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
        zTree.cancelSelectedNode();
        showRMenu("root", event.clientX, event.clientY);
    } else if (treeNode && !treeNode.noR) {
        zTree.selectNode(treeNode);
        showRMenu("node", event.clientX, event.clientY);
    }
}
 
function showRMenu(type, x, y) {
    $("#rMenu ul").show();
    if (type == "root") {
        $("#m_del").hide();
    } else {
        $("#m_del").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 onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        rMenu.css({
            "visibility": "hidden"
        });
    }
}
 
var addCount;
 
function getAndEditNode(zTree, fNode) {
    var nNode = zTree.getNodeByParam("temp", addCount, null);
    nNode.temp = 0;
    nNode.Ev = 0;
    if (fNode) {
        nNode.Fid = fNode.Id;
    } else {
        nNode.Fid = "0";
    }
    editTreeNode(nNode);
    addCount++;
}
 
function addTreeNode() {
    hideRMenu();
    var newNode = {
        Name: "新增" + addCount,
        temp: addCount
    };
    var fNode = zTree.getSelectedNodes()[0];
    if (fNode) {
        zTree.addNodes(fNode, newNode);
        getAndEditNode(zTree, fNode);
    } else {
        zTree.addNodes(null, newNode);
        getAndEditNode(zTree, fNode);
    }
}
 
function postData(jsonData, url) {
    var str;
    $.ajax({
        type: "post",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: url,
        data: jsonData,
        async: false,
        success: function(data) {
            str = data.Id;
        }
    });
    return str;
}
 
function editTreeNode(nNode) {
    hideRMenu();
    if (nNode) {
        zTree.editName(nNode);
    }
}
 
function beforeRename(treeId, treeNode, newName, isCancel) {
    if (newName.length == 0) {
        alert("节点名称不能为空.");
        return false;
    }
    return true;
}
 
function onRename(e, treeId, treeNode, isCancel) {
    if (treeNode.Ev == 0) {
        var id = postData(JSON.stringify(treeNode), "/green/category/create");
        treeNode.Id = id;
    } else {
        postData(JSON.stringify(treeNode), "/green/category/update");
        zTree.updateNode(treeNode);
    }
 
}
 
function beforeRemove(treeId, treeNode) {
    alert("treeId:" + treeNode.Id);
    return true;
}
 
function onRemove(event, treeId, treeNode) {
    alert("treeId:" + treeNode.Id);
}
 
$(document).ready(function() {
 
    $("#cangkuguanli").bind("click", function() {
        $("#treeBox").css("display", "block");
        $("article").css("width", "544px");
 
        var setting = {
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                url: "green/category/list"
            },
            data: {
                key: {
                    name: "Name"
                },
                simpleData: {
                    enable: true,
                    idKey: "Id",
                    pIdKey: "Fid",
                    rootPId: 0
                }
            },
            callback: {
                beforeRename: beforeRename,
                beforeRemove: beforeRemove,
                onRename: onRename,
                onRemove: onRemove,
                onRightClick: OnRightClick
            }
        };
 
        $.fn.zTree.init($("#tree"), setting);
        zTree = $.fn.zTree.getZTreeObj("tree");
        rMenu = $("#rMenu");
        addCount = 1;
 
    });
 
    //ztree添加一个新节点
    $("#m_add").click(function() {
        addTreeNode();
    });
 
    //ztree修改一个节点名称
    $("#m_rename").click(function() {
        var nNode = zTree.getSelectedNodes()[0];
        nNode.Ev = 1;
        editTreeNode(nNode);
    });
 
    //ztree删除一个节点
    $("#m_del").click(function() {
        var nNode = zTree.getSelectedNodes()[0];
        if (nNode) {
            hideRMenu();
            zTree.removeNode(nNode);
        }
 
    });
 
});

展开
收起
a123456678 2016-07-11 15:16:53 2592 0
1 条回答
写回答
取消 提交回答
  • 133行 开始 showRemoveBtn:true 就可以了 这是显示删除按钮

    同理,下面showRenameBtn true 显示编辑名图标

    2019-07-17 19:54:14
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载