antd-vue的修改树选择器treeData的属性名(递归实现)

在项目开发中,遇到antd-vue的树选择器要求treeData的键值对为title和value,但后端返回的数据是title和key。为了解决这个问题,需要编写一个递归函数,将后端数据转换为匹配tree选择器的格式。通过遍历和创建新的数据结构,将key替换为value,成功转换了数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目开发中,使用了antd-vue中的树选择器,但是antd-vue的树选择器的treeData的键值对是title和value.
但是我们后台给的数据是title和key,这就对应不上了,所以需要我们递归修改一下数据即可。

问题分析

1.官网实例

 //我们可以看到:tree-data="treeData" 这个tree-data依赖的数据是键值对title和value.
<a-tree-select
    v-model:value="value"
    show-search
    style="width: 100%"
    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
    placeholder="Please select"
    allow-clear
    tree-default-expand-all
    :tree-data="treeData"
  >
    <template #title="{ value: val, title }">
      <b v-if="val === 'parent 1-1'" style="color: #08c">sss</b>
      <template v-else>{{ title }}</template>
    </template>
  </a-tree-select>

//这是antd-vue的treeData,有一层层的title和value以及children
  const treeData = ref([
      {
        title: 'parent 1',
        value: 'parent 1',
        children: [
          {
            title: 'parent 1-0',
            value: 'parent 1-0',
            children: [
              {
                title: 'my leaf',
                value: 'leaf1',
              },
              {
                title: 'your leaf',
                value: 'leaf2',
              },
            ],
          },
          {
            title: 'parent 1-1',
            value: 'parent 1-1',
          },
        ],
      },
    ]);

2.现实后台返回的数据

可见,我们后台返回的数据是由title、key和children组成,并且children又是由title、key和children。

递归改造数据

那么,我们就需要自己写一个递归算法来改造一下后台返回的数据,将后台的title和key修改为官网需要的title和value。
//递归函数
let useFormatTree = (data) => {
    // data:传入的需要修改的后台数据
    let item: any = [];//定义一个空数组
    data.map((list) => {//遍历后台数据
        //定义一个空对象
        let newData = {};
        //给后台中的每一项赋值给这个新的对象
        newData.title = list.title;
        newData.value = list.key;
        //判断后台数据这项是不是有children,有的话递归调用,没有的话就为空。
        newData.children = list.children ? useFormatTree(list.children) : []
        //将这个对象push进定义的新数组
        item.push(newData);
    })
    return item;    //返回这个定义的数组,结果就是改造成功的结果。
}

下面让我们看一下改造完成的数据吧,可以看到数据已经改造为title和value的形式了。


抓紧时间练起来吧,兄dei,再不练你就废啦!

记得支持我哦,么么哒,祝您好事成双~~~~~~

### 实现 `a-tree-select` 自定义回填显示字段 在 `antd-design-vue` 中,可以通过设置 `labelInValue` 属性为 `true` 来获取选中项的完整信息,并通过自定义渲染函数来控制输入框中显示的内容。具体来说,可以利用 `onChange` 方法捕获选中节点的信息并动态更新输入框的显示内容。 以下是实现代码示例: ```vue <template> <a-space direction="vertical" style="width: 300px;"> <!-- TreeSelect 组件 --> <a-tree-select v-model:value="value" :tree-data="treeData" label-in-value @change="handleChange" placeholder="请选择" allow-clear /> <!-- 显示当前选中的值 --> <div>当前选中:{{ displayText }}</div> </a-space> </template> <script> export default { data() { return { value: null, // 当前选中的值 treeData: [ { title: 'Node1', key: '0-0', children: [{ title: 'Child Node1', key: '0-0-0' }] }, { title: 'Node2', key: '0-1', children: [{ title: 'Child Node2', key: '0-1-0' }] } ], displayText: '' // 输入框最终显示的文字 }; }, methods: { handleChange(selectedOption) { if (selectedOption && selectedOption.key) { const pathTitles = this.getPathTitles(this.treeData, selectedOption.key); this.displayText = pathTitles.join(' / '); // 自定义展示格式 } else { this.displayText = ''; // 清空显示文字 } }, getPathTitles(treeData, targetKey) { let result = []; function findPath(data, parentTitles = []) { for (const node of data) { const currentTitles = [...parentTitles, node.title]; if (node.key === targetKey) { result = currentTitles; return true; } if (node.children) { if (findPath(node.children, currentTitles)) { return true; } } } return false; } findPath(treeData); return result; // 返回路径上的所有标题 } } }; </script> ``` #### 解析 上述代码实现了以下功能: 1. **绑定数据源**:通过 `treeData` 定义形结构的数据[^1]。 2. **启用 `labelInValue`**:该属性允许我们获取完整的选项对象而非仅仅是键值[^2]。 3. **处理变更事件**:当用户选择某个节点时,触发 `@change` 方法,传入选中的节点信息。 4. **递归查找路径**:调用 `getPathTitles` 函数,基于目标节点的 `key` 值,在形结构中找到其对应的路径标题列表。 5. **拼接字符串**:将路径标题按指定分隔符 `/` 进行连接,并赋值给 `displayText` 变量用于展示。 此方法能够满足需求,即在选中某节点后,按照 “一层title/二层title” 的格式回显至输入框中。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值