0

I'm trying to build a TreeView from scratch in Vue. This is my code so far.

The first problem I'm encountering is that the content of the subfolders (like child folder 1) is not being displayed. The second problem is that minimizing the subfolders minimizes the whole treeview.

Could anyone help me understand why these two errors in functionality are occurring and how to fix them?

1 Answer 1

1
  1. your code only dealed with the first level of your folders, you should recursively revoke your tree components to deal with child folders. please refer to below article.

https://itnext.io/recursion-for-nested-tree-structure-components-in-vue-1cb600005475

  1. your code is using one param (isOpen) to toggle minimizing all folders, so if isOpen is false, all folders minimized; you should use item.isOpen to deal with different item;
treeData: {
        name: "My Tree",
        isOpen: true,
        children: [
          { name: "hello" },
          { name: "wat" },
          {
            name: "child folder",
            isOpen: false,
            children: [
              {
                name: "child folder 1",
                isOpen: false,
                children: [{ name: "hello" }, { name: "wat" }]
              },
              { name: "hello" },
              { name: "wat" },
              {
                name: "child folder 2",
                isOpen: false,
                children: [{ name: "hello" }, { name: "wat" }]
              }
            ]
          }
        ]
      }
    };
Sign up to request clarification or add additional context in comments.

2 Comments

In line 31 of TreeView.vue , which is <span v-if="isFolder">[{{ isOpen ? '-' : '+' }}]</span> , I tried replacing isOpen with child.isOpen, but I still can't get the child folder to expand.
codesandbox.io/s/loving-booth-q3x4p?file=/src/components/… here is a rough example. maybe some errors. just for reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.