首页 > 其他分享 >koacms(十三)element ui 树形下拉选择封装

koacms(十三)element ui 树形下拉选择封装

时间:2024-10-13 22:17:29浏览次数:3  
标签:node tree value element koacms id ui data children

直接上代码,这个用的地方也会比较多

<!-- 
 *  用法
    defaultValue: 默认值
    treeData: 树形数据
    disabled: 是否禁用
    defaultProps: 配置选项
    @clickSelectTree: 点击树形节点时触发
    @clearSelectInput: 清空输入框时触发
    
    <SelectTree v-model="editFormFl.faterClassName" :defaultValue="editFormFl.faterClassCode" :treeData="treeData" :disabled="isEditFl" :defaultProps="defaultProps" @clickSelectTree="handleSelectValueChange" style="width: 100%;"></SelectTree>
-->
<template>
    <div style="display: inline-block;" class="w-100">
      <el-select class="main-select-tree d-flex" ref="selectRef" v-model="value" style="width: 100%" clearable @clear="clearSelectInput" :disabled="disabled">
        <div class="my-10 px-20">
          <el-input class="box-border flex-1" placeholder="输入关键字进行过滤" v-model="filterText" clearable ></el-input>
        </div>
        
        <el-option v-for="item in formatData(treeData)" :key="item.value" :label="item.label" :value="item.value" style="display: none" />
        <el-tree class="my_select_Tree  box-border " ref="treeRef" :data="treeData" node-key="id" highlight-current :props="defaultProps" @node-click="handleNodeClick" :current-node-key="value" :expand-on-click-node="true" default-expand-all :filter-node-method="filterNode" />
      </el-select>
    </div>
  </template>
   
  <script>
  export default {
    name: "TreeSelect",
    props: {
      selectValue: {
        type: [String,Number],
        default: "",
      },
      treeData: {
        type: Array,
        default: () => ([])
      },
      // 配置选项
      defaultProps: {
        type: Object,
        default: () => ({ // 属性值为后端返回的对应的字段名
          children: 'children',
          label: 'className',
          value: 'id'
        })
      },
      //是否禁用
      disabled: {
        type: Boolean,
        default: false
      },
      //默认选中值
      defaultValue: {
        type: String,
        default: "",
      },
    },
    data() {
      return {
        filterText: "",
        value: "",
      };
    },
    watch: {
      filterText(val) {
        this.$refs.treeRef.filter(val);
      },
    },
    mounted() {
      this.initialization()//初始化赋值
    },
    methods: {
      // 初始化赋值
      initialization(){
        var that = this
        this.findNodeById(this.treeData, this.defaultValue, function(node) {
          that.value = node.className;
          console.log('Found node name:', that.value); // 立即查看结果
        });
      },
      //递归方法,获取树形结构中指定id的节点
      findNodeById(tree, id, callback) {  
        for (let node of tree) {  
          if (node.id === id) {  
            // 找到节点,调用回调函数  
            callback(node);  
            return; // 如果只需要找到第一个匹配的节点,可以返回  
          }  
          if (node.children) {  
            // 如果节点有子节点,递归查找  
            this.findNodeById(node.children, id, callback);  
          }  
        }  
      },
      //筛选方法
      filterNode(value, data) {
        if (!value) return true;
        return data.className.indexOf(value) !== -1;
      },
      // 递归遍历数据
      formatData(data) {
        let options = [];
        const formatDataRecursive = (data) => {
          data.forEach((item) => {
            options.push({ label: item.className, value: item.id });
            if (item.children && item.children.length > 0) {
              formatDataRecursive(item.children);
            }
          });
        };
        formatDataRecursive(data);
        return options;
      },
      // 点击事件
      handleNodeClick(node) {
        this.value = node.className;
        this.$refs.selectRef.blur();
        this.$emit("clickSelectTree", node);
      },
      // 清空事件
      clearSelectInput() {
        this.$emit("clickSelectTree", "");
        // 获取 el-tree 实例的引用
        const elTree = this.$refs.treeRef;
        // 将当前选中的节点设置为 null
        elTree.setCurrentKey(null);
      },
    },
  };
  </script>
   
  <style  scoped>
  .my_select_Tree .el-tree-node .is-current > .el-tree-node__content {
    font-weight: bold;
    color: #409eff;
  }
  .my_select_Tree .el-tree-node.is-current > .el-tree-node__content {
    font-weight: bold;
    color: #409eff;
  }
  </style>

