代码之家  ›  专栏  ›  技术社区  ›  quester

使用递归渲染虚拟树

  •  0
  • quester  · 技术社区  · 2 年前

    给定一个类似于虚拟树的对象。您需要递归地遍历对象的所有分支,并根据其内部层次结构来渲染元素。

    我已经写了一个函数,但它有错误。

    我无法重置父变量。似乎有必要使用递归以不同的方式实现函数

    const tree = {
        type: 'component',
        children: [
            {
                type: 'host',
                tag: 'div',
                children: 'div content',
            },
            {
                type: 'component',
                children: [
                    {
                        type: 'host',
                        tag: 'div',
                        children: [
                            {
                                type: 'host',
                                tag: 'span',
                                children: 'span content'
                            },
                            {
                                type: 'host',
                                tag: 'span',
                                children: 'span content'
                            },
                        ]
                    }
                ]
            },
            {
                type: 'host',
                tag: 'div',
                children: 'div content',
            }
        ]
    }
    
    let parent = null;
    
    function renderTree(tree) {
        const treeBox = document.getElementById('tree');
    
        for (const key in tree) {
            const value = tree[key];
    
            if (key === 'children') {
            }
    
            if (Array.isArray(value)) {
                renderTree(value);
            }
    
            if (!Array.isArray(value) && typeof value === "object") {
                if (value.type !== "component") {
                    let isNested = false;
    
                    const element = document.createElement(value.tag);
    
                    if (parent) {
                        parent.appendChild(element);
                    } else {
                        treeBox.appendChild(element);
                    }
    
                    if (Array.isArray(value.children)) {
                        isNested = !isNested;
                        parent = element;
                    }
    
                    if (!Array.isArray(value.children)) {
                        element.textContent = value.children;
                    }
    
                    // if(isNested == false) {
                    //   parent = null;          
                    // }
    
                }
    
                renderTree(value)
            }
        }
    }
    
    renderTree(tree);
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   AKX Bryan Oakley    2 年前

    假设 component s不需要有身体上的存在,但他们只会让他们的孩子:

    const tree = {
      type: "component",
      children: [
        {
          type: "host",
          tag: "div",
          children: "div content",
        },
        {
          type: "component",
          children: [
            {
              type: "host",
              tag: "div",
              children: [
                {
                  type: "host",
                  tag: "span",
                  children: "span content",
                },
                {
                  type: "host",
                  tag: "span",
                  children: "span content",
                },
              ],
            },
          ],
        },
        {
          type: "host",
          tag: "div",
          children: "div content",
        },
      ],
    };
    
    function renderTreeNode(tn, element) {
      const { type, tag, children } = tn;
      if (type === "component") {
        for (const child of children) {
          renderTreeNode(child, element);
        }
      } else if (type === "host") {
        const el = document.createElement(tag);
        if (Array.isArray(children)) {
          for (const child of children) {
            renderTreeNode(child, el);
          }
        } else {
          el.textContent = children;
        }
        element.appendChild(el);
      } else {
        throw new Error("Unknown type");
      }
    }
    
    renderTreeNode(tree, document.body);
    div {
      background: pink;
      padding: 1em;
      margin: 1em;
    }
    
    span {
      background: orange;
    }