zTree动态加载子节点

时间:2020-11-29 12:39:31

我这个案例里不是采用zTree默认的async进行异步加载,而是在onExpand事件里用ajax请求数据,

然后再动态的添加子节点,代码如下:

var setting = {
    data: {
        keep: {
            parent: true,
        }
    },
    callback: {
        onExpand: expandNode,
        beforeExpand: beforeExpand,
    },
};

//展开节点
var parentNode = null;
function expandNode(e, b, node) {
    if (node.AppID != null) {
        parentNode = node;
        $.ibo.crossOrgin({
            url: "url",
            funcName: "function",
            data: "data",
            success: function (res) {
                if (res.ResFlag == $.ResFlag.Success) {
                    if (res.ResObj && res.ResObj.List && res.ResObj.List.length > 0) {
                        var newNodes = [];
                        $.each(res.ResObj.List, function (i, n) {
                            newNodes.push({ name: n.Decription, icon: "../../img/icon.png" });
                        });
                        var treeObj = $.fn.zTree.getZTreeObj("treeDiv");
                        treeObj.addNodes(parentNode, newNodes);
                    }
                }
            }
        });
    }
};

function beforeExpand(e, node) {
    var treeObj = $.fn.zTree.getZTreeObj("treeDiv");
    parentNode = node;
    treeObj.removeChildNodes(parentNode);
};