记录element-plus树型表格的bug

问题描述

如果数据的子节点命名时children,就没有任何问题,如果后端数据结构子节点是其他名字,比如thisChildList就有bug

js 复制代码
  const tableData = [
    {
      id: 1,
      date: '2016-05-02',
      name: 'wangxiaohu',
      address: 'No. 189, Grove St, Los Angeles',
      selectedAble: true,
      thisChildList: [
        {
          id: 131,
          date: '2016-05-01',
          name: 'wangxiaohu',
          address: 'No. 189, Grove St, Los Angeles',
          selectedAble: true,
          thisChildList: [
           ...
          ]
        },
        
  ]

解决

参考大佬的代码
传送门

js 复制代码
// 递归
  const setChildren = (children, type) => {
    // 编辑多个子层级
    children.map((j) => {
      toggleSelection(j, type)
      if (j.thisChildList) {
        setChildren(j.thisChildList, type)
      }
    })
  }

  // 选中父节点时,子节点一起选中取消
  const select = (selection, row) => {
    console.log('select!!')

    const hasSelect = selection.some((el) => {
      return row.id === el.id
    })
    console.log('hasSelect', hasSelect)
    if (hasSelect) {
      if (row.thisChildList) {
        // 解决子组件没有被勾选到
        setChildren(row.thisChildList, true)
      }
    } else {
      // 解决点击父组件取消选中,子级也跟着取消
      if (row.thisChildList) {
        setChildren(row.thisChildList, false)
      }
    }
  }
  const toggleSelection = (row, select) => {
    if (row) {
      // 通过使用 nextTick 来延迟执行后续的代码,以确保在更新表格的选中状态之前先进行其他可能的 DOM 更新
      nextTick(() => {
        // 这里 && 的作用是 如果左侧表达式的值为真值,则返回右侧表达式的值;
        // 否则,返回左侧表达式的值。如果左侧表达式的值为假值,则整个表达式的结果也为假值。

        // toggleRowSelection用于多选表格,切换某一行的选中状态, 如果使用了第二个参数,则可直接设置这一行选中与否
        multipleTable.value && multipleTable.value.toggleRowSelection(row, select)
        // 也可以写成 multipleTable.value?.toggleRowSelection(row, select)
      })
    }
  }

  // 选择全部 默认全选框只能影响第一级的 二、三等级别不会联动
  //   当用户手动勾选全选 Checkbox 时触发的事件
  const selectAll = (selection) => {
    console.log('selectAll------------------selection', selection)

    // tabledata第一层只要有在selection里面就是全选
    const isSelect = selection.some((el) => {
      const tableDataIds = tableData.map((j) => j.id)
      return tableDataIds.includes(el.id)
    })
    // tableDate第一层只要有不在selection里面就是全不选
    const isCancel = !tableData.every((el) => {
      const selectIds = selection.map((j) => j.id)
      return selectIds.includes(el.id)
    })
    if (isSelect) {
      selection.map((el) => {
        if (el.thisChildList) {
          // 解决子组件没有被勾选到
          setChildren(el.thisChildList, true)
        }
      })
    }
    if (isCancel) {
      tableData.map((el) => {
        if (el.thisChildList) {
          // 解决子组件没有被勾选到
          setChildren(el.thisChildList, false)
        }
      })
    }
  }
  //   const selectionChange = (val) => {
  //     console.log(val)
  //   }

但仍然有问题,比如3级节点选中,他的父级节点无动于衷,不会联动

解决2

或者把thisChildList 改成children

js 复制代码
function renameChildListToChildren(data) {
  if (!Array.isArray(data)) {
    return data;
  }
  return data.map(item => {
    const newItem = { ...item };
    if (newItem.thisChildList) {
      newItem.children = renameChildListToChildren(newItem.thisChildList);
      delete newItem.thisChildList;
    }
    return newItem;
  });
}

const newData = renameChildListToChildren(tableData);
console.log(newData);

但是官方也有这个问题

二级不能影响一级的选中,有bug

相关推荐
柒和远方19 小时前
每日一学V010: 从 Python 回到前端:一个 AI Native 开发者的 JavaScript 底层基础补全
javascript
之歆19 小时前
Day21_电商详情页核心技术实战:从LESS预处理到复杂交互实现
开发语言·前端·javascript·css·交互·less
海鸥两三19 小时前
基于 Vue 3 + 高德地图的网格规划系统实战(有源码)
前端·javascript·vue.js
逸A19 小时前
某里v2反混淆 codec 化路上踩到的两个隐蔽坑:被清零的 salt 与 opaque loop bound
javascript·人工智能·目标跟踪
丷丩19 小时前
MapLibre GL JS第11课:获取鼠标指针坐标
前端·javascript·gis·地图·mapbox·maplibre gl js
代码AI弗森19 小时前
前端周刊第 467 期[特殊字符] 本期精选目录
前端
随便的名字19 小时前
前端路由的底层逻辑:URL 中 # 和 ? 的区别与关系详解
前端
kongba00719 小时前
ttyd Web终端安装指南(OpenCloudOS 9)
linux·前端
zhoumeina9919 小时前
前端串行合成流程 + 每张图上传接口
前端·状态模式
风骏时光牛马19 小时前
Swift 基于MVVM架构实现完整列表数据展示与交互功能实战案例
前端