记录实现级联选择器多选功能时主要用到的函数

级联选择器多选功能时主要用到的函数

1、校验所给层级是否有效并且是否为完整路径

// 功能:涉及到级联选择器回显时,需校验是否为完整的路径,是则回显,不是则无无效路径
// options为级联数据,selectedPaths为要校验的数据
validatePaths(selectedPaths, options) {let {originKeys} = this;let current = options;for (const value of selectedPaths) {if (!current) return false;const node = current.find(item => item[originKeys.value] === value);if (node) {current = node.children;} else {return false;}}// 校验是否为完整路径return this.isChange ? true : !current || current.length === 0;
},

2、查找元素父级

// 使用场景:如果所有子节点状态变成已选择,那么要找到父级将其状态也变成已选择,应用于回显以及操作节点时使用
// value为操作值,arr为级联数据
findParentByValue(value, arr) {let {originKeys} = this;let parent = null;const findParent = (items, parentObj) => {for (const item of items) {if (item[originKeys.value] === value) {parent = parentObj;break;}if (item.children) {findParent(item.children, item);}}};findParent(arr, null);return parent;
},

3、获取所有级别的路径

getAllLevelsPath() {let str = '';let {originKeys} = this;this.activeAry.forEach((item, index) => {str +=index < this.activeAry.length - 1? item.active[originKeys.label] + this.separator: item.active[originKeys.label];});return str;
},

4、状态判断

// 判断各个项状态,需要根据子项的状态判断父级也需要根据父级状态判断子集
allCheckStatus() {if (this.options.every(item => !this.getIsNotDisabled(item))) {return 'disabled'} else if (this.allCheckTrue(this.options)) {return 'checked'} else if (this.hasCheckTrue(this.options)) {return 'indeterminate'} else {return 'unChecked'}
},
// 元素没有被禁用则返回true
getIsNotDisabled(item) {let {defaultDisabledItems, originKeys} = this;if (!this.isChange) {if (defaultDisabledItems.includes(item[originKeys.value])) {return false;}if (item.children && item.children.length > 0) {return item.children.some(child => this.getIsNotDisabled(child));}return true;} else {return !defaultDisabledItems.includes(item[originKeys.value])}
},
// 多选级联每一项元素的勾选状态
getCheckStatus(item) {if (!this.isChange) {if (!this.getIsNotDisabled(item)) {return 'disabled'} else if (!item.check &&(!item.children || !item.children.length || !this.hasCheckTrue(item.children))) {return 'unChecked'} else if (!item.children || !item.children.length || this.allCheckTrue(item.children)) {return 'checked'} else {return 'indeterminate'}} else {return !this.getIsNotDisabled(item) ? 'disabled' : item.check ? 'checked' : 'unChecked'}
},
// 所有元素是否都被选择,包括孩子元素,剔除禁选元素
allCheckTrue(arr) {return arr.every(item => (item.check || !this.getIsNotDisabled(item)) &&(!item.children || !item.children.length || this.allCheckTrue(item.children)))
},
// 判断孩子元素中是否有被选择的,剔除禁选元素
hasCheckTrue(arr) {return arr.some(item => item.check &&this.getIsNotDisabled(item) ||(item.children && this.hasCheckTrue(item.children)));
},

5、给初始化的数组增加check属性

addCheckProperty(arr, ids, flag) {let {originKeys} = this;for (const item of arr) {if (ids.includes(item[originKeys.value])) {this.$set(item, 'check', flag);this.updataSelectedItems(item, flag)}if (item.children && item.children.length) {this.addCheckProperty(item.children, ids, flag);}}
},

6、元素没有被禁用则返回true

// 判断元素有没有被禁用,涉及父级全选以及回显功能
getIsNotDisabled(item) {let {defaultDisabledKeys, originKeys} = this;if (!this.isChange) {if (defaultDisabledKeys.includes(item[originKeys.value])) {return false;}if (item.children && item.children.length > 0) {return item.children.some(child => this.getIsNotDisabled(child));}return true;} else {return !defaultDisabledKeys.includes(item[originKeys.value])}
},

7、更新某一项

