Skip to content

简单树形选择

表单中需要可筛选关键字的树形选择时使用

提示

a-tree使用双向绑定时有多种类型(选中复选框/展开折叠/选中)而且单选和多选绑定返回的数据结构差异较大。所以将最常用的功能进行封装,单选绑定定义的key值,多选绑定key值数组。另外也将展开折叠、父子关联、全选功能也进行了封装,不需要对应功能时可通过属性关闭

a-tree官方案例中有关键字检索的例子,但是由业务代码实现的,组件本身并没有封装。当多个业务都需要筛选树时,会造成重复代码过多的情况。所以将此功能也封装到组件中,不需要的话可通过属性关闭

基础用法

引入组件import EasyTreeSelect from '@/components/easy-tree-select/index.vue' 使用tree-data树形即可展示树形结构,使用v-model双向绑定获取选中的key值,默认为多选,v-model绑定的值为key值数组。

需确保tree-data属性传入的值是有效的。如果通过双向绑定或异步获取的值,需在组件中使用 v-if="data && data.length > 0"

image-20241220223012428

vue
<template>
  <a-typography-title :level="4">最简单的树形选择</a-typography-title>
  <a-typography-text>选中的值:{{value}}</a-typography-text>
  <a-row>
    <a-col :span="6">
      <easy-tree-select v-if="test_tree.length > 0" :tree-data="test_tree" v-model="value"/>
    </a-col>
  </a-row>
</template>
<script setup lang="ts">
import EasyTreeSelect from '@/components/easy-tree-select/index.vue'
import {initDict} from "@/utils/Dict.ts"
import {ref} from "vue";
const {test_tree} = initDict("test_tree")
const value = ref<string[]>([])
</script>

单选的属性选择

使用 :multiple="false" 来指定属性结构为单选模式,这时v-model绑定的值为key值

image-20241220220714104

vue
<template>
  <a-typography-title :level="4">单选树形结构</a-typography-title>
  <a-typography-text>选中的值:{{value}}</a-typography-text>
  <a-row>
    <a-col :span="6">
      <easy-tree-select v-if="test_tree.length > 0" :tree-data="test_tree" v-model="value" :multiple="false"/>
    </a-col>
  </a-row>
</template>
<script setup lang="ts">
import EasyTreeSelect from '@/components/easy-tree-select/index.vue'
import {initDict} from "@/utils/Dict.ts"
import {ref} from "vue";
const {test_tree} = initDict("test_tree")
const value = ref<string>()
</script>

使用自定义插槽

可自定义节点显示插槽,通过#title="{keyword, id, label}"可获取到关键词 keyword 及每个节点的item属性

image-20241220220948204

vue
<template>
  <a-typography-title :level="4">自定义插槽</a-typography-title>
  <a-typography-text>选中的值:{{value}}</a-typography-text>
  <a-row>
    <a-col :span="6">
      <easy-tree-select v-if="test_tree.length > 0" :tree-data="test_tree" v-model="value">
        <template #title="{keyword,id,label}">
          {{label + ' [' + id + ']'}}
        </template>
      </easy-tree-select>
    </a-col>
  </a-row>
</template>
<script setup lang="ts">
import EasyTreeSelect from '@/components/easy-tree-select/index.vue'
import {initDict} from "@/utils/Dict.ts"
import {ref} from "vue";
const {test_tree} = initDict("test_tree")
const value = ref<string[]>([])
</script>

API

双向绑定

属性名称描述类型默认值是否必填
v-model双向绑定与Key定义类型相同(单选) Key类型数组(多选)-

属性

属性名称描述类型默认值是否必填
treeData可选的树形结构数据具有属性结构的数组-
fieldNames树形结构字段对应别名{children: string, title: string, key: string}{children: 'children', title: 'label', key: 'id'}
defaultExpandAll默认展开全部(1.2.5)booleanfalse
checkStrictly父子关联(1.2.5)booleanfalse
multiple是否多选booleantrue
showToolbar显示工具栏booleantrue
showSearch显示搜索框booleantrue
searchPlaceholder搜索框提示词string请输入关键词
bodyStyle树形卡片body样式Object{padding: '8px',borderRadius: '8px'}
maxHeight最大高度number-
bordered是否展示边框booleantrue

插槽

插槽名称描述返回参数是否必须
title自定义树形节点插槽keyword及各个节点属性值

方法

方法名称描述参数
change选中值变化时触发选中的key(单选为key,多选为key数组)