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
/>
相关推荐
a1117768 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得08 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
行走的陀螺仪10 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
We་ct11 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
2301_8127314111 小时前
CSS3笔记
前端·笔记·css3
ziblog11 小时前
CSS3白云飘动动画特效
前端·css·css3
越努力越幸运50812 小时前
CSS3学习之网格布局grid
前端·学习·css3
半斤鸡胗12 小时前
css3基础
前端·css
ziblog12 小时前
CSS3创意精美页面过渡动画效果
前端·css·css3
akangznl12 小时前
第四章 初识css3
前端·css·css3·html5