zTree应用

aspx:<ul id="productTree"></ul> 


js: 
var productTreeInfo = { 
    tree: null, 
    setting: null, 
    nodes: null, 
    treeId: "productTree", 
    nodeId: "", 
    nodeName: "", 
    nodeLevel: 0 
}; 

jQuery(function ($) { 
    initTree(); 
}); 


//强制刷新树菜单 
var refresh = "no"; 
function refreshTree() { 
    //判断权限 
    if (!confirm("是否要继续刷新树菜单?该动作将会同步远程文件服务器!")) { 
        return; 
    } 
    refresh = "yes";    
    initTree();    

//获取知识库左侧文件夹树菜单 
function initTree() { 
    progressDialog.showDialog("正在加载数据..."); 
    productTreeInfo.setting = { 
        checkable: false, 
        async: true, 
        expandSpeed: 0, 
        asyncUrl: "/tree/QueryProductTreeChildNode",  //获取知识库左侧文件夹树菜单节点数据的URL地址 
        asyncParam: ["id"],  //获取节点数据时,必须的数据名称,例如:id、name 
        asyncParamOther: ["pdu", pdu, "pduaddress", pduaddress,"selecttreeid",$("#DivSelectTreeid").val(),"refresh",refresh], //其它参数 ( key, value 键值对格式) 
        callback: { 
            click: zTreeOnClick, 
            beforeClick: zTreeOnBeforeClick, 
            rightClick: zTreeOnRightClick,  //点击右键方法 
            beforeDrop: zTreeBeforeDrop, 
            asyncSuccess: zTreeOnAsyncSuccess 
        } 
    }; 
    productTreeInfo.nodes = [{id: "0", name: "全部文件", open:false, isParent: true, checked: false,url:"",target:"mv"}]; 
    productTreeInfo.tree = $("#" + productTreeInfo.treeId).zTree(productTreeInfo.setting, productTreeInfo.nodes); 
    productTreeInfo.tree.expandNode(productTreeInfo.tree.getNodes()[0], true, false); //加载第一级产品线的节点 
}

//点击右键方法 
function zTreeOnRightClick(event, treeId, treeNode) { 
    productTreeInfo.tree.selectNode(treeNode); 


function zTreeOnBeforeClick(treeId, treeNode) { 
    if (treeNode.id=="0") { 
        if (!confirm("将服务器的全部文件显示出来,此操作的花费时间将依服务器文件数量而定!是否继续?")) { 

        } else { 
            $(document.getElementById("mainFrm")).attr("src", "../pub/FileListReport?treename=全部文件"); 
        } 
    } 

function zTreeOnClick(event, treeId, treeNode) { 
    clickProductTree(treeId, treeNode); 


function clickProductTree(treeId, treeNode) { 
    var nodeId = treeNode.id; 
    if (nodeId == productTreeInfo.nodeId) { return; } //点击相同的节点不作任何操作 


function zTreeOnAsyncSuccess(event, treeId, msg) { 
    progressDialog.hideDialog(); 
    if (msg == "[]") { 
        treeNode.isParent = false; 
        productTreeInfo.tree = $("#" + productTreeInfo.treeId).zTree(productTreeInfo.setting, productTreeInfo.nodes); 

    } 
    //右键菜单 
    $("#productTree_1 span").RightMenu('myMenu2', { 
        menuList: [ 
            { menuName: "上传文件", menuclass: "1", clickEvent: "divClick(1)" }, 
            { menuName: "新建文件夹", menuclass: "2", clickEvent: "divClick(2)" }, 
{ menuName: "重命名", menuclass: "3", clickEvent: "divClick(3)" }, 
            { menuName: "删除", menuclass: "4", clickEvent: "divClick(4)" }, 
            { menuName: "导出文件夹", menuclass: "5", clickEvent: "divClick(5)" }, 
            { menuName: "管理者信息", menuclass: "6", clickEvent: "divClick(6)" }                                
            ] 
    }); 
    zTreeSelectNodeById(); 

    //新增、重命名文件夹返回展开选中状态 
    
    if ($("#DivSelectTreeid").val() != "") {        
        var treeNode = productTreeInfo.tree.getNodeByParam("id", $("#DivSelectTreeid").val());  
        productTreeInfo.tree.selectNode(treeNode); 
        productTreeInfo.tree.expandNode(treeNode, true, false); 
    } 
}

function divClick(s) { 
    var selecttreeid = productTreeInfo.tree.getSelectedNode().id; 
    var url = productTreeInfo.tree.getSelectedNode().url; 
    if (url!=undefined) { 
        var treepath = url.substring(url.lastIndexOf('=') + 1); 
    }    
    switch (s) { 
        case 1:            
            $('#mainFrm').attr('src', "/upload/FileUpload?" + url.substring(url.indexOf("?") + 1) + ""); 
        break; 
    case 2: 
        opencreatedialog(selecttreeid); 
        break; 
    case 3: 
        openrenamedialog(selecttreeid); 
        break; 
    case 4: 
        opendeletedialog(selecttreeid); 
        break; 
    case 5: 
        loadfolder(selecttreeid); 
        break; 
    case 6: 
        loadfoldermgrinfo(selecttreeid); 
        break; 
    case 7: 
        break; 
    default: 
        alert(selecttreeid); 
        break; 
    } 


function zTreeSelectNodeById(selecttreeid) {    
    if (typeof (selecttreeid) != undefined) { 
        var nodes = productTreeInfo.tree.getNodes();//这里获得0,有问题        
        iteratorNode(nodes); 
    } 


function iteratorNode(nodes) { 
    $.each(nodes, function (index, item) {        
        if (item.id == 111) {            
            productTreeInfo.tree.selectNode(item); 
            return false; 
        } 

        if (item.nodes != undefined) { 
            iteratorNode(item.nodes); 
        } 
    }); 

//获取知识库左侧文件夹树菜单节点数据 TreeController.cs 
        public ContentResult QueryProductTreeChildNode(string id, string pdu, string pduaddress, string selecttreeid, string from, string refresh) 
        { 
            string cnt = "[]"; 

            try 
            { 
                cnt = tree.BuildProductTreeChildNode(id, pdu, pduaddress, selecttreeid, from, refresh); 
            } 
            catch (System.Exception ex) 
            { 
                LogUitls.error(ex); 
            } 

            return Content(cnt); 
        } 


//ITreeRepository.cs 
String BuildProductTreeChildNode(string id, string pdu, string pduaddress, string selecttreeid,string from,string refresh);

//DfxRepository.cs 
#region 获取类别树 
        public string BuildCategoryTreeChildNode(string id, string pduid, string pduaddress, string selecttreeid) 
        { 
            List<ProductTreeClass> nodeList = new List<ProductTreeClass>(); 
            List<Int32> expandidList = new List<Int32>(); 

            List<MANAGER_CFG_CATEGORY> allList = catedb.MANAGER_CFG_CATEGORY.ToList(); 

            nodeList = GetCategoryChildNodes(ref allList, ref nodeList, int.Parse(id), expandidList, pduid); 

            return JsonDataConverter.Serialize(nodeList.ToArray()); 
        } 

        public List<ProductTreeClass> GetCategoryChildNodes(ref List<MANAGER_CFG_CATEGORY> allList, ref List<ProductTreeClass> nodeList, int id, List<Int32> expandidList, string pduid) 
        { 
            List<MANAGER_CFG_CATEGORY> list = new List<MANAGER_CFG_CATEGORY>(); 

            List<ProductTreeClass> childList = new List<ProductTreeClass>(); 

            //list = allList.Where(x => x.ParentId == id && x.PduId == Convert.ToInt32(pduid)).OrderByDescending(x => x.CateOrder).OrderByDescending(x => x.Id).ToList(); 
            list = (from x in allList where x.ParentId == id && x.PduId == Convert.ToInt32(pduid) orderby x.CateOrder, x.Id select x).ToList();

            foreach (var item in list) 
            { 
                ProductTreeClass node = new ProductTreeClass(); 
                node.id = item.Id.ToString(); 
                node.name = item.CateName; 
                node.isParent = false; 
                node.open = true; 
                node.url = "../Menu/CategoryMenu?treeid=" + item.Id + "&treename=" + item.CateName + "&typename="+item.TypeName+""; 
                node.target = "mv"; 
                //判断是否有子节点 
                if (allList.Where(x => x.ParentId == item.Id).Count() > 0) 
                { 
                    node.isParent = true; 
                } 

                node.nodes = GetCategoryChildNodes(ref allList, ref childList, item.Id, expandidList, pduid); 

                childList.Add(node); 
            } 

            return childList; 
        } 
        #endregion 


public class ProductTreeClass 
    { 
        public string id {get; set;} 
        public string name {get; set;} 
        public string url {get; set;} 
        public bool isParent {get; set;} 
        public bool open {get; set;} 
        public List<ProductTreeClass> nodes {get; set;} 
        public string userData {get; set;} 
        public string target { get; set; } 
    }

原文地址:https://www.cnblogs.com/cw_volcano/p/2216523.html