vue3:树的默认勾选和全选、取消全选

ops/2024/9/24 0:29:17/

实现的功能,上面有个选择框,当选中全部时,下方树被全选

代码:
javascript"><template><div><el-select v-model="selectAll" style="margin-bottom: 10px;" @change="handleSelectAllChange"><el-option value="all" label="全部">全部</el-option><el-option value="one" label="取消">取消</el-option></el-select><el-tree ref="treeRef" class="filter-tree" :data="treeData" :props="defaultProps" node-key=id highlight-current:current-node-key="leafNode.id" @node-click="treeNodeClick" default-expand-all show-checkbox @check="check":default-checked-keys="checKay" /><!-- 设置 check-strictly="true" 表明你希望节点之间的选择状态是独立的,用户可以单独选择每个节点而不影响其他节点的选中状态。这与全选或取消其下一级层级的功能是相符合的,因为这意味着用户可以选择父节点而不会影响其子节点的选中状态。--></div></template><script setup>
import { ref } from "@vue/reactivity";
import { nextTick, watch } from "@vue/runtime-core";
import { ElTree } from "element-plus";
const { proxy } = getCurrentInstance();
const emit = defineEmits(["treeNodeClick"]);const treeData = ref([{children: [{ id: "1", label: "朝阳区", nodeType: 2 }],id: "2", label: "北京市", nodeType: 1
},
{children: [{children: [{ id: "3", label: "高新区", nodeType: 3 },{ id: "5", label: "历下区", nodeType: 3 }],id: "4", label: "济南市", nodeType: 2}],id: "6", label: "山东省", nodeType: 1
},
{children: [{children: [{ id: "7", label: "长安区", nodeType: 3 }],id: "8", label: "石家庄", nodeType: 2}],id: "9", label: "河北省", nodeType: 1
},
{children: [{children: [{ id: "10", label: "中原区", nodeType: 3 }],id: "11", label: "郑州", nodeType: 2},{children: [{ id: "12", label: "老城区", nodeType: 3 }],id: "13", label: "洛阳", nodeType: 2}],id: "14", label: "河南省", nodeType: 1
},
])const treeRef = ref(null);
const selectAll = ref(""); // 使用字符串类型的变量表示选择全部的状态
const defaultProps = {children: 'children',label: 'label',
};
const checKay = ref([])const leafNode = ref({});
const check = (node, checked) => {if (checked) {// 选中节点console.log(node, checked);leafNode.value = node;} else {// 取消选中节点if (node.id === leafNode.value.id) {leafNode.value = {};}}
};
const treeNodeClick = (node) => {emit("treeNodeClick", node);
};
const handleSelectAllChange = (value) => {if (value == "all") {checKay.value = getAllIds(treeData.value)} else {// 如果取消选择全部,则遍历树节点并取消选中所有节点checKay.value = []nextTick(() => {treeRef.value.setCheckedKeys([]);});}
};
// 递归函数,获取所有节点的 id
const getAllIds = (nodes) => {let ids = [];nodes.forEach((node) => {ids.push(node.id);if (node.children) {ids = [...ids, ...getAllIds(node.children)];}});return ids;
};// 递归函数,取消选中所有节点
const deselectAllNodes = (nodes) => {nodes.forEach((node) => {node.checkedKeys = [];if (node.children) {deselectAllNodes(node.children);}});
};
</script>
<style scoped>
.filter-tree {/* margin-top: 10px; */
}
</style>
实现效果:

具体代码剖析:
 A、html部分
 <el-tree ref="treeRef" 
class="filter-tree" 
:data="treeData" 
:props="defaultProps"node-key=id highlight-current:current-node-key="leafNode.id"@node-click="treeNodeClick" 
default-expand-all 
show-checkbox@check="check":default-checked-keys="checKay" />

1、ref="treeRef":给 <el-tree> 组件设置一个引用名,可以在 JavaScript 中通过这个引用名来操作该组件。

2、:data="treeData":将 treeData 绑定到 <el-tree> 的 data 属性上,这个属性是树形结构的数据源,用于渲染树节点。

3、:props="defaultProps":将 defaultProps 绑定到 <el-tree> 的 props 属性上,这个属性用于指定树节点的一些属性配置,比如子节点的键名和显示内容的键名。

4、node-key="id":指定树节点数据中用作节点唯一标识的字段名。

5、highlight-current:当节点被选中时,高亮显示该节点。

6、:current-node-key="leafNode.id":设置当前被选中的节点的 key,通常用于在树中显示当前选中的节点。

7、@node-click="treeNodeClick":当点击树节点时触发 treeNodeClick 方法。

8、default-expand-all:默认展开所有的树节点。

9、show-checkbox:显示复选框,允许用户通过复选框选择树节点。

10、@check="check":当复选框状态发生变化时触发 check 方法。

11、:default-checked-keys="checKay":将 checKay 绑定到 <el-tree> 的 default-checked-keys 属性上,用于设置默认选中的节点的 key。

B、js部分

通过官网告诉我们的方法,我们点击节点,打印我们可以发现,选中的时候是放到这个数组里面的

javascript">const check = (node, checked) => {if (checked) {// 选中节点console.log(node, checked);leafNode.value = node;} else {// 取消选中节点if (node.id === leafNode.value.id) {leafNode.value = {};}}
};

我们知道default-checked-keys是树结构默认勾选的数组

const checKay = ref([])这个是我定义的默认勾选的数组

当我的选择框为全部 的时候,我只需要把树结构default-checked-keys存放的为全部数据的id即可

javascript">const handleSelectAllChange = (value) => {if (value == "all") {checKay.value = getAllIds(treeData.value)} else {// 如果取消选择全部,则遍历树节点并取消选中所有节点checKay.value = []nextTick(() => {treeRef.value.setCheckedKeys([]);});}
};
// 递归函数,获取所有节点的 id
const getAllIds = (nodes) => {let ids = [];nodes.forEach((node) => {ids.push(node.id);if (node.children) {ids = [...ids, ...getAllIds(node.children)];}});r

当不为全部的时候,树全部不选,其实就是将这个数组给清空   就行了

checKay.value = []

我直接写,发现,树并没有取消勾选,

这时候需要首先清空 checKay.value,然后使用 nextTick 方法等待下一个 DOM 更新周期,并在其中调用 treeRef.value.setCheckedKeys([]) 来清空树节点的选中状态

javascript">checKay.value = []nextTick(() => {treeRef.value.setCheckedKeys([]);});


http://www.ppmy.cn/ops/6001.html

相关文章

能源成果3D网络三维展厅越发主流化

在这个数字化飞速发展的时代&#xff0c;我们为您带来了全新的展览形式——线上3D虚拟展厅。借助VR虚拟现实制作和web3d开发技术&#xff0c;我们能够将物品、图片、视频和图文信息等完美融合&#xff0c;通过计算机技术和3D建模&#xff0c;为您呈现一个逼真、生动的数字化展览…

“Python+”集成技术高光谱遥感数据处理与机器学习深度应用

高光谱遥感数据处理的基础、python开发基础、机器学习和应用实践。重点解释高光谱数据处理所涉及的基本概念和理论&#xff0c;旨在帮助学员深入理解科学原理。结合Python编程工具&#xff0c;专注于解决高光谱数据读取、数据预处理、高光谱数据机器学习等技术难题&#xff0c;…

第十五届蓝桥杯题解-握手

题目大意&#xff1a;有50个人组成的派对&#xff0c;每个人都要与其他所有人进行握手&#xff0c;但其中有7个人之间互相不握手&#xff0c;求握手多少次&#xff0c;a与b握手即b与a握手 思路&#xff1a;规定前7个人互相不握手&#xff0c;两重for循环暴力就好 代码&#x…

STM32几种库的比较,HAL、标准库、LL库!

STM32是一系列由STMicroelectronics公司生产的微控制器系列。它们基于ARM Cortex-M内核&#xff0c;提供了广泛的性能和功能。STM32系列拥有多个型号和系列&#xff0c;每个型号都有不同的特性和功能。 STMicroelectronics提供了名为STM32Cube的软件生态系统&#xff0c;其中包…

设计模式---模板方法模式

一、介绍 所谓模板方法模式&#xff0c;就是提供一种方法的模板来实现一种规范&#xff0c;其他人可以利用这个模板定义自己的逻辑。 在Java编程中的应用&#xff0c;主要就是通过接口或者抽象类来实现的&#xff0c;抽象类中可以把逻辑函数声明为final类型&#xff0c;表示不能…

PyTorch深度解析:Tensor——神经网络的核心构建块

在深度学习和神经网络的研究与应用中&#xff0c;Tensor&#xff08;张量&#xff09;无疑是一个核心概念。特别是在PyTorch这一强大的深度学习框架中&#xff0c;Tensor更是扮演了举足轻重的角色。本文将深入探讨PyTorch中的Tensor&#xff0c;从其基本定义、特性、操作到实际…

CSS3 新特性

文章目录 选择器圆角效果 - border-radius阴影效果 - box-shadow渐变效果 - linear-gradient()形变效果 - transform过渡效果 - transition动画效果 - animation媒体查询 - media弹性盒子布局 - flex网格布局 - grid背景效果1. 多背景图2. 背景裁剪3. 透明效果 CSS3中引入了许多…

react 项目路由配置(react-router-dom 版本 v6.3、v6.4)

根据 react-router-dom 的版本&#xff0c;有不同的方式 一、react-router-dom v6.3 用到的主要 api: BrowserRouteruseRoutesOutlet 下面是详细步骤&#xff1a; 1、index.js BrowserRouter 用来实现 单页的客户端路由使用 BrowserRouter 包裹 App放在 顶级 位置&#x…