el-tree树形结构动态更新数据

本文介绍了一个基于Vue的表格组件实现,该组件允许用户在表格中进行树形数据的选择。通过使用el-table、el-tree等Element UI组件,实现了动态添加机构的功能,并详细解析了如何通过双向数据绑定和事件监听来更新选中的树节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果展示:
在这里插入图片描述
代码解析:

<template>
  <div class="newuser">
    <div class="setBox">
      <el-row class="addRole">
        <el-col :span="11">机构</el-col>
        <el-col :span="5"><span @click="AddForm"><i class="el-icon-circle-plus-outline" style="color:#409EFF"></i></span></el-col>
      </el-row>
      <el-table :data="orglists" class="list" style="width: 100%;background: #F9F9F9!important;border: 1px solid #E9E9E9;">
        <el-table-column multiple collapse-tags min-width="45%">
          <template slot-scope="scope">
              <!--通过双向数据绑定的方式,将树中选中的数据添加到option中,其中scopr.$index用于将对应的数据绑定到指定的位置,唯一识别标志-->
              <el-select v-model="mineStatus[scope.$index]" placeholder="请选择" multiple collapse-tags>
                <el-option :value="mineStatus[scope.$index]">
                  <el-tree :data="data" :props="defaultProps" :ref="ref+scope.$index" accordion @node-expand="getNodeInfo" @node-click="handleNodeClick(scope.$index)"></el-tree>
                </el-option>
              </el-select>      
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
  export default {
    data () {
      return{
        ref:'tree',//动态添加机构,对应的每一条的ref都应该唯一,否则互相影响
        mineStatus: [],
        orglists:[],
        data: [{
           id: 1,
            label: '一级 1',
            children: [{
                id: 4,
                label: '二级 1-1'
            }]
        }, {
            id: 2,
            label: '一级 2',
            children: [{
                id: 5,
                label: '二级 2-1'
            }, {
                id: 6,
                label: '二级 2-2'
            }]
        }, {
            id: 3,
            label: '一级 3',
            children: [{
                id: 7,
                label: '二级 3-1'
            }, {
                id: 8,
                label: '二级 3-2'
            }]
        }],
        defaultProps: {
              children: 'children',
              label: 'label'
        },
      }
    },
    created () {
      //模拟初始化数据
      this.getInfo()
    },
    
    methods:{
      getInfo(){
        var info = ['二级 2-1','二级 3-2']
        this.orglists = info;
        for(var i=0; i<info.length; i++){
          var a = []
          a.push(info[i])
          this.$set(this.mineStatus,i,a)
        }
      },
      //点击tree选择数据
      handleNodeClick(a){
        //获取当前选中的数据
        let res = this.$refs['tree'+a].getCurrentNode(true,true)
        let arrLabel = []
        arrLabel.push(res.label)
        //vue中数组传值,不能直接puth,因为内存数据还没有修改,需要通过set进行修改
        this.$set(this.mineStatus,a,arrLabel)
      },
      AddForm () {
        let cope = {
          name:'',
          value:'',
          id:'',
          label:''
        }
        /**
          this.mineStatus的数据结构为[[],[],[]],每添加一个则push一个空的数组
        */
        this.mineStatus.push([])
        this.orglists.push(cope)
      },
      getNodeInfo(node,resolve) {
        
      }
  }
}
</script>
<style lang="scss" scoped>
  .newuser{
    height: 100%;
    .el-breadcrumb{
      padding: 20px 0 16px 0
    }
    .setBox{
      min-height: calc(100% - 70px);
      padding: 16px 16px 0 16px;
      margin-bottom: 16px;
      -webkit-box-shadow: 2px 2px 10px #888;
      border-radius: 8px;
      background: #FFF;
      .titIcon{
        font-size:0;
        margin-bottom:30px;
        em{
          display: inline-block;
          vertical-align: middle;
          width: 2px;
          height: 14px;
          margin-right:5px;
          background: #4285F4;
        }
        span{
          display: inline-block;
          vertical-align: middle;
          font-size:14px;
        }
      }
    }
    .newuser-form{
      text-align:center;
      input{
        height:30px
      }
    }
    .list{
      span{
        display:inline-block
      }
      li{
        border: 1px solid #ebeef5;
        border-top: none
      }
    }
    button{
      width: 68px;
      height: 26px;
      padding: 0;
      font-size: 12px;
    }
  }

  
  .el-breadcrumb__inner{
      color: #4285F4!important;
    }
    .is-link{
      color: #909191!important;
    }
    .el-form--inline .el-form-item{
      width:100%
    }
    .addRole{
      width:300px;
      color:#909399;
      border:1px solid #ebeef5;
      background:#f9f9f9;
      .el-col:first-child,.el-col:last-child{
        text-align:center
      }
    }
    .addForm {
      width:300px
    }
    .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
      height: auto;
      max-height: 274px;
      overflow: hidden;
      overflow-y: auto;
      background:#FFF
    }
    .el-select-dropdown__item.selected{
      font-weight: normal;
    }
    ul li >>>.el-tree .el-tree-node__content{
      height:auto;
      padding: 0 20px;
    }
    .el-tree-node__label{
      font-weight: normal;
    }
    .el-tree >>>.is-current .el-tree-node__label{
      color: #409EFF;
      font-weight: 700;
    }
    .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
      color:#606266;
      font-weight: normal;
    }

</style>
el-tree是一个Vue.js的树形控件,可以用于显示层级结构的数据。要动态添加显示数据,可以通过以下步骤实现: 1. 在data中定义一个数组,用于存储树形控件的数据。 2. 在el-tree中使用:data属性绑定该数组。 3. 在methods中定义一个函数,用于动态添加数据。该函数可以通过push方法向数组中添加新的节点数据。 4. 在el-tree中使用node-key属性指定节点的唯一标识符。 5. 在el-tree中使用:default-expanded-keys属性指定默认展开的节点。 6. 在el-tree中使用@node-click属性绑定一个函数,用于处理节点的点击事件。 具体实现可以参考以下代码: ``` <template> <el-tree :data="treeData" :props="defaultProps" :node-key="nodeKey" :default-expanded-keys="[0]" @node-click="handleNodeClick" ></el-tree> </template> <script> export default { data() { return { treeData: [ { id: 1, label: '节点1', children: [] } ], nodeKey: 'id', defaultProps: { children: 'children', label: 'label' } } }, methods: { addNode() { const newNode = { id: this.treeData.length + 1, label: `节点${this.treeData.length + 1}`, children: [] } this.treeData[0].children.push(newNode) }, handleNodeClick(data) { console.log(data) } } } </script> ``` 在上面的代码中,我们定义了一个treeData数组,用于存储树形控件的数据。在addNode方法中,我们通过push方法向treeData数组中添加新的节点数据。在el-tree中,我们使用:data属性绑定treeData数组,使用node-key属性指定节点的唯一标识符,使用:default-expanded-keys属性指定默认展开的节点,使用@node-click属性绑定一个函数,用于处理节点的点击事件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值