10人参与 • 2025-04-24 • Vue.js
<template> <div class="selection"> <el-select placeholder="请选择" v-model="namelist" clearable @clear="handleclear" ref="selectupresid" style="width: 100%"> <el-option hidden :key="1" :value="1"></el-option ><!--这个必不可少否则显示不出来下拉数据--> <!-- check-strictly :父子是否联动,根据业务修改 --> <el-tree :data="options" node-key="id" :props="defaultprops" :default-checked-keys="huixianarr" @check="handlenodeclick" show-checkbox ref="treeref" :check-strictly="true" > </el-tree> </el-select> </div> </template> <script setup name="selects"> import { ref, reactive } from "vue"; //接受父组件传来的数据 const props = defineprops({ treefilterdata: { type: array, default: () => [] //树形控件数据源 }, treehxlist: { type: array, default: () => [] //回显id集合,根据业务修改 }, dfprops: { type: object, default: () => {} //树形控件配置项,根据业务修改 } }); const treeref = ref(); let namelist = ref(""); let huixianarr = ref([]); let idlist = ref(); let options = ref([]); let defaultprops = ref({}); defaultprops.value = props.dfprops; let hxlist = ref([]); let treeform = ref(); let list = ref(); var propertyname = props.dfprops.label; init(); function init() { options.value = props.treefilterdata; huixianarr.value = props.treehxlist; let hxlist = findpathsbyids(options.value, huixianarr.value); namelist.value = hxlist.join(","); //显示内容 } const emit = defineemits(["checkedid"]); function handlenodeclick(data, lst) { let arr = [], name = []; lst.checkednodes.foreach(item => { //过滤拿到选中的id arr.push(item.id); }); lst.checkednodes.foreach(item => { //过滤拿到选中的name name.push(item[propertyname]); }); namelist.value = name.join(","); //显示内容 idlist.value = arr; //后台传参需要的id //传给父组件 emit("checkedid", idlist.value); } function handleclear() { hxlist.value = []; idlist.value = []; //id集合 namelist.value = ""; //input显示内容 huixianarr.value = []; //回显id集合 treeref.value.setcheckedkeys([]); //清空 } function findpathsbyids(data, targetids) { const resultpaths = []; // 存储匹配的 title // 辅助函数:递归查找单个 id 的 title function findpathrecursive(items, targetid) { for (const item of items) { // 如果当前项的 id 匹配,添加其 title 到结果数组 if (item.id === targetid) { resultpaths.push(item[propertyname]); return; // 找到后直接返回 } // 如果有 children,递归查找 if (item.children && item.children.length > 0) { findpathrecursive(item.children, targetid); } } } // 遍历目标 id 数组,逐一查找 for (const id of targetids) { findpathrecursive(data, id); } return resultpaths; } </script> <style scoped> .selection { width: 300px; } </style>
<selectoption :treefilterdata="treefilterdata" :treehxlist="treehxlist" :dfprops="dfprops" @checkedid="gettreelist" /> //回显 const treefilterdata = ref([1]); //格式 let dfprops = ref({ children: "children", label: "title" }); //数据 const treefilterdata = ref([ { "id": 1, "path": "/home/index", "name": "home", "component": "/home/index", "title": "首页", "meta": { "icon": "homefilled", "title": "首页", "islink": "", "ishide": false, "isfull": false, "isaffix": true, "iskeepalive": true } }, { "id": 6, "path": "/system", "name": "system", "redirect": "/system/accountmanage", "title": "系统管理", "meta": { "icon": "tools", "title": "系统管理", "islink": "", "ishide": false, "isfull": false, "isaffix": false, "iskeepalive": true }, "children": [ { "id": 61, "father": 6, "path": "/system/accountmanage", "name": "accountmanage", "component": "/system/accountmanage/index", "title": "账号管理", "meta": { "icon": "menu", "title": "账号管理", "islink": "", "ishide": false, "isfull": false, "isaffix": false, "iskeepalive": true } }, ] } ]);
到此这篇关于vue3+elementplus中 树形控件封装的实现的文章就介绍到这了,更多相关vue3 elementplus树形控件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论