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

vue实现下拉多选、可搜索、全选功能

最后的效果就是树形的下拉多选,可选择任意一级选项,下拉框中有一个按钮可以实现全选,也支持搜索功能。

在mounted生命周期里面获取全部部门的数据,handleTree是讲接口返回的数据整理成树形结构,可以自行解决

             <div class="LeftText"><span style="color: red; margin-right: 4px">*</span>部门:</div><el-selectv-model="executiveDepartName"filterable:filter-method="selectChange"multiple@visible-change="visibleChange"@remove-tag="seleRemoveTag"style="width: 80%"><el-option style="display: none" value=""></el-option><el-checkboxstyle="width: 100%;height: 40px;line-height: 40px;padding-left: 20px;border-bottom: 1px solid #dcdfe6;"class="allselect":indeterminate="isIndeterminate"v-model="allSelectModule"@change="allselect">全选</el-checkbox><el-cascader-panelref="cascaderModule":key="deptList.length":options="deptList"@change="cascaderChange"style="width: 80%":props="props"filterable:border="false":show-all-levels="false"v-model="executiveDepartment"></el-cascader-panel></el-select></div>

     props: {multiple: true,value: "deptId",label: "deptName",checkStrictly: true,emitPath: false,},   allDeptList:[];//所有的部门信息,内部结构为:{deptId:1,deptName:"一级部门"}isSeach:false;//是否搜索状态tempExecutive:[];// 搜索前已选中的数据//搜索查询事件--是因为在cascaderChange事件中,对v-model的值重新赋值,导致下拉选时,会触发el-select的搜索事件,所以加了一个isFilter判断selectChange(val) {if (val !== "") {this.deptList = [];this.deptList = this.allDeptList.filter((item) => {return item.deptName.toLowerCase().indexOf(val.toLowerCase()) > -1;});this.isSeach = true;this.tempExecutive = this.executiveDepartment;} else {if (!this.isFilter) {this.deptList = this.handleTree(this.allDeptList, "deptId");this.isFilter = !this.isFilter;}}},visibleChange(e) {if (e) {this.isSeach = false;this.isFilter = false;this.deptList = this.handleTree(this.allDeptList, "deptId");this.initStatus();}},对全选状态进行重新赋值initStatus() {if (this.executiveDepartment.length == this.allDeptList.length) {this.allSelectModule = true;this.isIndeterminate = false;} else if (this.executiveDepartment.length == 0) {this.allSelectModule = false;this.isIndeterminate = false;} else {this.allSelectModule = false;this.isIndeterminate = true;}},//select框里回显的是选中部门的名称getDeptName() {const result = [];this.executiveDepartment.filter((item) => {this.allDeptList.map((i) => {if (item == i.deptId) {result.push(i.deptName);}});});return result;},seleRemoveTag(val) {if (val) {const result = this.allDeptList.find((item) => {if (item.deptName == val) {return item;}});this.executiveDepartment = this.executiveDepartment.filter((item) => item !== result.deptId);}},// 下拉多选选中时触发的事件cascaderChange() {this.isFilter = true;//如果是搜索状态,讲之前选中的值和搜素状态下的值进行合并和去重,否则,之前选中的值会被清空if (this.isSeach) {this.executiveDepartment = [...new Set([...this.tempExecutive, ...this.executiveDepartment]),];}this.executiveDepartName = this.getDeptName();this.initStatus();},//全选事件allselect() {if (this.allSelectModule) {this.isIndeterminate = false;if (this.isSeach) {this.executiveDepartment = this.deptList.map((item) => item.deptId);this.executiveDepartName = this.getDeptName();} else {this.executiveDepartment = this.getAllIds(this.deptList);this.executiveDepartName = this.getDeptName();}} else {this.executiveDepartment = [];this.executiveDepartName = [];}},getAllIds(nodes) {let ids = [];(function getIds(nodes) {nodes.forEach((node) => {ids.push(node.deptId);if (node.children && node.children.length) {getIds(node.children);}});})(nodes);return ids;},

http://www.lryc.cn/news/513395.html

相关文章:

  • 探秘Kafka源码:关键内容解析
  • Android音频效果处理:基于`android.media.audiofx`包的原理、架构与实现
  • LeetCode - 初级算法 数组(两个数组的交集 II)
  • SQL 实战:分页查询的多种方式对比与优化
  • 汇川Easy系列正弦信号发生器(ST源代码)
  • JavaSpring AI与阿里云通义大模型的集成使用Java Data Science Library(JDSL)进行数据处理
  • Three.js教程002:Three.js结合Vue进行开发
  • pycharm+anaconda创建项目
  • vue2中遇到的问题与解决方案(自用)
  • CF2043b-B. Digits
  • ultralytics库RT-DETR代码解析
  • (七)- plane/crtc/encoder/connector objects
  • 基于STM32的四轴飞行器的控制系统(论文+源码)
  • 混合精度训练(Mixed Precision Training)中为什么在训练过程中不直接使用bf16进行权重更新?中英双语
  • 【java】HashMap的实现原理
  • FCM32F103C8T6开发指引
  • Python世界:人生苦短,我用Python
  • 【从零开始入门unity游戏开发之——C#篇43】C#补充知识——值类型和引用类型汇总补充、变量的生命周期与性能优化、值类型和引用类型组合使用
  • 从论文到实践:Stable Diffusion模型一键生成高质量AI绘画
  • 项目管理:用甘特图 “导航” 项目全程
  • v3.0.8- 「S+会员」新增专属运动秀,试试新穿搭吧- 与「好友」
  • 9-Gin 中自定义 Model --[Gin 框架入门精讲与实战案例]
  • 【VBA】EXCEL - VBA 创建 Sheet 表的 6 种方法,以及注意事项
  • 数据库中,group by 和partition by:数据分组和数据分区的区别
  • 【linux学习指南】Ext系列文件系统(四)路径分区链接
  • 深度学习中的参数初始化
  • wpf 基于Behavior库 的行为模块
  • 【每日学点鸿蒙知识】导入cardEmulation、自定义装饰器、CallState状态码顺序、kv配置、签名文件配置
  • 【SpringMVC】REST 风格
  • IDEA修改编译版本