TreeSelect 组件 showCheckedStrategy 属性不生效问题

背景

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

问题现象

当设置了 showCheckedStrategy为 TreeSelect.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
/>
相关推荐
莪_幻尘几秒前
Agent 体检:乱编、连锁、失忆,给 Agent 做一次五维体检
前端·人工智能·agent
秋天的一阵风13 分钟前
🚀 nacos-web-config:运营半夜改条配置,网页秒更新 —— 不用发版、不用轮询,我把它开源了
java·前端·开源
不一样的少年_31 分钟前
别用前端思维写后端:一张 5MB 图片,为什么能撑爆内存?
前端·后端·图片资源
沐言人生33 分钟前
太吊了!这 2 款网盘工具,让我省 4000 大洋!
前端
鬼手点金36 分钟前
opencode-隐私优先配置
服务器·前端·javascript·bug·openclaw
涛涛ing39 分钟前
4200万月活、85万行代码,Lovable用6个月把生产应用从Next.js迁到了TanStack Start
前端
悠仁さん43 分钟前
【C++】auto
java·前端·c++
liangshanbo12151 小时前
面试题:React.memo 和 useMemo 的区别
前端·react.js·前端框架
苹果二1 小时前
使用Vue的指南
前端·javascript·vue.js
害人终害己2 小时前
‘vue-cli-service‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
前端·javascript·vue.js