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

打印 上一主题 下一主题

主题 687|帖子 687|积分 2061

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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

数据回显时,会出当代码如下:
  1. <template>
  2.   <el-tree ref="treeRef" :data="tree" show-checkbox node-key="id" :props="defaultProps"> </el-tree>
  3. </template>
  4. <script setup>
  5. import { ref, onMounted } from 'vue';
  6. const tree = ref([
  7.   {
  8.     id: 1,
  9.     label: '一级 1',
  10.     children: [
  11.       {
  12.         id: 2,
  13.         label: '二级 2',
  14.         children: [
  15.           { id: 3, label: '三级 3' },
  16.           { id: 4, label: '三级 4' },
  17.         ],
  18.       },
  19.     ],
  20.   },
  21.   {
  22.     id: 5,
  23.     label: '一级 5',
  24.     children: [
  25.       { id: 6, label: '二级 6' },
  26.       { id: 7, label: '二级 7' },
  27.     ],
  28.   },
  29.   {
  30.     id: 8,
  31.     label: '一级 8',
  32.     children: [
  33.       { id: 9, label: '二级 9' },
  34.       {
  35.         id: 10,
  36.         label: '二级 10',
  37.         children: [
  38.           { id: 11, label: '三级 11' },
  39.           { id: 12, label: '三级 12' },
  40.         ],
  41.       },
  42.     ],
  43.   },
  44. ]);
  45. // 树组件
  46. const treeRef = ref(null);
  47. const defaultProps = ref({ children: 'children', label: 'label' });
  48. // 回显数据
  49. const dataPlayback = ref([1, 2, 4, 5, 6, 7, 8, 10, 12]);
  50. onMounted(() => {
  51.   // 回显数据 赋值
  52.   treeRef.value.setCheckedKeys(dataPlayback.value);
  53. });
  54. </script>
复制代码
数据回显题目,如图所示:

修复方法如下:
  1. <template>
  2.   <el-tree ref="treeRef" :data="tree" show-checkbox node-key="id" :props="defaultProps"> </el-tree>
  3.   <el-button @click="submit">提交</el-button>
  4.   <span> {{ submitData }}</span>
  5. </template>
  6. <script setup>
  7. import { ref, onMounted } from 'vue';
  8. const tree = ref([
  9.   {
  10.     id: 1,
  11.     label: '一级 1',
  12.     children: [
  13.       {
  14.         id: 2,
  15.         label: '二级 2',
  16.         children: [
  17.           { id: 3, label: '三级 3' },
  18.           { id: 4, label: '三级 4' },
  19.         ],
  20.       },
  21.     ],
  22.   },
  23.   {
  24.     id: 5,
  25.     label: '一级 5',
  26.     children: [
  27.       { id: 6, label: '二级 6' },
  28.       { id: 7, label: '二级 7' },
  29.     ],
  30.   },
  31.   {
  32.     id: 8,
  33.     label: '一级 8',
  34.     children: [
  35.       { id: 9, label: '二级 9' },
  36.       {
  37.         id: 10,
  38.         label: '二级 10',
  39.         children: [
  40.           { id: 11, label: '三级 11' },
  41.           { id: 12, label: '三级 12' },
  42.         ],
  43.       },
  44.     ],
  45.   },
  46. ]);
  47. // 树组件
  48. const treeRef = ref(null);
  49. const defaultProps = ref({ children: 'children', label: 'label' });
  50. // 回显数据
  51. const dataPlayback = ref([1, 2, 4, 5, 6, 7, 8, 10, 12]);
  52. // 提交数据
  53. const submitData = ref([]);
  54. // 提取含有 children 的所有节点id
  55. const getContainChildrenNode = (data) => {
  56.   let ids = [];
  57.   const recurse = (item) => {
  58.     if (Array.isArray(item)) {
  59.       item.forEach((node) => {
  60.         if (node.children && node.children.length) {
  61.           // 含有子项的节点id
  62.           ids.push(node.id);
  63.           recurse(node.children);
  64.         }
  65.       });
  66.     }
  67.   };
  68.   // 调用递归函数
  69.   recurse(data);
  70.   // 返回含有 children 的所有节点id
  71.   return ids;
  72. };
  73. // 提交
  74. const submit = () => {
  75.   submitData.value = [...treeRef.value.getCheckedKeys(), ...treeRef.value.getHalfCheckedKeys()]; //得到 所有选中的节点id [ 4, 5, 6, 7, 12, 1, 2, 8, 10 ]
  76. };
  77. onMounted(() => {
  78.   // 收集所有顶级节点的值
  79.   const nodeIds = getContainChildrenNode(tree.value); // 得到 含有 children 的所有节点id [1, 2, 5, 8, 10]
  80.   // 过滤掉 顶级节点的值
  81.   const treeVal = dataPlayback.value.filter((item) => !nodeIds.includes(item)); // 得到 回显数据 [4, 6, 7, 12]
  82.   // 回显数据 赋值
  83.   treeRef.value.setCheckedKeys(treeVal);
  84. });
  85. </script>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表