q-tree: Is there a getParentNode() method?

  • Each node in my tree has a unique nodeid property and node-key="nodeid".

    With a set of ticked nodes, I also need to get their parents. getNodeByKey() and getTickedNodes() return node objects but it seems I cannot do:

    const aNode = this.$refs.myTree.getNodeByKey(nodeid)
    const parentNode = aNode.getParent()

    Instead I have made my own method:

        getParentNode (nodeid) {
          const nodeMeta = this.$refs.myTree.meta[nodeid]
          const parentNode = this.$refs.myTree.getNodeByKey(nodeMeta.parent.key)
          return parentNode

    Am I missing something or is that the way to do it?

  • A more robust version:

        getParentNode (nodeid) {
          let parentNode = null
          if (nodeid) {
            const nodeMeta = this.$refs.projectTree.meta[nodeid]
            if (nodeMeta.parent) {
              parentNode = this.$refs.projectTree.getNodeByKey(nodeMeta.parent.key)
          return parentNode

  • @murrah I use a recursive function for this, my node structure is unlimited nesting. i use scope-slots ie.

    // node model
      id: 1, // unique id
      name: '',
      description: '',
      children: [], // same array of objects like this node
      parent_id: null, // null indicates this node is root, else holds id of its immediate parent
      ... // other props
    getRootNode (tree, node) {
          let curNode = node.parent_id !== null ? tree.getNodeByKey(node.parent_id) : node
          if (curNode.parent_id !== null) {
            curNode = this.getRootNode(tree, curNode)
          return curNode

  • @metalsadman Thanks! 🙂

Log in to reply