Convert JSON to HTML Tree

前端 未结 6 1725
悲&欢浪女
悲&欢浪女 2020-12-13 16:03

I would like to generate an HTML tree (preferably UL-LI) from the JSON example below. Does anyone have a simple, recursive JS function (not a framework) tha

相关标签:
6条回答
  • 2020-12-13 16:15

    @Boldewyn: I believe you can also use a For...In loop instead of a regular For loop to shorten the code a bit. Of course, I don't have much experience using this kind of loop, so please check my code snippet.

    for (var i in obj.folder) {
        li = document.createElement ("li");
        li.appendChild (document.createTextNode (i.title));
        // if the child has a 'folder' prop on its own, call me again
        if (i.folder) {
          li.appendChild (to_ul (i.folder));
        }
    }
    
    0 讨论(0)
  • 2020-12-13 16:22

    Check out the jquery plugin JSON2HTML, it's a little simpler to use than PURE and I've used it on a couple of site's I've created.

    http://json2html.com

    0 讨论(0)
  • 2020-12-13 16:27
    function to_li(obj, name) {
        var li = document.createElement("li");
        if (typeof(name) != "undefined") {
            var strong = document.createElement("strong");
            strong.appendChild(document.createTextNode(name + ": "));
            li.appendChild(strong);
        }
        if (typeof(obj) != "object"){
            li.appendChild(document.createTextNode(obj));
        } else {
            var ul = document.createElement ("ul");
            for (var prop in obj){
                ul.appendChild(to_li(obj[prop],prop));
            }
            li.appendChild(ul);
        }
        return li;
    }
    
    0 讨论(0)
  • 2020-12-13 16:28
    function to_ul (obj) {
      // --------v create an <ul> element
      var f, li, ul = document.createElement ("ul");
      // --v loop through its children
      for (f = 0; f < obj.folder.length; f++) {
        li = document.createElement ("li");
        li.appendChild (document.createTextNode (obj.folder[f].title));
        // if the child has a 'folder' prop on its own, call me again
        if (obj.folder[f].folder) {
          li.appendChild (to_ul (obj.folder[f].folder));
        }
        ul.appendChild (li);
      }
      return ul;
    }
    

    Caveat: No error checking! If a 'title' or 'folder' is missing, the whole thing could blow up.

    0 讨论(0)
  • 2020-12-13 16:30

    I had a problem getting my browser to accept the data structure submitted by the OP, but here is a fully working example I've drawn up for my own, similar purposes. Beside the function I provide the data structure as well, with name/branches instead of title/folder.

    function to_ul(branches) {
      var ul = document.createElement("ul");
    
      for (var i = 0, n = branches.length; i < n; i++) {
        var branch = branches[i];
        var li = document.createElement("li");
    
        var text = document.createTextNode(branch.name);
        li.appendChild(text);
    
        if (branch.branches) {
          li.appendChild(to_ul(branch.branches));
        }
    
        ul.appendChild(li);
      }
    
      return ul;
    }
    
    function renderTree() {
      var treeEl = document.getElementById("tree");
    
      var treeObj = {
        "root": [{
          "name": "George & Sarah Trede",
          "branches": [{
            "name": "George & Frances Trede",
            "branches": [{
              "name": "Mary (Trede) Jempty"
            }, {
              "name": "Carol (Trede) Moeller"
            }]
          }, {
            "name": "Mary (Trede) Sheehan"
          }, {
            "name": "Ward Trede"
          }]
        }]
      };
    
      treeEl.appendChild(to_ul(treeObj.root));
    }
    
    renderTree()
    <div id="tree"></div>

    0 讨论(0)
  • 2020-12-13 16:36

    I've used PURE with some success in the past for this kind of thing.

    0 讨论(0)
提交回复
热议问题