当前位置: 首页 > news >正文

Vue树选择

Element ui树选择

TreeSelect.vue

<template><div class="tree-select"><el-popoverplacement="bottom-start"width="300"trigger="click"v-model="visible"popper-class="tree-select-popper"><!-- 搜索框 --><el-inputv-model="filterText"placeholder="搜索..."size="small"clearablestyle="margin-bottom: 8px;"/><!-- 树形结构 --><el-treeref="tree":data="data":props="defaultProps"node-key="id":show-checkbox="multiple"highlight-current:filter-node-method="filterNode"@node-click="handleNodeClick"@check="handleCheck"default-expand-all/><!-- 输入框触发器 --><el-inputslot="reference"v-model="selectedLabels":placeholder="placeholder"readonlysuffix-icon="el-icon-caret-bottom"/></el-popover></div>
</template><script>
export default {name: "TreeSelect",props: {value: [String, Number, Array], // 绑定值data: { type: Array, required: true }, // 树数据multiple: { type: Boolean, default: false }, // 是否多选placeholder: { type: String, default: "请选择" }},data() {return {visible: false,filterText: "",selectedLabels: "",defaultProps: { children: "children", label: "label" }};},watch: {filterText(val) {this.$refs.tree.filter(val);},value: {immediate: true,handler(val) {if (!val) {this.selectedLabels = "";return;}if (this.multiple && Array.isArray(val)) {const nodes = this.$refs.tree? this.$refs.tree.getCheckedNodes(): [];this.selectedLabels = this.formatGroupedLabels(nodes);} else {const node = this.findNodeById(this.data, val);this.selectedLabels = node ? node.label : "";}}}},methods: {filterNode(value, data) {if (!value) return true;return data.label.indexOf(value) !== -1;},handleNodeClick(node) {if (!this.multiple) {this.selectedLabels = node.label;this.$emit("input", node.id);this.visible = false; // 单选时选完收起}},handleCheck() {if (this.multiple) {const nodes = this.$refs.tree.getCheckedNodes();this.selectedLabels = this.formatGroupedLabels(nodes);this.$emit("input", nodes.map(n => n.id));}},// 格式化多选显示:按顶级父节点分组formatGroupedLabels(nodes) {const grouped = {};nodes.forEach(node => {const topParent = this.findTopParent(this.data, node.id);if (topParent) {// 跳过顶级父节点本身if (node.id === topParent.id) return;if (!grouped[topParent.label]) {grouped[topParent.label] = [];}grouped[topParent.label].push(node.label);}});return Object.keys(grouped).map(parent => `${parent}: ${grouped[parent].join(", ")}`).join("; ");},// 找到某个节点的顶级父节点findTopParent(list, id, parent = null) {for (let item of list) {if (item.id === id) return parent || item;if (item.children) {const found = this.findTopParent(item.children, id, parent || item);if (found) return found;}}return null;},findNodeById(list, id) {for (let item of list) {if (item.id === id) return item;if (item.children) {const found = this.findNodeById(item.children, id);if (found) return found;}}return null;}}
};
</script><style scoped>
.tree-select {width: 300px;
}
.tree-select-popper {max-height: 300px;overflow: auto;
}
</style>

使用示例

<template><div><h3>单选</h3><tree-select v-model="singleValue" :data="treeData" /><h3 style="margin-top:20px;">多选</h3><tree-select v-model="multiValue" :data="treeData" multiple /></div>
</template><script>
import TreeSelect from "./TreeSelect.vue";export default {components: { TreeSelect },data() {return {singleValue: null,multiValue: [],treeData: [{id: 1,label: "水果",children: [{id: 11,label: "苹果",children: [{ id: 111, label: "红富士" },{ id: 112, label: "奶苹果" }]},{ id: 12, label: "香蕉" }]},{id: 2,label: "蔬菜",children: [{ id: 21, label: "西红柿" },{ id: 22, label: "黄瓜" }]}]};}
};
</script>

效果

http://www.dtcms.com/a/395822.html

相关文章:

  • opencv人脸识别
  • 怿星科技桂林子公司乔迁新址,于山水画中开启研发新篇章
  • 创建者模式:工厂方法模式
  • 【 C/C++ 算法】入门动态规划-----路径问题(以练代学式)
  • 三.上网行为安全
  • k个一组翻转链表
  • Super分区和动态分区
  • 2026华清远见新品发布会:聚焦人工智能嵌入式物联网,打造“虚实融合•软硬协同“智能化教育新生态!
  • 09 - spring security加载流程
  • 【大前端】Android:读取剪切板与禁用剪切板复制功能(完整指南)
  • 第18讲 机器学习与深度学习
  • 数据结构 05(线性:栈和队列)
  • RAG系统嵌入模型怎么选?选型策略和踩坑指南
  • 机器学习 vs. 动力学模型,Ai2 最新研究:仅需 2 分钟,ACE2 可完成一次 4 个月季节预报
  • RNN循环神经网络详解
  • 【Nginx开荒攻略】深入解析Nginx进程管理与信号控制:从原理到实战
  • MySQL与Redis面试问题详解
  • 鸿蒙Next IPC Kit详解:构建高效进程间通信的完整指南
  • 【开题答辩全过程】以 基于springboot的高校疫情防控系统为例,包含答辩的问题和答案
  • Centos7 命令行使用nmcli重置网络配置
  • 如何计算sequence粒度的负载均衡损失
  • 学财税大数据应用,需要考CPA/税务师吗?
  • RAG全栈技术——文档加载器
  • 如何理解Service Mesh(服务网格)
  • android9适配camera gc02m1
  • 【十天成长计划】BoostKit初级班 开班啦!——陪伴式学习,阶梯式成长
  • 【图文详解】强化学习核心框架、数学基础、分类、应用场景
  • Rust简介
  • golang和rust内存分配策略
  • 简历项目之无人机图像目标识别