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

vue3中实现el-tree通过ctrl或shift批量选择节点并高亮展示

一、看效果:

按住ctrl键实现单个多选                                按住shift实现区间范围多选                              

                  

二、代码:

        vue页面

<template><el-treeclass="w100%":data="$.treeData"ref="treeTableListRef":props="$.defaultProps"highlight-current:expand-on-click-node="false"key="id":default-expand-all="true"@node-click="(data, node) => $.tableFieldsNodeClick(data, node, treeTableListRef)"><template #default="{ data }"><div style="user-select: none">{{ data.name }}</div></template></el-tree>
</template><script setup lang="ts">
import { useData } from "./hooks/index";
const treeTableListRef = ref();
let { $data: $ } = useData();
onMounted(() => {});
onBeforeMount(() => {window.addEventListener("keydown", handleKeyDown);window.addEventListener("keyup", handleKeyUp);
});
// 按下为true
const handleKeyDown = (event: any) => {// 代表按下的是ctrl键if (event.key == "Control") {$.ctrlKeyPressed = true;}// 代表按下的是shift键if (event.key == "Shift") {$.shiftKeyPressed = true;}
};
// 释放为false
const handleKeyUp = (event: any) => {// 代表按下的是ctrl键if (event.key == "Control") {$.ctrlKeyPressed = false;}// 代表按下的是shift键if (event.key == "Shift") {$.shiftKeyPressed = false;}
};
</script><style scoped lang="scss">
</style>

        引入的hooks文件,index.ts

export function useData() {const $data: any = reactive({ctrlKeyPressed: false,shiftKeyPressed: false,shiftKeyFelid: [],defaultProps: {children: "children",label: "name",},treeData: [{name: '一级1',id: 1,children: [{name: '二级1',id: 2,children: [{name: '三级1',id: 2,}, {name: '三级2',id: 3,}, {name: '三级3',id: 4,}, {name: '三级4',id: 5,}, {name: '三级5',id: 6,}]}, {name: '二级2',id: 3,}, {name: '二级3',id: 4,}, {name: '二级4',id: 5,}, {name: '二级5',id: 6,}]}, {name: '一级2',id: 7,children: [{name: '二级1',id: 8,}, {name: '二级2',id: 9,}, {name: '二级3',id: 10,}, {name: '二级4',id: 11,}, {name: '二级5',id: 12,}]}],selectNodes: []})// 节点选中事件$data.tableFieldsNodeClick = (nodeData: any, node: any, treeTableListRef: any) => {const nodes = treeTableListRef.store._getAllNodes();//所有node节点const ishas = $data.selectNodes.includes(node.id)// 递归遍历节点数组进行ID存放function addSelectId(arr: any) {for (const item of arr) {$data.selectNodes.push(item.id)if (Array.isArray(item.children) && item.children.length) {addSelectId(item.children)}}}// 递归遍历删除节点idfunction delSelectId(arr: any) {for (const item of arr) {const index = $data.selectNodes.findIndex((x: any) => x == item.id);$data.selectNodes.splice(index, 1);if (Array.isArray(item.children) && item.children.length) {delSelectId(item.children);}}}// 按住了ctrl键,可以进行单个多选if ($data.ctrlKeyPressed) {// 如果为true代表当前选中的节点已存在if (ishas) {// 查找当前选中的节点的索引const index = $data.selectNodes.findIndex((x: any) => x == node.id);// 删除父节点$data.selectNodes.splice(index, 1);// 删除子节点if (Array.isArray(node.childNodes) && node.childNodes.length) {deleteSelectId(node.childNodes);}} else {// 否则当前选中的节点不存在,就加入到已选节点数组序列$data.selectNodes.push(node.id)// 防止选中的是父节点,就需要递归将子节点加入if (Array.isArray(node.childNodes) && node.childNodes.length) {addSelectId(node.childNodes);}}node.isCurrent = !node.isCurrent;// 按下了shift键,可以进行范围多选} else if ($data.shiftKeyPressed) {// 先清空$data.selectNodes = []// 将当前节点放入$data.selectNodes.push(node.id)$data.shiftKeyFelid.push(node.id);if ($data.shiftKeyFelid.length > 1) {// 首索引const sIndex = nodes.findIndex((x: any) => x.id == $data.shiftKeyFelid[0])// 尾索引const eIndex = nodes.findIndex((x: any) => x.id == $data.shiftKeyFelid[$data.shiftKeyFelid.length - 1]);// 根据首尾索引,存入中间节点const s = sIndex < eIndex ? sIndex : eIndex //取小值当开头索引const e = sIndex < eIndex ? eIndex : sIndex//取大值当结尾索引for (let i = s; i < e; i++) {// 放入该区间节点id$data.selectNodes.push(nodes[i].id);}}} else {// 否则就是单机选择$data.shiftKeyFelid = [];$data.selectNodes = [];$data.selectNodes = [node.id];}// 下面是对已选中的节点,进行高亮展示// 通过控制elementui中节点上的isCurrent属性// isCurrent为true是高亮,否则取消高亮for (const item of nodes) {if ($data.selectNodes.includes(item.id)) {item.isCurrent = true;} else {item.isCurrent = false;}}};return {$data: $data}
}

三、注意:

        1、重点是要获取当前所选节点数组

        2、通过循环节点数组来更新nodes节点中isCurrent属性,控制高亮

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

相关文章:

  • HarmonyOS 振动效果开发指导
  • 【ACM独立出版、确定的ISBN号】第三届密码学、网络安全和通信技术国际会议(CNSCT 2024)
  • Qt12.8
  • QT使用SQLite 超详细(增删改查、包括对大量数据快速存储和更新)
  • 基于Springboot+mybatis+mysql+jsp招聘网站
  • PHP介绍及安装
  • linux C++监听管道文件方式
  • 【Qt开发流程】之UI风格、预览及QPalette使用
  • 数组实现循环队列(增设队列大小size)
  • [BJDCTF2020]EzPHP 许多的特性
  • Ubuntu开机出现Welcome to emergency mode解决办法
  • Android 7.1 默认自拍镜像
  • 设计模式(二)-创建者模式(5)-建造者模式
  • 学习使用三个命令实现在腾讯云服务器TencentOS Server 3.1或者CentOS 8上安装ffmpeg
  • Java 22种设计模式详解
  • 代码随想录算法训练营第四十八天 _ 动态规划_198.打家劫舍、213.打家劫舍II、337.打家劫舍 III。
  • 记录一下快速上手Springboot登录注册项目
  • 【LVGL】STM32F429IGT6(在野火官网的LCD例程上)移植LVGL官方的例程(还没写完,有问题 排查中)
  • Vue学习笔记-Vue3中ref和reactive函数的使用
  • 大数据分析与应用实验任务十一
  • “78Win-Vận mệnh tốt”Trang web hỗ trợ kỹ thuật
  • React中使用react-json-view展示JSON数据
  • 一文简述“低代码开发平台”到底是什么?
  • HNU计算机体系结构-实验3:多cache一致性算法
  • Go语言学习路线规划
  • 微软NativeApi-NtQuerySystemInformation
  • 灵活与高效的结合,CodeMeter Cloud Lite轻云锁解决方案
  • Flink 系列文章汇总索引
  • 计算机网络——期末考试复习资料
  • 【数据结构】面试OJ题——链表