vue2+element-ui模拟vue3+element-plus的el-tree-select组件,使用el-tree、el-select实现treeSelect组件功能

提示:vue2+element-ui模拟vue3+element-plus的el-tree-select组件

前言

‌‌‌‌‌element-ui没有el-tree-select组件,需要想要一个vue2版本tree-select

一、教程

element-ui文档

二、tree-select组件

<template>
    <div class="tree_select_box T_S_tree_select_box">
        <el-select
          class="T_S_tree_select"
          v-model="selectIds"
          placeholder="请选择"
          no-data-text="暂无内容"
          filterable
          :multiple="type=='multiple'"
          :collapseTags="type=='multiple'"
          :clearable="true"
          @clear="clearNode"
          @change="changeSelect">
          <el-option label="" style="height: auto; padding: 0" value="" >
            <el-tree
              ref="treeSelectRef"
              node-key="id"
              empty-text="暂无内容"
              :data="treeList"
              :default-expand-all="false"
              :multiple="type=='multiple'"
              :showCheckbox="type=='multiple'"
              :expand-on-click-node="type=='multiple'"
              :check-on-click-node="type=='radio'"
              :check-strictly="type=='radio'"
              @check="treeSelect"
              @node-click="clickTreeNode"
            />
          </el-option>
          <el-option v-for="item, index in selectData" :label="item.label" :value="item.id" :key="index" style="display: none;"></el-option>
        </el-select>
    </div>
  </template>
  
  <script>
  export default {
    name:"TreeSelect",
    props:{
      type:{
        type:String,
        default:'multiple'
      },
    },
    data(){
      return{
        selectIds:[],
        selectData:[],
        treeList:[],
      }
    },
    created(){
      this.getTreeList();
    },
    mounted(){
    },
    methods: {
      // 清空
      clearNode(){
        this.$refs.treeSelectRef.setCheckedKeys([])
        if(this.type == 'multiple'){
          this.selectIds = [];
          this.selectData = [];
          this.$emit('changeSelect',[]);
        }else{
          this.$emit('clearNode');
        }
      },
      // 单选点击树结构子节点
      clickTreeNode(data){
        this.selectIds = data.id;
        this.selectData = [data];
        this.$emit('clickTreeNode',data);
      },
      // 删除tag时触发
      changeSelect(){
        if(this.type == 'radio')return
        this.selectData.forEach((node,index)=>{if(this.selectIds.indexOf(node.id)<0){return this.selectData.slice(index,1)}});
        this.$refs.treeSelectRef.setCheckedKeys(this.selectIds);
        this.$emit('changeSelect',this.selectData);
      },
      // 切换节点复选框选中状态
      treeSelect(){
        if(this.type == 'radio')return
        let nodes = this.$refs.treeSelectRef.getCheckedNodes();
        this.selectIds = nodes.map(node=>node.id);
        this.selectData = nodes.map(node=>node);
        this.$emit('changeSelect',this.selectData);
      },
      // 获取数据
      getTreeList(type){
        this.$nextTick(()=>{
            this.treeList = [
                {
                    label:'a',
                    id:'1',
                    name:'a',
                    children:[
                        {
                            label:'a1',
                            id:'11',
                            name:'a1',
                            children:[
                                {
                                    label:'a11',
                                    id:'111',
                                    name:'a11',
                                },
                                {
                                    label:'a12',
                                    id:'112',
                                    name:'a12',
                                },
                            ],
                        },
                        {
                            label:'a2',
                            id:'12',
                            name:'a2',
                            children:[
                                {
                                    label:'a21',
                                    id:'121',
                                    name:'a21',
                                },
                                {
                                    label:'a22',
                                    id:'122',
                                    name:'a22',
                                }

                            ],
                        }
                    ],
                },
                {
                    label:'b',
                    id:'2',
                    name:'b',
                    children:[
                        {
                            label:'b1',
                            id:'21',
                            name:'b1',
                            children:[
                                {
                                    label:'b11',
                                    id:'211',
                                    name:'b11',
                                },
                                {
                                    label:'b12',
                                    id:'212',
                                    name:'b12',
                                },
                            ],
                        },
                        {
                            label:'b2',
                            id:'22',
                            name:'b2',
                            children:[
                                {
                                    label:'b21',
                                    id:'221',
                                    name:'b21',
                                },
                                {
                                    label:'b22',
                                    id:'222',
                                    name:'b22',
                                }

                            ],
                        }
                    ],
                },
                {
                    label:'c',
                    id:'3',
                    name:'c',
                    children:[
                        {
                            label:'c1',
                            id:'31',
                            name:'c1',
                            children:[
                                {
                                    label:'c11',
                                    id:'311',
                                    name:'c11',
                                },
                                {
                                    label:'c12',
                                    id:'312',
                                    name:'c12',
                                },
                            ],
                        },
                        {
                            label:'c2',
                            id:'32',
                            name:'c2',
                            children:[
                                {
                                    label:'c21',
                                    id:'321',
                                    name:'c21',
                                },
                                {
                                    label:'c22',
                                    id:'322',
                                    name:'c22',
                                }

                            ],
                        }
                    ],
                }
            ];
        });
      },
    }
  }
  </script>
  
  <style lang="less">
  .T_S_tree_select_box {
    --theme-color: #1082f5;
    .T_S_tree_select {
      width: 260px;
      line-height: 22px !important;
      position: relative;
      box-sizing: border-box;
      .el-input__inner{
        border-color: #dcdfe6!important;
        height: 32px!important;
        line-height: 32px;
      }
      .el-input.is-focus .el-input__inner{
        border-color: var(--theme-color);
      }
      .el-select-tree__tags-wrapper {
        max-width: 180px !important;
        min-width: 0 !important;
        width: auto !important;
        overflow: hidden;
      }
      .el-tree-select__tags-text {
        max-width: 80px !important;
      }
      .el-select-dropdown{
        min-width: 400px!important;
      }
      .el-tree-select__input {
        display: inline-block;
        width: 20px !important;
      }
      .el-input__suffix-inner .el-input__icon {
        line-height: 32px !important;
      }
      .el-input__inner {
        border-radius: 8px !important;
      }
    }
  }
  
  </style>

