antv中的TreeSelect连级选择问题

antv中的TreeSelect连级选择问题

需求效果

在这里插入图片描述

所有代码:

<template>
  <a-tree-select
    show-search
    style="width: 100%"
    :tree-data="treeData"
    :labelInValue = 'true'    // 重要
    v-model="value"
    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
    placeholder="Please select"
    allow-clear
    multiple
    tree-default-expand-all
    @change="onChange"
  />
</template>
<script>
const treeData = [
  {
    title: "Node1",
    value: "0-0",
    key: "0-0",
    children: [
      {
        title: "Child Node1",
        value: "0-0-0",
        key: "0-0-0",
        children: [
          {
            title: "Child Node1-1",
            value: "0-0-0-0",
            key: "0-0-0-0",
          },
        ],
      },
    ],
  },
  {
    title: "Node2",
    value: "0-1",
    key: "0-1",
    children: [
      {
        title: "Child Node3",
        value: "0-1-0",
        key: "0-1-0",
        disabled: true,
      },
      {
        title: "Child Node4",
        value: "0-1-1",
        key: "0-1-1",
      },
      {
        title: "Child Node5",
        value: "0-1-2",
        key: "0-1-2",
      },
    ],
  },
];
export default {
  data() {
    return {
      value: [],
      treeData,
    };
  },
  methods: {
    // 选中树节点时调用此函数
    onChange(value) {
      value.forEach((item) => {
        item.label = this.getTreePath(this.treeData, (v) => v.value === item.value);
      });
    },
    //获取树形数据的某个元素的所有父节点
    getTreePath(tree, func, path = []) {
      if (!tree) return [];
      for (const i of tree) {
        // 注意:title根据项目数据变化
        path.push(i.title);
        if (func(i)) return this.formatPath(path);
        if (i.children) {
          const findChildren = this.getTreePath(i.children, func, path);
          if (findChildren.length) return findChildren;
        }
        path.pop();
      }
      return [];
    },
    // 将上面父子级数组转换成 label/label/label格式
    formatPath(path) {
      let str = "";
      path.forEach((item) => {
        item = item.trim();
        str = str + item + "/";
      });
      return str.slice(0, str.length - 1);
    }
  },
};
</script>

参考文档

vue中使用js获取树形数据的所有父节点
js 树形结构数据 已知某一子节点 一次向上获取所有父节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值