jquery zTree 查找所有的叶子节点

jquery zTree 查找所有的叶子节点

// 保存所有叶子节点  10 为初始化大小,并非数组上限
var arrayObj = new Array([10]);

/*
  treeNode:
         根节点
  result:
        用于保存叶子节点的数组
*/
// 获取所有叶子节点
function getAllLeafNodes(treeNode, result) {
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                if (!childrenNodes[i].isParent) {
                    arrayObj.push(childrenNodes[i]);
                } else {
                    arrayObj = getAllLeafNodes(childrenNodes[i], arrayObj);
                }
            }
        }
    } else {
        arrayObj.push(treeNode);
    }
    return arrayObj;
}

 

常用zTree 操作:

    // 获取树节点
    var treeObj = $.fn.zTree.getZTreeObj("divTreeContainer");
    // 获取树所有的节点
    var nodes = treeObj.getNodes();
   // 通过 tid  获取节点
  var node = treeObj.getNodeByTId(node.tId);

   // 创建一个节点
   var newNode = { id: 1, name:‘wisdo’‘, filePath: ‘http://www.cnblogs.com/wisdo‘};

   // 追加节点
   treeObj.addNodes(parentNode, newNode)
 

 

(function () {
    var treeIndexZTree = {
        //树的配置
        setting: {
            data: {
                key: {
                    //title: "t"
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {

            }
        },

        //存放树的节点
        zNodes: null,

        //初始化
        init: function (zNodes) {
            if (!zNodes) {
                zNodes = treeIndexZTree.getZNoeds();
            }
            treeIndexZTree.zNodes = zNodes; //获取树节点
            treeIndexZTree.setting.callback.onClick = treeIndexZTree.onClick; //绑定鼠标左键单击事件
            treeIndexZTree.setting.callback.onRightClick = treeIndexZTree.onRightClick; //绑定鼠标右键单击事件
            $.fn.zTree.init($("#divTreeContainer"), treeIndexZTree.setting, treeIndexZTree.zNodes); //初始化树
        },

        //鼠标左键单击事件
        onClick: function (event, treeId, treeNode, clickFlag) {
            if (tagAreaChange) {
                tagAreaChange(treeNode);
            }
        },

        //鼠标右键单击事件
        onRightClick: function (event, treeId, treeNode) {
            //$("#divTreeRightMenu").css({ "display": "block", left: event.clientX, top: event.clientY });
        },

        //获取树节点
        getZNoeds: function () {
            return [];
        }
    };

    //dom创建完成之后执行
    $(function () {
        //设置标签点树样式
        $("#divTreeContainer").css({ "overflow-y": "auto" });
        $.ajax({
            url: "/Wisdo/Home/GetTree",
            data: { level: true },
            type: "get",
            async: true,
            dataType: ‘json‘,
            success: function (obj) {
                //debugger;
                treeIndexZTree.init(obj.data);//ztree初始化
                refreshTree(); // 追加子节点,刷新树
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                 console.log(JSON.stringify(XMLHttpRequest));
                console.log(XMLHttpRequest.status);
            }
        });
    });
})();

 

    

 

zTree 官网 API开发文档:http://www.ztree.me/v3/api.php

文章来自:http://www.cnblogs.com/wisdo/p/5074424.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3