Vue-easy-tree封装及使用

本文详细介绍了如何在Vue项目中安装和使用vue-easy-tree组件,包括两种引入方式,以及如何封装并配置该组件以实现多选功能和动态加载。还展示了在具体页面中的应用和数据处理方法。

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

1.使用及安装

下载依赖
npm install @wchbrad/vue-easy-tree

引入俩种方案
1.在main.js中引入
import VueEasyTree from "@wchbrad/vue-easy-tree";
import "@wchbrad/vue-easy-tree/src/assets/index.scss"
Vue.use(VueEasyTree)

2.当前页面引入
import VueEasyTree from "@wchbrad/vue-easy-tree";
import "@wchbrad/vue-easy-tree/src/assets/index.scss"

2.封装vue-easy-tree

<template>
    <div class='select-tree-container' ref="selectMutiple">
         <el-input
            autocomplete="off"
            :placeholder="loading?'正在加载数据···':placeholder"
            :readonly="true"
            :disabled="loading"
            :value="checkedTreeData"
            >
            <i v-show="loading" slot="suffix" class="el-input__icon el-icon-loading"></i>
          </el-input>
            <transition name="sub-comments">
              <div

                class="scroll-container"
                v-show="selectTreeFlag">
                <el-input
                  placeholder="输入关键字进行过滤"
                  v-model="filterText"
                 >
                </el-input>
                <VueEasyTree
                    :data="treeData"
                    show-checkbox
                    height="500px"
                    :node-key="treeProps.value"
                    :props="treeProps"
                    :default-checked-keys="newArr"
                    ref="selectMutipleTree"
                    @check="handleCheck"
                    :filter-node-method="filterNode"
                    :check-strictly="checkStrictly">
                </VueEasyTree>
                </div>
            </transition>
    </div>
</template>

