树形弹窗选择框/vue2/Element/弹框选择

前言

此类选择器根据vue+elementUI实现,使用vue3的可以根据此案例稍作改动即可实现,主要功能有弹出选择、搜索过滤、搜索结果高亮等,此选择器只支持单选,如需多选可在此基础进行改造。


效果图


代码实现

使用时,props-value必须要传,并且要保证其唯一性!!!

HTML

<!-- * @description: 通用树形弹窗选择框* @fileName: treeDialogSelect/index.vue * @author: tan * @date: 2024-09-10 16:31:49* @Attributes: data	展示数据	arrayvalue 展示在输入框的值props	配置选项,具体配置可以参照element ui库中el-tree的配置	objectexpand-first-level 当根节点只有一个是,是否展开,默认为是search-highlight 是否开启搜索高亮,默认开启,仅在slot-tree不传入时生效onFilter 自定义过滤规则,参数为:value 搜索框的值data 原始数据callback 回调函数 接受一个参数类型boolean  为是否展示该节点 !!!!!必传!!!!!! props-value	每个树节点用来作为唯一标识的属性,整棵树应该是唯一的 !!!!!!
!-->
<template><div class="treeDialogSelectCom"><slot :value="value" v-if="$slots.default"></slot><el-inputv-elsev-bind="$attrs":value="valueFilter(value)"suffix-icon="el-icon-arrow-down":placeholder="$attrs['placeholder'] || '请选择'":clearable="$attrs['clearable'] || true"class="treeDialogSelectInput"@focus="open"@clear="selectClear()"></el-input><el-dialog:visible.sync="visible":width="$attrs['width'] || '50vw'":append-to-body="true":close-on-click-modal="false":close-on-press-escape="false":title="$attrs['dialog-title'] || '请选择'"><div class="treeDialogBody"><el-input placeholder="输入关键字进行过滤" v-model="filterText" clearable><slot slot="prepend" name="prepend"></slot></el-input><div class="treeDialogBodyTree"><el-treev-bind="$attrs":data="data":props="props"@node-click="handleNodeClick":check-strictly="$attrs['check-strictly']":icon-class="$attrs['icon-class']":lazy="$attrs['lazy']":load="$attrs['load']":node-key="props.value":filter-node-method="filterNode":default-expanded-keys="defaultExpandedKeys"ref="myTree"><template slot-scope="{ node, data }"><slot :node="node" :data="data" name="tree"><span class="slotSpan"><i v-show="!node.disabled && filterText" class="el-icon-warning"></i><span><span v-html="searchHighlightFilter(node, data)"></span><b v-if="$attrs['show-count'] != undefined && data[props.children]">({{ data[props.children].length }})</b></span></span></slot></template></el-tree></div></div><div class="footer"><el-button type="primary" plain @click="selectClear()">清 空</el-button></div></el-dialog></div>
</template>

JS

