How does the Element ui tree menu get the parent node information of the clicked child node?

A tree menu node has three layers

<
    <1-1
         1-1-1
         1-1-2   
    <1-2
<
<

if I click on one year > > one year 1-1 > > one year 1-1-1,

then I"m going to get text messages like 1-1-1 in one year. How to write it?

the specific code implementation is in the answer below.

Mar.07,2021
The api that comes with

has a way to get the parent based on the current node. Get the current click on the node object, and then call getparentnode to get the parent. The recursive result is


Code implementation:
HTML Code

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.13.0/lib/index.js"></script>
<div id="app">
<el-tree
  :data="data"
  :props="defaultProps"
  ref="tree"
  accordion
  @node-click="handleNodeClick">
</el-tree>
</div>

JS Code

var Main = {
    data() {
      return {
        data: [{
          label: ' 1',
          children: [{
            label: ' 1-1',
            children: [{
              label: ' 1-1-1'
            }]
          }]
        }, {
          label: ' 2',
          children: [{
            label: ' 2-1',
            children: [{
              label: ' 2-1-1'
            }]
          }, {
            label: ' 2-2',
            children: [{
              label: ' 2-2-1'
            }]
          }]
        }, {
          label: ' 3',
          children: [{
            label: ' 3-1',
            children: [{
              label: ' 3-1-1'
            }]
          }, {
            label: ' 3-2',
            children: [{
              label: ' 3-2-1'
            }]
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      handleNodeClick(data,node) {      
        let selectNode = this.$refs.tree.getNode(node);        
      //  console.log(this.$refs.tree.getNode(data));         
        let selectedLabelArray = []
        let recursionParent = function (node) {
        if (!node.parent) {
            return
        }
        selectedLabelArray.push(node.label)
        recursionParent(node.parent)
        }
        recursionParent(selectNode)
        //  recursionParent(node)
        console.log(selectedLabelArray.reverse().join(' > '))                
      }
    }
  };
var Ctor = Vue.extend(Main)
new Ctor().$mount('-sharpapp')

CSS code block
@ import url ("/ / unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css");
copy the above code to online Vue code to see the effect ( may be slow to load )
you can also copy the code to run

locally.

nicejob


how did you get this big shot

Menu