How can I recursively create a UL/LI's from JSON data - multiple layers deep

前端 未结 7 775
暖寄归人
暖寄归人 2020-12-30 12:32

I am trying to use use the following JSON data to create the following similar structure in a recursive inner function with not much luck, really need some help and so if an

7条回答
  •  独厮守ぢ
    2020-12-30 13:06

    This is like a complete solution for generating UL/LI recursively from JSON config, which has customizable classes for each node and support of expand and collapse events for each node. This provides just a basic working model, from which you ll be able to expand and customize to your needs.

    I found this answer from https://techmeals.com/fe/questions/javascript/6/How-can-I-create-a-dynamic-tree-of-UL-and-LI-from-JSON-config

    Example JSON config file:

    var config = {
        "Menu-1-Level-1": {
            "label": "Menu-1-Level-1",
            "type": "treeView",
            "class": "Menu-1-Level-1",
            "children": [
                {
                    label: "Menu-1-Level-2",
                    type: "treeView",
                    "class": "Menu-1-Level-2",
                    children: [
                        {
                            label: "Menu-1-Level-3",
                            class: "Menu-1-Level-3"
                        }
                    ]
                },
                {
                    label : "Menu-2-Level-2",
                    class: "Menu-2-Level-2"
                }
            ]
        },
        "Menu-2-Level-1": {
            "label": "Menu-2-Level-1",
            "type": "treeView",
            "class": "Menu-2-Level-1",
            "children": [
                {
                    label: "Menu-1-Level-2",
                    class: "Menu-1-Level-2",
                    type: "treeView",
                    children: [
                        {
                            label: "Menu-1-Level-3",
                            class: "Menu-1-Level-3"
                        }
                    ]
                },
                {
                    label : "Menu-2-Level-2",
                    class : "Menu-2-Level-2"
                }
            ]
        }
    };
    

    HTML Code:

    
    
    
    
        Tree Menu
        
        
        
    
    
    
        

    Tree.js

    var tree;
    
    tree = function (treeNodeParent, dataObj) {
        this.dataObj = dataObj;
        this.treeNodeParent = treeNodeParent;
        this.treeNode = $(document.createElement("ul")).addClass("treeNode");
    };
    
    tree.prototype.expandCollapse = function (e) {
        var target = $(e.currentTarget), parentLabel = target.parent();
    
        if (parentLabel.hasClass("collapsed")) {
            parentLabel.removeClass("collapsed").addClass("expanded");
        } else {
            parentLabel.addClass("collapsed").removeClass("expanded");
        }
    };
    
    tree.prototype.attachEvents = function () {
        var me = this;
        me.treeNodeParent.delegate(".collapsed label, .expanded label", "click", me.expandCollapse);
    };
    
    tree.prototype.attachMarkUp = function () {
        var me = this;
        me.treeNodeParent.append(me.treeNode);
    };
    
    tree.prototype.getEachNodeMarkup = function (nodeObj, rootNode, selector) {
        var selectedNode, i, me = this;
    
        if (nodeObj.children) {
    
            if (!selector) {
                selectedNode = rootNode;
            } else {
                selectedNode = rootNode.find(selector);
            }
    
            nodeObj.class = nodeObj.class ? nodeObj.class : "";
            selectedNode.append($.parseHTML(""));
            selector = selector + " li[name=" + nodeObj.label + "] > ul";
    
            for (i = 0; i < nodeObj.children.length; i = i + 1) {
                me.getEachNodeMarkup(nodeObj.children[i], rootNode, selector);
            }
    
        } else {
            nodeObj.class = nodeObj.class ? nodeObj.class : "";
            rootNode.find(selector).append($.parseHTML("
  • " + "" + "
  • ")); } }; tree.prototype.getTree = function () { var component, me = this; for (component in me.dataObj) { if (me.dataObj.hasOwnProperty(component)) { me.getEachNodeMarkup(me.dataObj[component], me.treeNode, ""); } } me.attachMarkUp(); me.attachEvents(); return me.treeNode; };

    Tree.css

    .treeNode .collapsed > ul, .collapsed > li {
        display: none;
    }
    
    .treeNode .expanded > ul, .expanded > li {
        display: block;
    }
    

    testPage.js

    // the variable "config" is nothing but the config JSON defined initially. 
    treeNode = new tree($('.treeContainer .tree'), config); 
    treeNodeObj = treeNode.getTree();
    

    Look at the example provided at https://jsfiddle.net/3s3k3zLL/

提交回复
热议问题