<script>
import VueEasyTree from "@wchbrad/vue-easy-tree";
// 样式文件,可以根据需要自定义样式或主题
import "@wchbrad/vue-easy-tree/src/assets/index.scss"
import {debounce} from '@/shared/debounceAndtThrottle.js'
export default {
  name: 'SelectMutipleTree',
  components: {
    VueEasyTree
  },
  props: {
    treeData: {
      type: Array,
      required: true,
      default:[],
    },
    treeProps: {
      type: Object,
      default: function () {
        return {
          value: 'id',
          label: 'orgName',
          checkStrictly: true
        }
      }
    },
    checkedTreeData: {
      type: String,
      required: true
    },
    placeholder: {
      type: String,
      default: '请输入'
    },
    checkStrictly: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: true
    },
    defaultCheckedKeys:{
      type: Array,
      default:() => {
        return []
      },
    }
  },
  data () {
    return {
      selectTreeFlag: false,
      filterText: '',
      filterText_:null,
      newArr:[]
    }
  },
  watch: {
    checkedTreeData: function (newValue) {
      if (!newValue) {
        this.$nextTick(() => {
          this.$refs.selectMutipleTree.setCheckedKeys([])
        })
      }
    },
    filterText(val) {
      this.filterText_(val)
    },
    treeData(val){
      var num = 0
      this.chuli(val,num)
    },
    defaultCheckedKeys(val){
      if(val.length == 0) {
        this.newArr =[]
      }else{
        this.chuliCheckedKeys(this.treeData, val)
      }
    }
  },
  methods: {
    debounce (callback, delay = 500) {
      var t = null;
      return function (...args) {
        clearTimeout(t);
        t = setTimeout(() => callback(...args),delay);
      }
    },
    chuliCheckedKeys(list, val){
      list.forEach(item => {
        val.forEach(i => {
          if(item[this.treeProps.value].split('-')[0] === i){
            i= item[this.treeProps.value]
            this.newArr.push(i)
          }
        })
        if(item[this.treeProps.children]){
          this.chuliCheckedKeys(item[this.treeProps.children], val)
        }
      })
    },
    chuli(list,num){
       list.forEach(i=>{
        num++
        i[this.treeProps.value] = i[this.treeProps.value] + "-" + num;
        if(i[this.treeProps.children]){
          this.chuli(i[this.treeProps.children],num)
        }
       })
    },
    handleCheck (checkedNodes, checkedKeys) {
      checkedKeys.checkedNodes.forEach((f) => {
         f[this.treeProps.value] =  f[this.treeProps.value].split('-')[0]
        });
      this.$emit('handleCheckData', checkedKeys.checkedNodes)
    },



    filterNode(value, data) {
      if (!value) return true;
      return data[this.treeProps.label].includes(value);

    }
  },
  created () {

  },
  mounted () {
    if (!this.checkedTreeData) {
      this.$nextTick(() => {
        this.$refs.selectMutipleTree.setCheckedKeys([])
      })
    }
    this.$refs.selectMutiple && this.$refs.selectMutiple.addEventListener('click', (event) => {
      const ev = event || window.event
      if (ev.stopPropagation) {
        ev.stopPropagation()
      } else {
        ev.cancelable = true
      }
       this.selectTreeFlag =  !this.loading && true
    })
    this.$root.$el.addEventListener('click', (event) => {
      this.selectTreeFlag = false
    })
    this.filterText_ = debounce((val)=>this.$refs.selectMutipleTree.filter(val), 1000)
  },
  destroyed () {

  }
}
</script>
<style lang="scss" scoped>
.scroll-container {
  max-height: 600px;
  overflow-y: auto;
  position: absolute;
  z-index: 10;
  width: 100%;
  border: 1px solid #eeeeee;
  border-top: none;
  background: #ffffff;
  ::v-deep {
    .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
.sub-comments-leave-active,.sub-comments-enter-active {
    transition: max-height 0.1s linear;
}
.sub-comments-enter,.sub-comments-leave-to {
    max-height:0 ;
    opacity: 0;
}
.sub-comments-enter-to,.sub-comments-leave {
    max-height: 136px ;
}
</style>

3.具体页面该如何使用

<select-mutiple-tree  size="mini" style="display: inline-block" :treeData="organizationTree" :treeProps="releaseTreeProps" :loading="treeLoading" :checkedTreeData="addCaseForm.copyToUserListName" 
placeholder="请选择"  @handleCheckData="handleCheck" :checkStrictly="releaseTreeProps.checkStrictly">
</select-mutiple-tree>

1.data中定义
    organizationTree: [],
    treeLoading: true, //控制人员树加载状态
    releaseTreeProps: {
      value: "nodeIdAndType",
      label: "nodeName",
      children: "organizationTreeUserSingeNodeList",
      checkStrictly: false,
    },
    addCaseForm: {
      copyToUserListName:'',
      copyToUserList:[], //抄送人
    }
2.引入及注册
   import SelectMutipleTree from "@/components/selectMutipleTree";
   components:{SelectMutipleTree }
3.接口中写
    接口名字().then((response) => {
      const { data, success } = response;
      if (success) {
        this.organizationTree = data;
        this.treeLoading = false;
      } else {
        this.organizationTree = [];
        this.treeLoading = false;
      }
    }).catch((error) => {
        this.organizationTree = [];
        this.treeLoading = false;
    });
4.方法
    handleCheck(checkedData) {
      if (checkedData.length > 0) {
        // 集合
        var namesArr = [];
        var idsArr = [];
        var userName = [];
        checkedData.forEach((f) => {
          if (f.nodeType !== 0) {
            namesArr.push(f.nodeName);
            idsArr.push(f.nodeId)
            userName.push({
              id: f.nodeId,
              name: f.nodeName,
            });
          }
        });
        this.addCaseForm.copyToUserListName = namesArr.join(";");
        this.addCaseForm.copyToUserList = idsArr;
      } else {
        this.addCaseForm.copyToUserListName = '';
        this.addCaseForm.copyToUserList = [];
      }
    },
    

 5.具体返回的后台格式

### 使用 `vue-easy-tree` 替代 `el-tree` 在现代前端开发中,树形结构组件对于展示层次化数据非常有用。当考虑从 Element UI 的 `el-tree` 迁移到更轻量级的选择如 `vue-easy-tree` 时,主要关注点在于如何保持功能的一致性和用户体验。 #### 安装依赖 为了使用 `vue-easy-tree`,首先需要安装该库: ```bash npm install vue-easy-tree --save ``` #### 基本用法对比 ##### el-tree 示例 通常情况下,在使用 `el-tree` 组件时会这样定义模板部分[^2]: ```html <template> <div> <!-- 应用子组件 --> <el-tree :data="treeData" @node-click="handleNodeClick"></el-tree> </div> </template> <script> export default { data() { return { treeData: [ // 数据源... ] }; }, methods: { handleNodeClick(data) {} } }; </script> ``` ##### vue-easy-tree 实现 而采用 `vue-easy-tree` 可以按照如下方式重构上述逻辑: ```html <template> <div> <!-- 使用 vue-easy-tree --> <easy-tree :nodes="treeNodes" @select-node="onSelectNode"/> </div> </template> <script> import EasyTree from 'vue-easy-tree'; export default { components: {EasyTree}, data() { return { treeNodes: [ /* 节点数据 */ ], }; }, methods: { onSelectNode(node, event){ console.log('Selected node:', node); } } } </script> ``` #### 配置项映射 - **节点数据**:两者都接受一个表示树状结构的数据列表作为输入参数。需要注意的是,虽然字段名称可能不同,但是基本概念一致。 - **事件处理**:`el-tree` 中常见的点击事件 (`@node-click`) 对应于 `vue-easy-tree` 的 `@select-node` 或其他自定义事件监听器。 - **样式调整**:由于两个库的设计风格有所差异,因此还需要根据实际需求微调 CSS 样式来匹配原有的视觉效果。 通过以上步骤可以在大多数场景下顺利迁移至 `vue-easy-tree` 并保留原有功能特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值