updataSelectedItems(item, flag) {let {originKeys} = this;this.$set(item, 'check', flag);if (item.children && item.children.length && !this.isChange){this.changeCheckProperty(item.children, flag);} else {if (flag) {// 未被禁选&&没有被选择的元素push到selectedItemsthis.getIsNotDisabled(item) &&!this.selectedItems.find(selectedItem => selectedItem[originKeys.value] === item.value) &&this.selectedItems.push(item)} else {// 过滤掉取消勾选的元素this.selectedItems = this.selectedItems.filter(val => val[originKeys.value] !== item[originKeys.value])}// 拿到全路径if (this.isShowLevels) {let hierarchyArr = [];let selectedOptionsArr = []this.selectedItems.forEach(item => {hierarchyArr.push(this.getSelectedOptionsWithPath(item[originKeys.value], this.options).hierarchy)selectedOptionsArr.push(this.getSelectedOptionsWithPath(item[originKeys.value], this.options).selectedOptions)})this.selectedItemsAllPath = hierarchyArrthis.activeItems = selectedOptionsArr}}
},

8、返回点击事件回传参数 以及全路径

//事件回传参数处理
getSelectedOptionsWithPath(val, options) {let {originKeys, separator} = this;let hierarchy = null;let selectedOptions = [];const findOption = (items, parentHierarchy) => {for (const item of items) {if (item[originKeys.value] === val) {let obj = {}obj[originKeys.value] = item[originKeys.value]obj[originKeys.label] = item[originKeys.label]selectedOptions.push(obj);hierarchy = parentHierarchy ?`${parentHierarchy}${separator}${item[originKeys.label]}` :item[originKeys.label];return true;}if (item.children && item.children.length) {const currentHierarchy = parentHierarchy ?`${parentHierarchy}${separator}${item[originKeys.label]}` :item[originKeys.label];const found = findOption(item.children, currentHierarchy);if (found) {let obj = {}obj[originKeys.value] = item[originKeys.value]obj[originKeys.label] = item[originKeys.label]selectedOptions.unshift(obj);return true;}}}return false;};findOption(options, null);return {hierarchy, selectedOptions};
},

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

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

相关文章

C++模板——(3)类模板

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 勤奋&#xff0c;机会&#xff0c;乐观…

使用pyinstaller打包生成exe(解决gradio程序的打包问题)

解决 [Errno 2] No such file or directory: gradio_client\types.json 问题&#xff0c;不需要手动创建hook文件 解决 FileNotFoundError: [Errno 2] No such file or directory: gradio\blocks_events.pyc 问题&#xff0c;不需要将pyi文件重命名为pyc文件 最终实现gradio程…

卡码网Java基础课 | 7. 摆平积木,8. 奇怪的信

卡码网Java基础课|7. 摆平积木 7. 摆平积木8. 奇怪的信 7. 摆平积木 import java.util.Scanner; import java.util.ArrayList;public class Main{public static void main(String[] args){Scanner sc new Scanner(System.in);while(sc.hasNextInt()){int n sc.nextInt();if(…

【CSS】讲一讲BFC、IFC、GFC、FFC

1. 前言 FC&#xff08;Formatting Contexts&#xff09;&#xff0c;是CSS2.1的一个概念&#xff0c;是页面中的一块渲染区域&#xff0c;具有一套渲染规则&#xff0c;决定FC中子元素如何定位&#xff0c;以及和其他元素的关系和相互作用。在说FC之前说一下文档流。 1.1. 普…

手撕 PCA

PCA&#xff08;Principal Component Analysis&#xff09;&#xff0c;中文名称&#xff1a;主成分分析。迄今为止最流行的降维算法。 假设 n 维空间中的一个单位立方体&#xff0c;易知&#xff1a;一维空间中该立方体中任意两点的距离不超过 1 1 1&#xff0c;二维空间中该…

自动连接校园网(河海大学)

layout: post # 使用的布局&#xff08;不需要改&#xff09; title: 自动连接校园网&#xff08;河海大学&#xff09; # 标题 subtitle: 网络 #副标题 date: 2024-01-09 # 时间 author: BY ThreeStones1029 # 作者 header-img: img/about_bg.jpg #这篇文章标题背景图片 catal…

GAMES101-Assignment4

一、问题总览 实现de Casteljau算法来绘制由4个控制点表示的Bzier曲线。需要修改main.cpp中的如下函数&#xff1a; bezier&#xff1a;该函数实现绘制Bzier曲线的功能。它使用一个控制点序列和一个OpenCV::Mat对象作为输入&#xff0c;没有返回值。它会使t在0到1的范围内进行…

Python采集微博评论做词云图

嗨喽~大家好呀&#xff0c;这里是魔王呐 ❤ ~! python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取 环境使用: Python 3.10 Pycharm 第三方模块使用: import requests >>> pip install requests import wordcloud >>> pip install wordclou…

扩展学习|数据融合助推商务智能与分析

文献来源&#xff1a;[1]李爱华,续维佳,石勇.基于数据融合的商务智能与分析架构研究[J].计算机科学,2022,49(12):185-194. 一、信息融合 &#xff08;一&#xff09;信息融合定义演变 早期信息融合的定义指出&#xff0c;其主要任务是综合分析若干传感器观测到的信息[9,…

请求参数乱码问题

POST请求方式解决乱码问题 在web.xml里面设置编码过滤器 <filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><!-- 设置过滤器中的属性值 -…

Python面试题(基础篇)

题目001: 在Python中如何实现单例模式。 点评&#xff1a;单例模式是指让一个类只能创建出唯一的实例&#xff0c;这个题目在面试中出现的频率极高&#xff0c;因为它考察的不仅仅是单例模式&#xff0c;更是对Python语言到底掌握到何种程度&#xff0c;建议大家用装饰器和元类…

微信小程序canvas画布实现矩形元素自由缩放、移动功能

获取画布信息并绘制背景 .whml <canvas class="canvas" type="2d" id="myCanvas" bindtouchstart="get_rect_touch_position" bindtouchmove="move_or_scale" bind:tap="finish_edit_check"/> .wxss .c…

【案例实战】业务稳定性运行之全链路混合压测

1.全链路压测开展步骤 &#xff08;1&#xff09;什么是全链路压测 全链路压测是指基于真实业务场景&#xff0c;通过模拟海量的用户请求&#xff0c;对整个后台服务进行压力测试&#xff0c;从而评估整个系统的性能水平。 对应用程序的整个技术栈进行完整的压力和性能测试&a…

DataFrame相关的API

目录 DataFrame的操作方案 SQL相关的API 创建一个视图/表 DSL相关的API DSL的传递方式 SQL的函数库 Spark SQL的综合应用 直接基于DataFrame来处理 SQL方式 DSL方式 基于RDD转换DataFrame的方式 DataFrame的操作方案 操作DataFrame一般有两种操作方案:一种为DSL方式,一种…

关于24年信息系统项目管理师论文如何提升?

信息系统项目管理师论文满分是75分&#xff0c;45分及以上为及格&#xff0c;论文评分可分为优良、及格与不及格3个档次。 评分的分数可分为&#xff1a; &#xff08;1&#xff09;60分至75分优良&#xff08;相当于百分制80分至100分&#xff09;。 &#xff08;2&#xf…

<设计模式> 七大原则

设计模式七大原则 开放封闭原则&#xff1a;对扩展开放&#xff0c;对修改关闭。这意味着在设计模式中&#xff0c;我们应该尽可能地将代码的扩展和修改分开处理&#xff0c;对于可以通过扩展来实现的功能&#xff0c;我们应该选择扩展代码&#xff0c;而对于必须修改现有代码…

数模学习day09-cftool使用

老版本的MATLAB可以在命令行使用cftool打开&#xff0c;2017a的版本可以直接找到。 x和y在你的工作区中需要已经存在&#xff0c;然后打开该工具箱就可以看见。 选择X和Y xy选择好之后就自动画好了拟合曲线。 Results分析 画好之后结果就呈现在这里了 这里的p1就是拟合系数&…

用React给XXL-JOB开发一个新皮肤(一):环境搭建和项目初始化

目录 一. 简述二. Fork 项目三. 搭建开发环境四. 初始化皮肤项目五. 添加相关依赖六. 预览 一. 简述 大名鼎鼎的 xxl-job 任务调度中心我们应该都使用过&#xff0c;项目地址&#xff1a;xxl-job。它是一个分布式任务调度平台&#xff0c;其核心设计目标是开发迅速、学习简单…

【代码随想录】刷题笔记Day48

前言 早上练车去了&#xff08;好久没有8点前醒了&#xff09;&#xff0c;练科目二两小时下来脚根可真酸啊&#xff0c;希望下周一把过。练完顺带去Apple西湖免费换新了耳机&#xff0c;羊毛爽&#xff01; 121. 买卖股票的最佳时机 - 力扣&#xff08;LeetCode&#xff09;…