TreeSelect 组件 showCheckedStrategy 属性不生效问题

背景

在使用 AntdV 的 TreeSelect组件时,发现 设置showCheckedStrategy属性不生效,导致选中节点的显示格式不符合预期。

问题现象

当设置了 showCheckedStrategyTreeSelect.SHOW_ALL时,期望显示选中的节点以及它上层的父节点,期望回填显示:父节点1/父节点2/当前节点

问题原因

https://github.com/vueComponent/ant-design-vue/issues/2113

看到官方issue,一个已知问题,至今未修复...

解决方案

方案一: treeNodeLabelProp属性

通过指定一个自定义的属性来显示节点的完整路径信息,绕过原生的显示逻辑。

递归处理树数据,为每个节点添加完整路径

javascript 复制代码
const formatWithFullTitle = (nodes: any[], parentPath: string = ''): any[] => {
    return nodes.map(node => {
        const currentPath = parentPath ? `${parentPath} / ${node.name}` : node.name
        const formattedNode = {
            ...node,
            fullTitle: currentPath  // 添加完整路径属性
        }
        
        if (node.children && node.children.length > 0) {
            formattedNode.children = formatWithFullTitle(node.children, currentPath)
        }
        
        return formattedNode
    })
}

在 ATreeSelect 组件中配置

javascript 复制代码
<ATreeSelect
    v-model:value="selectedValue"
    :tree-data="formattedTreeData"
    treeNodeLabelProp="fullTitle"  <!-- 关键配置 -->
    :field-names="{
        children: 'children',
        label: 'name',
        value: 'id'
    }"
    show-search
    tree-default-expand-all
/>
相关推荐
幼儿园技术家12 分钟前
实现 GEO 监控:从多引擎探测到优化闭环
前端·后端
甲维斯12 分钟前
GLM5.2+ZCode复刻坦克大战,自测50万帧!
前端·ai编程·游戏开发
Csvn1 小时前
useRef 的 5 个冷门但救命的高级用法
前端
小小小小宇1 小时前
Harness Engineering 与 AI 联动
前端
鱼人1 小时前
HTML5 页面性能优化大全
前端
ping某1 小时前
专栏-null 和 undefined 到底是什么?
前端·javascript·后端
用户900463370401 小时前
5MB vs 4KB vs 无限大:浏览器存储谁更强?
前端
小小小小宇2 小时前
Harness Engineering 全解析与应用
前端
牧艺2 小时前
cos-design v3.0:从 15 个 Demo 到 49 个组件的视觉特效库
前端·视觉设计
lichenyang4532 小时前
ASCF 架构升级总览:WebRuntimePage 为什么要变薄
前端