标签:node,tree,value,element,koacms,id,ui,data,children
From: https://blog.csdn.net/weixin_52380389/article/details/142905726

相关文章

  • Burp Suite为何能抓到HTTPS的明文流量,Wireshark可以吗,公司电脑的加密流量也是被监控了
    在前期博文《万字图文详解HTTPS协议通信过程,结合抓包实战解析带你一次看透HTTPS!》中,我们知悉HTTPS通信内容是用会话密钥加密的,但不少细心的读者存在疑问:为何对于使用HTTPS协议的站点,在BurpSuite中拦截到的数据包却是“明文传输”的?如下图所示,这又是什么原理呢?那公司电脑的......
  • Guitar Pro怎么制作伴奏谱,吉他谱制作软件guitar pro教程
    在诸多教学吉他谱制作软件中GuitarPro是一款非常优秀的软件,它是专为吉他和其他弦乐器设计,且能提供乐谱编辑、音轨录制和播放、和弦与音阶库等功能的强大软件。GuitarPro不仅具有强大的乐谱编辑功能,其用户界面也易于上手,更支持简谱编辑。在支持音轨录制和播放的同时,也提供了高......
  • element 穿梭框el-transfer 实现上下移动选中的数据顺序
    代码实现<template><div><el-buttontype="primary"size="default"@click="upDown('up')">up</el-button><el-buttontype="primary"size="default"@click="upDo......
  • 时隔半年 DotNetGuide 已突破了 6.6K + Star,持续更新,欢迎更多小伙伴PR投稿!
    前言记得今年5月份的时候DotNetGuideGitHub才突破5kStar,经过持续不断地输出时隔半年DotNetGuide已突破了6.6K+Star!并且由我创建的DotNetGuide技术社区微信交流群人数也突破了3200+,非常开心和自豪能够帮助到这么多对C#/.NET感兴趣的小伙伴。之后还是会持续更新,努力输出更......
  • Burp Suite的标签了解
    burpsuite是一款集成了多种功能的Web应用渗透测试工具,可以帮助测试人员对Web应用进行拦截、分析、修改、重放、扫描、爆破、模糊测试等操作,从而发现和利用Web应用种的漏洞基础用法Dashboard标签:显示burpsuite的仪表盘,可以通过它进行漏洞扫描,但是该功能仅限付费版使用左......
  • Java项目:高校心理辅导系统(java+SpringBoot+Vue+elementui+mysql)
    源码获取:俺的博客首页"资源"里下载! 项目介绍基于Springboot+vue高校心理教育辅导设计与实现本系统分为前后台,包含管理员、学生、教师三种角色,前台为学生、教师登录,后台为管理员、学生、教师分别登录。前台主要功能:首页、心理健康学习、试卷列表、公告通知、留言反馈、......
  • Java项目:母婴商城系统(java+SpringBoot+Mybaits+Vue+elementui+mysql)
    源码获取:俺的博客首页"资源"里下载! 项目介绍基于Springboot+vue的母婴商城系统本系统分为前后台,包含管理员、用户两种角色,前台为普通用户登录,后台为管理员、用户分别登录。前台主要功能:首页、商品信息、商品资讯、用户登录、用户注册、用户个人中心、我的订单、我的地......
  • Java项目:房产销售系统(java+SpringBoot+Mybaits+Vue+elementui+mysql)
    源码获取:俺的博客首页"资源"里下载! 项目介绍基于Springboot+vue的房产销售系统本系统分为前后台,包含管理员、用户、销售经理三种角色,前台为普通用户登录,后台为管理员、用户、销售经理分别登录。前台主要功能:首页、房源信息、用户登录、用户注册、用户个人中心、我的收......
  • 前端学习第四天笔记 函数 对象 math对象 Date对象 DOM概述 document对象的获取元素、
    文章目录函数函数的声明函数名的提升对象math对象Math.abs()Math.max()和Math.min()Math.floor()和Math.ceil()Math.random()Date对象Date.now()Date对象中的Get方法DOM概述节点节点树Node.nodeType属性document对象_方法/获取元素document.getElementsByTagName()do......
  • 第108天:免杀对抗-Python&混淆算法&反序列化&打包生成器&Py2exe&Nuitka
    知识点#知识点:1、Python-对执行代码做文章2、Python-对shellcode做文章3、Python-对代码打包器做文章#章节点:编译代码面-ShellCode-混淆编译代码面-编辑执行器-编写编译代码面-分离加载器-编写程序文件面-特征码定位-修改程序文件面-加壳花指令-资源代码加载面-Dll反......