el-tree-select懒加载数据如何回显?

<el-tree-select
  v-model="value"
  lazy
  :load="load"
  :props="props"
  node-key="id"
  @check-change="handleCheckedChange"
  :default-expanded-keys="expandedKeys"
  :default-checked-keys="checkedKeys"
  multiple
  show-checkbox
  />

数据是通过懒加载获取的,default-expanded-keys属性值需要从顶层根节点到当前选中节点的key路径,现在回显的时候只能得到当前选中节点的key,这样该如何回显呢?

想要的效果是:编辑回显时,展开树形控件选中的节点是默认选中的状态

阅读 6.7k
2 个回答
function getPathToNode(tree, targetKey, path = []) {
  for (const node of tree) {
    path.push(node.id);
    if (node.id === targetKey) {
      return path;
    }
    if (node.children) {
      const foundPath = getPathToNode(node.children, targetKey, path.slice());
      if (foundPath) {
        return foundPath;
      }
    }
    path.pop();
  }
  return null;
}

tree:是一个对象数组,你第一次调用时候,它是整个树的根节点数组。在递归调用里的时候,它是当前节点的子节点数组。
targetKey:这是你要查的选中节点的key。

运用组件库的:cache-data="cacheData"方法解决

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