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

Element-plus、Element-ui之Tree 树形控件回显Bug问题。

需求:提交时,需要把选中状态半选中状态 的数据id提交。如图所示:

数据回显时,会出现代码如下:

<template><el-tree ref="treeRef" :data="tree" show-checkbox node-key="id" :props="defaultProps"> </el-tree>
</template><script setup>
import { ref, onMounted } from 'vue';const tree = ref([{id: 1,label: '一级 1',children: [{id: 2,label: '二级 2',children: [{ id: 3, label: '三级 3' },{ id: 4, label: '三级 4' },],},],},{id: 5,label: '一级 5',children: [{ id: 6, label: '二级 6' },{ id: 7, label: '二级 7' },],},{id: 8,label: '一级 8',children: [{ id: 9, label: '二级 9' },{id: 10,label: '二级 10',children: [{ id: 11, label: '三级 11' },{ id: 12, label: '三级 12' },],},],},
]);
// 树组件
const treeRef = ref(null);
const defaultProps = ref({ children: 'children', label: 'label' });
// 回显数据
const dataPlayback = ref([1, 2, 4, 5, 6, 7, 8, 10, 12]);onMounted(() => {// 回显数据 赋值treeRef.value.setCheckedKeys(dataPlayback.value);
});
</script>

数据回显问题,如图所示:

修复方法如下:

<template><el-tree ref="treeRef" :data="tree" show-checkbox node-key="id" :props="defaultProps"> </el-tree><el-button @click="submit">提交</el-button><span> {{ submitData }}</span>
</template><script setup>
import { ref, onMounted } from 'vue';const tree = ref([{id: 1,label: '一级 1',children: [{id: 2,label: '二级 2',children: [{ id: 3, label: '三级 3' },{ id: 4, label: '三级 4' },],},],},{id: 5,label: '一级 5',children: [{ id: 6, label: '二级 6' },{ id: 7, label: '二级 7' },],},{id: 8,label: '一级 8',children: [{ id: 9, label: '二级 9' },{id: 10,label: '二级 10',children: [{ id: 11, label: '三级 11' },{ id: 12, label: '三级 12' },],},],},
]);
// 树组件
const treeRef = ref(null);
const defaultProps = ref({ children: 'children', label: 'label' });
// 回显数据
const dataPlayback = ref([1, 2, 4, 5, 6, 7, 8, 10, 12]);
// 提交数据
const submitData = ref([]);// 提取含有 children 的所有节点id
const getContainChildrenNode = (data) => {let ids = [];const recurse = (item) => {if (Array.isArray(item)) {item.forEach((node) => {if (node.children && node.children.length) {// 含有子项的节点idids.push(node.id);recurse(node.children);}});}};// 调用递归函数recurse(data);// 返回含有 children 的所有节点idreturn ids;
};// 提交
const submit = () => {submitData.value = [...treeRef.value.getCheckedKeys(), ...treeRef.value.getHalfCheckedKeys()]; //得到 所有选中的节点id [ 4, 5, 6, 7, 12, 1, 2, 8, 10 ]
};onMounted(() => {// 收集所有顶级节点的值const nodeIds = getContainChildrenNode(tree.value); // 得到 含有 children 的所有节点id [1, 2, 5, 8, 10]// 过滤掉 顶级节点的值const treeVal = dataPlayback.value.filter((item) => !nodeIds.includes(item)); // 得到 回显数据 [4, 6, 7, 12]// 回显数据 赋值treeRef.value.setCheckedKeys(treeVal);
});
</script>

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

相关文章:

  • 互联网全景消息(10)之Kafka深度剖析(中)
  • Oracle Dataguard(主库为双节点集群)配置详解(5):将主库复制到备库并启动同步
  • pytorch小记(一):pytorch矩阵乘法:torch.matmul(x, y)
  • PyTorch环境配置常见报错的解决办法
  • 罗永浩再创业,这次盯上了 AI?
  • VUE3 provide 和 inject,跨越多层级组件传递数据
  • git打补丁
  • 机械燃油车知识图谱、知识大纲、知识结构(持续更新...)
  • Vue3学习总结
  • Type-C双屏显示器方案
  • 【读书与思考】焦虑与内耗
  • 基于python的网页表格数据下载--转excel
  • Vue.js开发入门:从零开始搭建你的第一个项目
  • LS1046+XILINX XDMA PCIE调通
  • HarmonyOS:@LocalBuilder装饰器: 维持组件父子关系
  • YOLOv10-1.1部分代码阅读笔记-downloads.py
  • 计算机图形学【绘制立方体和正六边形】
  • 基于django中医药数据可视化平台(源码+lw+部署文档+讲解),源码可白嫖!
  • kafka消费堆积问题探索
  • Vue.js 使用插槽(Slots)优化组件结构
  • Broker如何进行定时心跳发送和故障感知
  • 网络安全设备主要有什么
  • Android Framework WMS全面概述和知识要点
  • 记一次某红蓝演练经历
  • 一个运行在浏览器中的开源Web操作系统Puter本地部署与远程访问
  • 【零基础入门Go语言】struct 和 interface:Go语言是如何实现继承的?
  • 麦田物语学习笔记:实现拖拽物品交换数据和在地图上生成物品
  • 一些计算机零碎知识随写(25年1月)-1
  • Qt学习笔记第81到90讲
  • Centos9 + Docker 安装 MySQL8.4.0 + 定时备份数据库到本地