Vue-easy-tree封装及使用

news2024/10/7 6:39:56

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: {
    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.具体返回的后台格式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1434342.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

红队渗透靶机:LORD OF THE ROOT: 1.0.1

目录 信息收集 1、arp 2、nmap 3、knock 4、nikto 目录探测 1、gobuster 2、dirsearch WEB sqlmap 爆库 爆表 爆列 爆字段 hydra爆破 ssh登录 提权 信息收集 内核提权 信息收集 1、arp ┌──(root㉿ru)-[~/kali] └─# arp-scan -l Interface: eth0, ty…

Redis(三)主从架构、Redis哨兵架构、Redis集群方案对比、Redis高可用集群搭建、Redis高可用集群之水平扩展

转自 极客时间 Redis主从架构 redis主从架构搭建&#xff0c;配置从节点步骤&#xff1a; 1、复制一份redis.conf文件2、将相关配置修改为如下值&#xff1a; port 6380 pidfile /var/run/redis_6380.pid # 把pid进程号写入pidfile配置的文件 logfile "6380.log" …

使用代理IP有风险吗?如何安全使用代理IP?

代理IP用途无处不在。它们允许您隐藏真实IP地址&#xff0c;从而实现匿名性和隐私保护。这对于保护个人信息、绕过地理受限的内容或访问特定网站都至关重要。 然而&#xff0c;正如任何技术工具一样&#xff0c;代理IP地址也伴随着潜在的风险和威胁。不法分子可能会滥用代理IP…

【计算机学院寒假社会实践】——走进社区,共建美好家园

为了加强社区基层党组织建设和改进社区工作&#xff0c;推动更多资源向社区倾斜&#xff0c;曲阜师范大学计算机学院“青年扎根基层&#xff0c;服务走进社区”实践队员饶子恒在2024年1月27日来到了山东省菏泽市郓城县唐塔社区&#xff0c;对社区卫生进行清洁工作。 图为实践队…

pdf高亮显示

现在前端pdf需求越来越多&#xff0c;比如发票的显示&#xff0c;文件的显示&#xff0c;怎么实现具体步骤百度一下吧&#xff0c;这里不做详细介绍&#xff0c;主要记录下遇到的问题 1.页面pdf已经看到了&#xff0c;但是 iframe.contentWindow.PDFViewerApplication显示unde…

Python中的while循环,知其然知其所以然

文章目录 while循环结构1.用循环打印1 ~ 100步骤解析2. 1 ~ 100的累加和3.死循环1. 用死循环的方法实现 1 ~ 100累加和 4. 单向循环(1)打印 一行十个小星星*(2)通过打印一个变量的形式,展现一行十个小星星(3)一行十个换色的星星 ★☆★☆★☆★☆★☆(4)用一个循环,打印十行十列…

JAVA中的object类

一、equals方法 1.和equals的比较 既可以判断基本类型有可以判断引用类型 如果判断基本类型&#xff0c;判断的值是否相等 int num1 10; double num2 10.0; System.out.println(num1 num2); 如果判断引用类型&#xff0c;判断的地址是否相等&#xff0c;即判断是不是同一…

【教程】一个比较良心的C++代码混淆器

这是一个比较良心的C代码混淆器&#xff0c;用于信息竞赛训练和保护代码免受抄袭。本文将介绍这个混淆器的使用方法、混淆效果和已知的一些bug。同时&#xff0c;我们也会给出一些示例来演示混淆器的具体操作。 引言 在信息竞赛训练和实际开发中&#xff0c;保护代码的安全性和…

HomeAssistant系统添加HACS插件商店与远程控制家中智能家居

文章目录 基本条件一、下载HACS源码二、添加HACS集成三、绑定米家设备 ​ 上文介绍了如何实现群晖Docker部署HomeAssistant&#xff0c;通过内网穿透在户外控制家庭中枢。本文将介绍如何安装HACS插件商店&#xff0c;将米家&#xff0c;果家设备接入 Home Assistant。 基本条件…

leetcode(滑动窗口)3.无重复字符的最长字串(C++详细题解)DAY2

文章目录 1.题目示例提示 2.解答思路3.实现代码结果 4.总结 1.题目 给定一个字符串 s &#xff0c;请你找出其中不含有重复字符的 最长子串 的长度。 示例 示例 1: 输入: s “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”&#xff0c;所以其长度为 3。 示…

基于Vue2用keydown、keyup事件实现长按键盘任意键(或组合键)3秒触发自定义事件(以F1键为例)

核心代码 <template></template> <script> export default {created() {//监听长按快捷键addEventListener("keydown", this.keydown);addEventListener("keyup", this.keyup);},destroyed(d) {//移除长按快捷键removeEventListener(&…

基于抖音小程序实现的答题小程序

看过《一站到底》后&#xff0c;不仅学到了很多新知识&#xff0c;还感受到了选手们的聪明才智和对知识的热爱。同时&#xff0c;也意识到自己的知识储备还有很大的提升空间&#xff0c;因此我会继续努力学习&#xff0c;提高自己的知识水平。 《一站到底》是一档益智答题类节…

Leetcode刷题笔记题解(C++):1863. 找出所有子集的异或总和再求和

思路如下&#xff1a;递归思路&#xff0c;依次遍历数组中的数&#xff0c;当前数要不要选择像二叉树一样去遍历如下图所示 0 0 &#xff08;选5&#xff09; 5&#xff08;不选5&#xff09; 0 1 0 1 0 6 …

[word] 怎么删除文字底纹 #职场发展#其他

怎么删除文字底纹 怎么删除文字底纹?我们在录入文字到文档的时候&#xff0c;或者是复制网上内容时&#xff0c;都会带有格式&#xff0c;有时候还会遇到删除不掉的问题。今天给大家分享小技巧&#xff0c;解决你的问题。 1、删除文字底纹 文档自带的底纹&#xff0c;删除技…

Mysql连接编译踩坑问题

复习下mysql基本命令 登录mysql mysql #没密码登录 mysql -u root -p #以用户名root 需要交互输入密码方式登录显示所有数据库 SHOW DATABASES&#xff1b; 使用某个数据库 use database_name; 查看当前数据库某一张表的表结构 DESCRIBE table_name; 解决一个mysql 用…

FPGA高端项目:解码索尼IMX327 MIPI相机转USB3.0 UVC 输出,提供FPGA开发板+2套工程源码+技术支持

目录 1、前言免责声明 2、相关方案推荐我这里已有的 MIPI 编解码方案 3、本 MIPI CSI-RX IP 介绍4、个人 FPGA高端图像处理开发板简介5、详细设计方案设计原理框图IMX327 及其配置MIPI CSI RX图像 ISP 处理图像缓存UVC 时序USB3.0输出架构FPGA逻辑设计工程源码架构SDK软件工程源…

LeetCode、746. 使用最小花费爬楼梯【简单,动态规划 线性DP】

文章目录 前言LeetCode、746. 使用最小花费爬楼梯【简单&#xff0c;动态规划 线性DP】题目与分类思路 资料获取 前言 博主介绍&#xff1a;✌目前全网粉丝2W&#xff0c;csdn博客专家、Java领域优质创作者&#xff0c;博客之星、阿里云平台优质作者、专注于Java后端技术领域。…

VPN 虚拟专用网、VPN类型

1 虚拟专用网 VPN&#xff08;Virtual Private Network&#xff09; 利用公用互联网作为本机构各专用网之间的通信载体&#xff0c;这样的专用网又称为虚拟专用网 VPN (Virtual Private Network)。 专用网&#xff1a;指这种网络是为本机构的主机用于机构内部的通信&#xff…

塑料管道市场分析:行业利润率仅为5.57%

塑料和塑料管道行业组织的“掌门人”不约而同地将我国塑料管道行业的发展定义为“机遇与挑战并存”&#xff0c;而且都将抓住机遇的关键词指向了“高质量”&#xff0c;认为我国塑料管道行业必须“面对新局&#xff0c;创新突破&#xff0c;坚定高质量发展之路” 近两年来&…

day32 买卖股票的最佳时机Ⅱ 跳跃游戏 跳跃游戏Ⅱ

题目1&#xff1a;122 买卖股票的最佳时机Ⅱ 题目链接&#xff1a;122 买卖股票的最大时机Ⅱ 题意 整数数组prices[i]表示某股票的第i天的价格&#xff0c;每天可买卖股票且最多持有1股股票&#xff0c;返回最大利润 利润拆分&#xff0c;拆分为每天的利润 每天的正利…