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
/>
相关推荐
李顿波3 小时前
Chrome 扩展程序 MV3 架构组件概览
前端·chrome·浏览器插件
KingCodeHan3 小时前
ESLint 完全指南:从入门到精通
前端
GHL2842710903 小时前
安装chrome浏览器
前端·chrome
谙忆10244 小时前
WebCodecs 实战:用 ImageDecoder 和 VideoFrame 在浏览器里做硬件加速的帧处理
前端
JerrySir4 小时前
密码没泄露,2FA 也没失效:Chrome 146 为什么还要让 Cookie“搬不走”?
前端
小蚂蚁i4 小时前
React Hooks 原理深度解析:从会用到真正懂
前端·react.js
触底反弹4 小时前
Vue 实战:手把手教你实现 ChatGPT 同款打字机效果
前端·javascript·人工智能
BreezeJiang4 小时前
Vue 3 流式输出实战:ReadableStream + DeepSeek API 实现打字机对话
前端
用户EasyAdminBlazor4 小时前
EasyAdminBlazor 第十篇:数据导入导出——批量处理不再麻烦
前端·后端
饮茶三千4 小时前
电子保函模板编辑器实战二:HTML→FTL 编译转换的设计与实现
前端·vue.js·设计模式