当前位置:网站首页>How to get the parent node of all nodes in El tree

How to get the parent node of all nodes in El tree

2022-07-04 05:41:00 _ Virgo programmer's daily life

  <el-tree ref="tree" :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
  <script>
  import {
     throttle } from 'lodash'
  export default {
    
    data() {
    
      return {
    
        data: [
          {
    
            label: ' Class A  1',
            value: '1',
            children: [{
    
              label: ' second level  1-1',
              value: '1-1',
              children: [{
    
                label: ' Level three  1-1-1',
                value: '1-1-1'
              }]
            }]
          }, {
    
            label: ' Class A  2',
            value: '1-2',
            children: [{
    
              label: ' second level  2-1',
              value: '2-1',
              children: [{
    
                label: ' Level three  2-1-1',
                value: '2-1-1'
              }]
            }, {
    
              label: ' second level  2-2',
              value: '2-2',
              children: [{
    
                label: ' Level three  2-2-1',
                value: '2-1-1'
              }]
            }]
          }, {
    
            label: ' Class A  3',
            value: '3',
            children: [{
    
              label: ' second level  3-1',
              value: '3-1',
              children: [{
    
                label: ' Level three  3-1-1',
                value: '3-1-1'
              }]
            }, {
    
              label: ' second level  3-2',
              value: '3-2',
              children: [{
    
                label: ' Level three  3-2-1',
                value: '3-2-1'
              }]
            }]
          }],
        defaultProps: {
    
          children: 'children',
          label: 'label'
        }
      }
    },
    methods: {
    
 
      handleNodeClick(data, node) {
    
        // console.log("data How much is? ");
        // console.log(node);
        // let selectedNode = this.$refs.tree.getNode(node);
        this.breadList =[]
        this.platform(node);
        console.log("value How much is? ");
        console.log(this.breadList);
      },
      //  Recursive function 
      platform(node) {
    
        if (!node.parent) {
    //  If there is no parent node , Then return directly 
          return
        }
        this.breadList.unshift(node.data.value)//  take value Save up 
        // Call recursion 
        this.platform(node.parent)
      },
    }

</script>

Click the third level to print the results as follows :
 Insert picture description here

原网站

版权声明
本文为[_ Virgo programmer's daily life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040529279837.html