三、引用

<template>
    <div class="home_box">
      <span>多选</span>
      <TreeSelect @clearNode="clearNode" @changeSelect="changeSelect" @treeSelect="treeSelect"></TreeSelect>
      <span>单选</span>
      <TreeSelect :type="'radio'" @clearNode="clearNode" @clickTreeNode="clickTreeNode"></TreeSelect>
    </div>
</template>
  
<script>
  import TreeSelect from './TreeSelect'
  
  export default {
    name: 'Hmoe',
    components:{TreeSelect},
    data () {
      return {}
    },
    methods: {
      // 清空select
      clearNode(){
        console.log('清空select');
      },
      // 删除tag时触发--多选
      changeSelect(data){
        console.log('删除tag时触发',data);
      },
      // 切换节点复选框选中状态--多选
      treeSelect(data){
        console.log('切换节点复选框选中状态',data);
      },
      // 单选点击树结构子节点--单选
      clickTreeNode(data){
        console.log('单选点击树结构子节点',data);
      },
    }
  }
  </script>
  <style scoped></style>
  

四、效果

多选

在这里插入图片描述

单选

在这里插入图片描述

总结

踩坑路漫漫长@~@

element-plus 是一个基于 Vue.jsUI 组件库,el-tree-select 是其中的一个组件,用于实现树形结构的下拉选择。 el-tree-select 组件是在 el-select 组件的基础上进行扩展,使其支持树形结构的数据。使用 el-tree-select,我们可以在下拉列表中展示树状数据,并支持选择树节点的功能el-tree-select使用方式大致分为以下几步: 1. 引入 element-plus 库,确保已安装并正确引入相关 CSS 和 JS 文件。 2.组件使用 el-tree-select 标签,通过 v-model 绑定选中的节点,将选中的节点值赋给 data 中的一个变量。 3. 设置 el-tree-select 的配置项,包括数据源、显示字段、样式等。 4. 响应选择事件,在 el-tree-select 标签上绑定 change 事件,根据选中的节点进行相应操作。 配置项中常用的属性有: - data:树形数据源,可以是一个数组或者通过异步加载数据。 - label-prop:用于显示节点文本的属性名。 - value-prop:用于取值的属性名。 - default-expand-all:是否默认展开全部节点。 - filterable:是否支持输入框搜索。 el-tree-select 还提供了其他的配置项和方法,可以根据具体需求进行调整和使用。 总之,element-plusel-tree-select 组件提供了一种简单易用的方式来展示和选择树形数据,在Vue.js项目中非常实用。通过合理配置,我们可以灵活定制树形下拉选择框的功能和样式,使其符合项目的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值