export default {props: {value: {type: undefined,default: null,},data: {type: Array,default: () => new Array(),},props: {type: Object,default: () => {return {label: 'label',value: 'value',children: 'children',};},},},data() {return {defaultExpandedKeys: [],visible: false,filterText: '',};},created() {this.propsInit();},mounted() {setTimeout(this.initData, 10);},beforeUpdate() {this.propsInit();this.initData();},methods: {open() {this.visible = true;},initData() {let newItem = this.recurrenceQuery(this.data, this.props.value, this.value);if (newItem?.length) {if (this.props.value && newItem[0][this.props.value]) {this.defaultExpandedKeys = [newItem[0][this.props.value]];}this.$nextTick(() => {if (this.$refs.myTree?.setCurrentNode) this.$refs.myTree.setCurrentNode(newItem[0]);});} else {if (this.data.length == 1 && this.$attrs['expand-first-level'] !== false) {this.defaultExpandedKeys = [this.data[0][this.props.value]];}}this.$forceUpdate();},// 单选事件handleNodeClick(data, e) {if (this.props.disabled && e.disabled) {return false;} else {if (data[this.props.children] && data[this.props.children]?.length) {return false;}}this.$emit('input', data[this.props.value]);this.visible = false;this.$emit('change', data, e);},//   递归查找通用方法recurrenceQuery(list, key, value) {if (!list || !key || !value) return [];let queryData = [];list.map(item => {if (item[this.props.children] && item[this.props.children].length) {queryData.push(...this.recurrenceQuery(item[this.props.children], key, value));}if (item[key] == value) {queryData.push(item);}return item;});return queryData;},selectClear(flag) {if (!flag) {this.$emit('input', '');this.$emit('change', null, null);}this.$refs.myTree.setCurrentKey(null);this.remoteMethod('');},getCheckedNodes() {if (this.value !== null && this.value !== undefined && this.value !== '') {return this.$refs.myTree.getCheckedNodes();}return [];},getCurrentNode() {if (this.value !== null && this.value !== undefined && this.value !== '') {return this.$refs.myTree.getCurrentNode();}return null;},valueFilter(val) {let res = '';[res] = this.recurrenceQuery(this.data, this.props.value, val);return res?.[this.props.label] || '';},propsInit() {this.props.label = this.props.label || 'label';this.props.value = this.props.value || 'value';this.props.children = this.props.children || 'children';},remoteMethod(query) {this.$refs.myTree.filter(query);},filterNode(value, data) {if (!value) return true;let result = true;if (this.$listeners.onFilter) {this.$emit('onFilter', value, data, res => {result = res;});} else {result = data[this.props.label].indexOf(value) !== -1;}return result;},searchHighlightFilter(node, data) {let { label } = this.props;if (this.$attrs['search-highlight'] === false) return data[label];if (!this.filterText) return data[label];const regex = new RegExp(this.filterText, 'gi');let text = data[label].replace(regex, match => {return `<strong class="highlight">${match}</strong>`;});return text;},},watch: {value: {deep: true,handler(val) {if (!val) {this.selectClear(true);}},},filterText(val) {this.$refs.myTree.filter(val);},},
};

CSS

.selecTree {max-height: 50vh;overflow: auto;padding: 5px;::v-deep .el-tree-node__content {font-size: 14px;}
}
::v-deep.slotSpan {font-size: 14px;> i {color: #67c23a;margin-right: 5px;}b {font-weight: normal;font-size: 12px;color: #999;}.highlight {color: #67c23a;}
}.treeDialogBody {max-height: 60vh;display: flex;flex-direction: column;::v-deep .el-input__validateIcon {display: none;}::v-deep .treeDialogBodyTree {flex: 1;overflow: auto;padding: 12px 8px;margin: 12px 0;background-color: #f5f7fa;border-radius: 5px;.el-tree {background: transparent;.el-tree-node__content:hover {background-color: #eaeef4;}}}
}
.footer {text-align: right;
}

使用案例

<template><treeDialogSelectv-model="treeDialogSelectVal":data="treeDialogSelectData"show-count></treeDialogSelect>
</template><script>
import treeDialogSelect from '@/components/treeDialogSelect';
export default {components: { treeDialogSelect },data() {return {treeDialogSelectValue: '',treeDialogSelectData: [{id: 1,label: '一级 1',children: [{id: 4,label: '二级 1-1',children: [{id: 9,label: '三级 1-1-1',},{id: 10,label: '三级 1-1-2',},],},],},{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',},],},],};},
};
</script>

使用文档 

以下只列举主要属性与方法,更多具体的属性配置请移步element官网进行查看。

属性

属性名类型默认值是否必传说明
value / v-modelstring / number-绑定值

props

object

与element保持一致

配置选项,具体配置可以参照element ui库中el-tree的配置

expand-first-level

booleantrue

当根节点只有一个时,是否展开

search-highlight

booleantrue

是否开启搜索高亮,仅在slot-tree未传入时生效

show-countbooleanfalse若节点中存在children,则在父节点展示所属children的数量,注意但设置插槽时 show-count将失效

事件

事件名称说明回调参数

change

当选择项发生改变时触发共两个参数,依次为:当前节点的数据,当前节点的 Node 对象

onFilter

当过滤框输入的值改变时触发

共三个参数,依次为:搜索框的值,当前节点的数据,回调函数callback, 接受一个参数类型boolean,表示是否展示该节点

插槽

name说明

-

页面展示的输入框slot,如果传入默认插槽,则会不显示默认el-input,参数为 { value }

prepend

弹窗中过滤文本框的顶部插槽

tree

自定义树节点的内容,参数为 { node, data }

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

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

相关文章

NVIDIA AI Workbench 让 Windows 上的 GPU 使用更加简便

NVIDIA AI Workbench 是一款免费的、用户友好型开发环境管理器&#xff0c;可在您选择的系统&#xff08;PC、工作站、数据中心或云&#xff09;上简化数据科学、ML 和 AI 项目。在 Windows、macOS 和 Ubuntu 上&#xff0c;您可以本地开发、测试项目和构建项目原型&#xff0c…

Redis 持久化机制详解

引言 Redis 是一款基于内存的高性能键值存储系统&#xff0c;为了在数据丢失时能快速恢复&#xff0c;Redis 提供了多种持久化机制。这些持久化机制可以将内存中的数据存储到磁盘上&#xff0c;确保即使系统重启或宕机后也能恢复数据。Redis 支持两种主要的持久化方式&#xf…

【移动端】Flutter与uni-app:全方位对比分析

文章目录 一、含义1. Flutter2. uni-app 二、开发程序步骤1. Flutter2. uni-app 三、基本语言区别四、优缺点1. Flutter2. uni-app优点&#xff1a;缺点&#xff1a; 五、如何选型 一、含义 1. Flutter Flutter是由Google开发的一款跨平台移动应用开发框架&#xff0c;采用Da…

一文说清什么是数据仓库

01 数据仓库的概念 数据仓库的概念可以追溯到20世纪80年代&#xff0c;当时IBM的研究人员开发出了“商业数据仓库”。本质上&#xff0c;数据仓库试图提供一种从操作型系统到决策支持环境的数据流架构模型。 目前对数据仓库&#xff08;Data Warehouse&#xff09;的标准定义&a…

rocky8安装docker步骤

1、设置 Docker 仓库 添加 Docker 官方仓库&#xff1a; sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 2. 安装 Docker 引擎 安装 Docker 引擎、CLI 和 Containerd&#xff1a; sudo dnf install docker-ce docker-ce-cli c…

【研赛论文】数学建模2024华为杯论文word/latex模板

国赛结束&#xff0c;研究生瞩目的研赛马上就要来了&#xff0c;相信研究生同学也是在努力的准备当中&#xff0c;在这里祝愿大家能够获得一个好的名次。一举冲出重围&#xff0c;拿下国奖。在数模比赛当中&#xff0c;论文是参赛者唯一能够与评阅老师进行沟通的方式&#xff0…

工厂安灯系统在优化生产流程上的优势

工厂安灯系统通过可视化的方式&#xff0c;帮助工厂管理者和操作工人及时了解生产状态&#xff0c;快速响应问题&#xff0c;从而优化生产流程。 一、安灯系统实时监控与反馈 安灯系统的核心功能是实时监控生产线的状态。通过在生产现场设置灯光、显示屏等设备&#xff0c;工人…

VUE + NODE 历史版本安装

以node 12.20.0为例子&#xff0c;想下载哪个版本&#xff0c;后面写哪个版本 https://registry.npmmirror.com/binary.html?pathnode/v12.20.0/ 安装国内镜像7.1.0 cnpm npm install -g cnpm7.1.0 -g --registryhttps://registry.npmmirror.com 安装vue脚手架4.5.15 cnpm …

【最新顶刊综述】【多模态学习】Vision + X:A Survey on Multimodal Learning in the Light of Data

VisionX&#xff1a;基于数据的多模态学习综述 论文链接 0.论文摘要和信息 摘要 摘要——我们以多感官的方式感知世界并与世界交流&#xff0c;不同的信息源由人脑的不同部分复杂地处理和解释&#xff0c;构成一个复杂但和谐统一的感知系统。为了赋予机器真正的智能&#x…

【信创】麒麟KOS上安装使用网络抓包工具Wireshark

原文链接&#xff1a;【信创】麒麟KOS上安装使用网络抓包工具Wireshark Hello&#xff0c;大家好啊&#xff01;今天给大家带来一篇关于如何在麒麟桌面操作系统上安装和使用Wireshark的文章。Wireshark是一款强大的网络协议分析工具&#xff0c;广泛应用于网络故障排查、网络流…

OCR在线识别网站现已上线!

注意,本文只提供学习的思路,严禁违反法律以及破坏信息系统等行为,本文只提供思路 如有侵犯,请联系作者下架 由作者亲自开发的ocr识别网站哈哈,暂时汇聚了三十多种验证码模型以及算法,欢迎各路朋友去尝试,网站地址如下 http://gbj5w3.natappfree.cc/ocr 验证码类型包括但…

qt绘制时钟

代码 #include "widget.h" #include "ui_widget.h"#include <QWidget> #include <QPaintEvent> //绘图事件 #include <QDebug> //测试 #include <QPainter> //画家 #include <QPen> //笔 #include <QBrush> //画刷 …

JDK下载安装教程(国产化生产环境无联网服务器部署JDK实操)

-----------------------------生产环境实操&#xff0c;记录时间2024年09-11日----------------------------- 前言&#xff1a;一定要下载适合自己系统版本的JDK 1.32位系统就下载32位系统的JDK&#xff0c;64位系统就下载64位系统的JDK&#xff0c;否则会报错&#xff01;…

C语言小游戏--贪吃蛇实现

C语言小游戏--贪吃蛇实现 1.游戏实现背景2.Win32 API介绍2.1什么是Win32 API2.2控制台程序(Console)2.3控制台屏幕的坐标COORD2.4GetStdHandle2.4.1函数语法2.4.2函数的使用 2.5GetConsoleCursorInfo2.5.1函数语法2.5.2函数的使用 2.6CONSOLE_CURSOR_INFO2.6.1结构体结构2.6.2结…

【佳学基因检测】在织梦网站中, 创建或修改目录:/var/www/html/cp 失败! DedeTag Engine Create File False

【佳学基因检测】在织梦网站中, 创建或修改目录&#xff1a;/var/www/html/cp 失败&#xff01; DedeTag Engine Create File False 在使用 DedeCMS&#xff08;一个常用的内容管理系统&#xff09;时&#xff0c;如果遇到“创建或修改目录&#xff1a;/var/www/html/cp 失败&…

基于Spring Boot开发一个自习室预定系统

基于Spring Boot开发一个自习室预定系统是一个实用的项目&#xff0c;可以帮助学生或工作人员更有效地管理和预订自习室资源。以下是一个简化的开发指南&#xff0c;可以帮助你启动这个项目。 1. 项目初始化 使用Spring Initializr (https://start.spring.io/) 创建一个新的S…

系统架构师考试学习笔记第四篇——架构设计实践知识(18)面向服务架构设计理论与实践

本章考点&#xff1a; 第18课时主要学习面向服务架构设计理论与实践。根据考试大纲&#xff0c;本课时知识点会涉及单选题型&#xff08;约占2~5分&#xff09;和案例题&#xff08;25分&#xff09;&#xff0c;本课时内容偏重于方法的掌握和应用&#xff0c;根据以往全国计算…

[数据集][目标检测]水面垃圾检测数据集VOC+YOLO格式2027张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;2027 标注数量(xml文件个数)&#xff1a;2027 标注数量(txt文件个数)&#xff1a;2027 标注…

循环链表(判断双循环链表是否为对称,将两个单循环链表合并成一个循环链表)

一、判断带头节点的双循环链表是否为对称链表 思想&#xff1a;设置两个指针&#xff0c;一个从头开始&#xff0c;一个从后开始遍历&#xff0c;两个指针相等&#xff0c;或者其中一个指针的下一个节点为另外一个节点时结束遍历。如果数据相同&#xff0c;则往后遍历。否则不…

hpl 的测试配置文件 HPL.dat 的内容说明

1&#xff0c;HPL.dat 内容和主体结构 在编译完成后&#xff0c;bin/$(arch)/HPL.dat 内容如下&#xff1a; $ cat HPL.dat HPLinpack benchmark input file Innovative Computing Laboratory, University of Tennessee HPL.out output file name (if any) file …