vue + elementUI 实现下拉树形结构选择部门,支持多选,支持检索

news/2025/2/21 10:58:27/

vue + elementUI 实现下拉树形结构选择部门,支持多选,支持检索

<template><div><el-select v-model="multiple?choosedValue:choosedValue[0]" element-loading-background="rgba(0,0,0,0.8)":disabled="disableFlag" @visible-change="visibleChange"filterable clearable collapse-tags :filter-method="filterMethod" @clear="clear" @remove-tag="removeTag":multiple="multiple" ref="selectRef" v-loading="loading" style="width: 100%"><el-option :label="option.name" :value="option.id" v-for="option in options" :key="option.id"class="optionClass"/><template v-slot:empty><div/></template><el-tree :props="treeProps" :load="loadNode" :data="treeData" :show-checkbox="multiple" @check="handleCheck":expand-on-click-node="false" @node-click="chooseNode" :filter-node-method="filterNodeMethod"class="treeClass" ref="treeRef" :node-key="'id'" :default-checked-keys="choosedValue"/></el-select></div>
</template><script>
import {getDwxxOfTree} from "@/api/commentTable/api";export default {name: "chooseUnitTree",props: {disableFlag: {Type: Boolean,required: false,default: false},value: {Type: Object,required: true},multiple: {Type: Boolean,required: false,default: false}},data() {return {treeProps: {label: 'name',value: 'id',children: 'children'},deptMap: {},treeData: [],options: [],loading: false,choosedValue: [],choosedOptions: [],}},computed: {},watch: {// choosedValue: {//   handler(n, o) {//     if (this.$refs.treeRef) {//       this.$refs.treeRef.filter()//     }//   },//   immediate: true,//   deep: true// }},mounted() {this.choosedValue = []this.getNodeData()},methods: {visibleChange(visible) {if (!visible) {this.$refs.treeRef.filter()}},removeTag(nodeId) {this.choosedValue = this.choosedValue.filter(item => item !== nodeId)this.choosedOptions = this.choosedOptions.filter(item => item.id !== nodeId)this.$refs.treeRef.setCheckedKeys(this.choosedValue, false)this.$emit('input', this.multiple ? this.choosedValue : this.choosedValue[0])},clear() {this.choosedValue = []this.choosedOptions = []this.$refs.treeRef.setCheckedKeys([], false)this.$emit('input', '')},filterMethod(keyWord) {this.$refs.treeRef.filter(keyWord)},filterNodeMethod(keyWord, node) {if (!keyWord) {return true}return (node.name + node.id).includes(keyWord)},init() {this.choosedValue = []if (typeof this.value === 'string') {this.choosedOptions.push(this.deptMap[this.value])this.choosedValue.push(this.value)} else {this.value.forEach(item => {this.choosedOptions.push(this.deptMap[item.id])this.choosedValue = this.value})}},getNodeData(resolve) {this.loading = truegetDwxxOfTree().then(dwxxResult => {// dwxxResult.data :// [{//   id  : "123456"//   name : "xx集团"//   pid : "000000"// }]this.loading = falseif (dwxxResult.data) {this.options = dwxxResult.dataconst rootDept = []this.deptMap = {}for (let deptInfo of dwxxResult.data) {this.deptMap[deptInfo.id] = deptInfo}for (let deptInfo of dwxxResult.data) {if (!this.deptMap[deptInfo.pid]) {rootDept.push(deptInfo)}}if (resolve) {resolve(rootDept)}} else {if (resolve) {resolve([])}}this.init()this.createTree(dwxxResult.data)})},createNodeChildren(node) {let children = []for (let deptId in this.deptMap) {let tmpNode = this.deptMap[deptId]if (tmpNode.pid === node.id) {children.push(this.createNodeChildren(tmpNode))}}node.children = childrenreturn node},createTree() {this.treeData = []for (let deptId in this.deptMap) {let node = this.deptMap[deptId]if (!this.deptMap[node.pid]) {this.treeData.push(this.createNodeChildren(node))}}},loadNode(node, resolve) {if (node.level === 0) {this.getNodeData(resolve)} else {const children = []for (let deptId in this.deptMap) {if (this.deptMap[deptId].pid === node.data.id) {children.push(this.deptMap[deptId])}resolve(children)}}},handleCheck(data, currentData) {this.choosedOptions = this.multiple ? [data] : currentData.checkedNodes // this.$refs.treeRef.getCheckedNodes(false, false)if (this.choosedOptions.length > 0) {const tempMap = {}this.choosedOptions.forEach(op => {tempMap[op.id] = op})let tmpOps = []this.choosedOptions.forEach(op => {if (!tempMap[op.pid]) {tmpOps.push(op)}})this.choosedOptions = tmpOpsthis.choosedValue = this.choosedOptions.map(item => item.id)} else {this.choosedValue = []}this.$emit('input', this.multiple ? this.choosedValue : this.choosedValue[0])},chooseNode(data) {this.choosedOptions = [data]this.choosedValue = [data.id]this.$emit('input', data.id)this.$refs.selectRef.visible = false}}
}
</script><style scoped lang="scss">
.optionClass {display: none;
}.treeClass {background: transparent;margin: 10px;
}
</style>

http://www.ppmy.cn/news/1012327.html

相关文章

linux帮助命令

帮助命令 linux中命令很多&#xff0c;而且每个命令还有不同的选项&#xff0c;在使用命令的时候经常忘记该命令后应该跟什么选项来进行操作&#xff0c;linux中是有帮助命令来介绍每个命令及每个选项的 man命令 man命令是linux中最常用的帮助命令 #man [选项] 命令man ls-----…

WebRTC | 音视频直播客户端框架

端到端通信互动技术可分解为以下几个技术难点&#xff1a;客户端技术、服务器技术、全球设备网络适配技术和通信互动质量监控与展示技术。 一、音视频直播 音视频直播可分成两条技术路线&#xff1a;一条是以音视频会议为代表的实时互动直播&#xff1b;另一条是以娱乐直播为代…

19、springboot引用配置属性或引用生成文件的属性或引用随机值

引用配置属性或引用生成文件的属性或引用随机值 ★ 使用占位符引用其他配置属性&#xff1a; 配置文件中可用${}占位符引用已有的属性&#xff0c;被引用的属性可以是&#xff1a; - 已定义的属性。 - 来自其他配置源&#xff08;比如命令行的选项参数、环境变量、系统属性等…

单元测试和集成测试有什么区别

单元测试和集成测试有什么区别 单元测试和集成测试是软件开发中的两个重要测试阶段&#xff0c;它们的主要区别如下&#xff1a; 目的&#xff1a; 单元测试&#xff1a;主要针对代码的最小可测试单元&#xff0c;通常是一个函数或方法&#xff0c;确保它按照预期工作。集成…

华为harmonyos4.0鸿蒙4.0安装谷歌服务框架Play商店,解决从服务器检索信息时出错

8月4号华为手机发布了全新的harmonyos4.0鸿蒙4.0系统&#xff0c;很多人需要问还是不是支持谷歌服务框架&#xff1f;那么答案是肯定的&#xff0c;它和鸿蒙3是一样的&#xff0c;一样的操作&#xff0c;一样的支持安装谷歌服务框架&#xff0c;安装Google play商店。测试机型&…

【VUE】localStorage跨域数据操作实战笔记

环境 vue: ^3.3.4实战 发送端(即触发站点) 在App.vue中引入CrossDomainStorage组件(后面有实现过程) <script setup> import {ref } from vue import CrossDomainStorage from "@/components/CrossDomainStorage/index.vue"; const crossDomainStorageRef =…

安装Jenkins

一、什么是Jenkins Jenkins是一个开源软件项目&#xff0c;是基于Java开发的。我们可以利用Jenkins来实现持续集成的功能。 因为Jenkins是基于Java开发的&#xff0c;所以在安装Jenkins之前首先需要安装Java的JDK。 二、安装Jenkins 在Windows平台上面安装Jenkins共有两种方式…

时间复杂度接近O(n)的三种排序算法

1.桶排序 桶排序&#xff0c;顾名思义&#xff0c;会用到“桶”&#xff0c;核心思想是将要排序的数据分到几个有 序的桶里&#xff0c;每个桶内的数据再单独进行排序。桶内排完序之后&#xff0c;再把每个桶内的数据按照顺序依次 取出&#xff0c;组成的序列就是有序的了。 …