TreeSelect树选择 - 图1

TreeSelect 树选择

树型选择控件。

何时使用

类似 Select 的选择控件,可选择的数据结构是一个树形结构时,可以使用 TreeSelect,例如公司层级、学科系统、分类目录等等。

代码演示

TreeSelect树选择 - 图2

基本用法

最简单的用法。

  1. <template>
  2. <a-tree-select
  3. v-model:value="value"
  4. show-search
  5. style="width: 100%"
  6. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  7. placeholder="Please select"
  8. allow-clear
  9. tree-default-expand-all
  10. >
  11. <a-tree-select-node key="0-1" value="parent 1" title="parent 1">
  12. <a-tree-select-node key="0-1-1" value="parent 1-0" title="parent 1-0">
  13. <a-tree-select-node key="random" value="leaf1" title="my leaf" />
  14. <a-tree-select-node key="random1" value="leaf2" title="your leaf" />
  15. </a-tree-select-node>
  16. <a-tree-select-node key="random2" value="parent 1-1" title="parent 1-1">
  17. <a-tree-select-node key="random3" value="sss">
  18. <template #title><b style="color: #08c">sss</b></template>
  19. </a-tree-select-node>
  20. </a-tree-select-node>
  21. </a-tree-select-node>
  22. </a-tree-select>
  23. </template>
  24. <script lang="ts">
  25. import { defineComponent, ref, watch } from 'vue';
  26. export default defineComponent({
  27. setup() {
  28. const value = ref<string>();
  29. watch(value, () => {
  30. console.log(value.value);
  31. });
  32. return {
  33. value,
  34. };
  35. },
  36. });
  37. </script>

TreeSelect树选择 - 图3

从数据直接生成

使用 treeData 把 JSON 数据直接生成树结构。

  1. <template>
  2. <a-tree-select
  3. v-model:value="value"
  4. style="width: 100%"
  5. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  6. :tree-data="treeData"
  7. placeholder="Please select"
  8. tree-default-expand-all
  9. >
  10. <template #title="{ key, value }">
  11. <span style="color: #08c" v-if="key === '0-0-1'">Child Node1 {{ value }}</span>
  12. </template>
  13. </a-tree-select>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent, ref, watch } from 'vue';
  17. interface TreeDataItem {
  18. value: string;
  19. key: string;
  20. title?: string;
  21. slots?: Record<string, string>;
  22. children?: TreeDataItem[];
  23. }
  24. const treeData: TreeDataItem[] = [
  25. {
  26. title: 'Node1',
  27. value: '0-0',
  28. key: '0-0',
  29. children: [
  30. {
  31. value: '0-0-1',
  32. key: '0-0-1',
  33. slots: {
  34. title: 'title',
  35. },
  36. },
  37. {
  38. title: 'Child Node2',
  39. value: '0-0-2',
  40. key: '0-0-2',
  41. },
  42. ],
  43. },
  44. {
  45. title: 'Node2',
  46. value: '0-1',
  47. key: '0-1',
  48. },
  49. ];
  50. export default defineComponent({
  51. setup() {
  52. const value = ref<string>();
  53. watch(value, () => {
  54. console.log(value.value);
  55. });
  56. return {
  57. value,
  58. treeData,
  59. };
  60. },
  61. });
  62. </script>

TreeSelect树选择 - 图4

后缀图标

最简单的用法。

  1. <template>
  2. <a-tree-select
  3. v-model:value="value"
  4. show-search
  5. style="width: 100%"
  6. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  7. placeholder="Please select"
  8. allow-clear
  9. tree-default-expand-all
  10. >
  11. <template #suffixIcon><SmileOutlined /></template>
  12. <a-tree-select-node value="parent 1" title="parent 1" key="0-1">
  13. <a-tree-select-node value="parent 1-0" title="parent 1-0" key="0-1-1">
  14. <a-tree-select-node value="leaf1" title="my leaf" key="random" />
  15. <a-tree-select-node value="leaf2" title="your leaf" key="random1" />
  16. </a-tree-select-node>
  17. <a-tree-select-node key="random2" value="parent 1-1" title="parent 1-1">
  18. <a-tree-select-node key="random3" value="sss">
  19. <template #title><b style="color: #08c">sss</b></template>
  20. </a-tree-select-node>
  21. </a-tree-select-node>
  22. </a-tree-select-node>
  23. </a-tree-select>
  24. </template>
  25. <script lang="ts">
  26. import { SmileOutlined } from '@ant-design/icons-vue';
  27. import { defineComponent, ref } from 'vue';
  28. export default defineComponent({
  29. components: {
  30. SmileOutlined,
  31. },
  32. setup() {
  33. const value = ref<string>();
  34. return {
  35. value,
  36. };
  37. },
  38. });
  39. </script>

TreeSelect树选择 - 图5

多选

多选的树选择。

  1. <template>
  2. <a-tree-select
  3. show-search
  4. style="width: 100%"
  5. v-model:value="value"
  6. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  7. placeholder="Please select"
  8. allow-clear
  9. multiple
  10. tree-default-expand-all
  11. >
  12. <a-tree-select-node key="0-1" value="parent 1" title="parent 1">
  13. <a-tree-select-node key="0-1-1" value="parent 1-0" title="parent 1-0">
  14. <a-tree-select-node key="random" value="leaf1" title="my leaf" />
  15. <a-tree-select-node key="random1" value="leaf2" title="your leaf" />
  16. </a-tree-select-node>
  17. <a-tree-select-node key="random2" value="parent 1-1" title="parent 1-1">
  18. <a-tree-select-node key="random3" value="sss">
  19. <template #title><b style="color: #08c">sss</b></template>
  20. </a-tree-select-node>
  21. </a-tree-select-node>
  22. </a-tree-select-node>
  23. </a-tree-select>
  24. </template>
  25. <script lang="ts">
  26. import { defineComponent, ref, watch } from 'vue';
  27. export default defineComponent({
  28. setup() {
  29. const value = ref<string[]>([]);
  30. watch(value, () => {
  31. console.log('select', value.value);
  32. });
  33. return {
  34. value,
  35. };
  36. },
  37. });
  38. </script>

TreeSelect树选择 - 图6

可勾选

使用勾选框实现多选功能。

  1. <template>
  2. <a-tree-select
  3. v-model:value="value"
  4. style="width: 100%"
  5. :tree-data="treeData"
  6. tree-checkable
  7. allow-clear
  8. :show-checked-strategy="SHOW_PARENT"
  9. search-placeholder="Please select"
  10. />
  11. </template>
  12. <script lang="ts">
  13. import { TreeSelect } from 'ant-design-vue';
  14. import { defineComponent, ref, watch } from 'vue';
  15. const SHOW_PARENT = TreeSelect.SHOW_PARENT;
  16. interface TreeDataItem {
  17. value: string;
  18. key: string;
  19. title?: string;
  20. disabled?: boolean;
  21. children?: TreeDataItem[];
  22. }
  23. const treeData: TreeDataItem[] = [
  24. {
  25. title: 'Node1',
  26. value: '0-0',
  27. key: '0-0',
  28. children: [
  29. {
  30. title: 'Child Node1',
  31. value: '0-0-0',
  32. key: '0-0-0',
  33. },
  34. ],
  35. },
  36. {
  37. title: 'Node2',
  38. value: '0-1',
  39. key: '0-1',
  40. children: [
  41. {
  42. title: 'Child Node3',
  43. value: '0-1-0',
  44. key: '0-1-0',
  45. disabled: true,
  46. },
  47. {
  48. title: 'Child Node4',
  49. value: '0-1-1',
  50. key: '0-1-1',
  51. },
  52. {
  53. title: 'Child Node5',
  54. value: '0-1-2',
  55. key: '0-1-2',
  56. },
  57. ],
  58. },
  59. ];
  60. export default defineComponent({
  61. setup() {
  62. const value = ref<string[]>(['0-0-0']);
  63. watch(value, () => {
  64. console.log(value.value);
  65. });
  66. return {
  67. value,
  68. treeData,
  69. SHOW_PARENT,
  70. };
  71. },
  72. });
  73. </script>

TreeSelect树选择 - 图7

异步加载

异步加载树节点。

  1. <template>
  2. <a-tree-select
  3. v-model:value="value"
  4. tree-data-simple-mode
  5. style="width: 100%"
  6. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  7. :tree-data="treeData"
  8. placeholder="Please select"
  9. :load-data="onLoadData"
  10. />
  11. </template>
  12. <script lang="ts">
  13. import { defineComponent, ref, watch } from 'vue';
  14. interface TreeDataItem {
  15. id: string | number;
  16. pId: number;
  17. value: string;
  18. title: string;
  19. isLeaf?: boolean;
  20. }
  21. export default defineComponent({
  22. setup() {
  23. const value = ref<string>();
  24. const treeData = ref<TreeDataItem[]>([
  25. { id: 1, pId: 0, value: '1', title: 'Expand to load' },
  26. { id: 2, pId: 0, value: '2', title: 'Expand to load' },
  27. { id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
  28. ]);
  29. watch(value, () => {
  30. console.log(value.value);
  31. });
  32. const genTreeNode = (parentId: number, isLeaf = false): TreeDataItem => {
  33. const random = Math.random().toString(36).substring(2, 6);
  34. return {
  35. id: random,
  36. pId: parentId,
  37. value: random,
  38. title: isLeaf ? 'Tree Node' : 'Expand to load',
  39. isLeaf,
  40. };
  41. };
  42. const onLoadData = (treeNode: any) => {
  43. console.log(treeNode);
  44. return new Promise((resolve: (value?: unknown) => void) => {
  45. const { id } = treeNode.dataRef;
  46. setTimeout(() => {
  47. treeData.value = treeData.value.concat([genTreeNode(id, false), genTreeNode(id, true)]);
  48. resolve();
  49. }, 300);
  50. });
  51. };
  52. return {
  53. value,
  54. treeData,
  55. genTreeNode,
  56. onLoadData,
  57. };
  58. },
  59. });
  60. </script>

API

Tree props

参数说明类型默认值版本
allowClear显示清除按钮booleanfalse
defaultValue指定默认选中的条目string/string[]-
disabled是否禁用booleanfalse
dropdownClassName下拉菜单的 className 属性string-
dropdownMatchSelectWidth下拉菜单和选择器同宽booleantrue
dropdownStyle下拉菜单的样式object-
filterTreeNode是否根据输入项进行筛选,默认用 treeNodeFilterProp 的值作为要筛选的 TreeNode 的属性值boolean|Function(inputValue: string, treeNode: TreeNode) (函数需要返回 bool 值)Function
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。Function(triggerNode)() => document.body
labelInValue是否把每个选项的 label 包装到 value 中,会把 value 类型从 string 变为 {value: string, label: VNode, halfChecked(treeCheckStrictly 时有效): string[] } 的格式booleanfalse
loadData异步加载数据function(node)-
maxTagCount最多显示多少个 tagnumber-
maxTagPlaceholder隐藏 tag 时显示的内容slot/function(omittedValues)-
multiple支持多选(当设置 treeCheckable 时自动变为 true)booleanfalse
placeholder选择框默认文字string|slot-
searchPlaceholder搜索框默认文字string|slot-
searchValue(v-model)搜索框的值,可以通过 search 事件获取用户输入string-
treeIcon是否展示 TreeNode title 前的图标,没有默认样式,如设置为 true,需要自行定义图标相关样式booleanfalse
showCheckedStrategy定义选中项回填的方式。TreeSelect.SHOW_ALL: 显示所有选中节点(包括父节点). TreeSelect.SHOW_PARENT: 只显示父节点(当父节点下所有子节点都选中时). 默认只显示子节点.enum{TreeSelect.SHOW_ALL, TreeSelect.SHOW_PARENT, TreeSelect.SHOW_CHILD }TreeSelect.SHOW_CHILD
showSearch在下拉中显示搜索框(仅在单选模式下生效)booleanfalse
size选择框大小,可选 large smallstring‘default’
suffixIcon自定义的选择框后缀图标VNode | slot-
treeCheckable显示 checkboxbooleanfalse
treeCheckStrictlycheckable 状态下节点选择完全受控(父子节点选中状态不再关联),会使得 labelInValue 强制为 truebooleanfalse
treeDatatreeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(value 在整个树范围内唯一)array<{value, label, children, [disabled, disableCheckbox, selectable]}>[]
replaceFields替换 treeNode 中 title,value,key,children 字段为 treeData 中对应的字段object{children:’children’, title:’title’, key:’key’, value: ‘value’ }
treeDataSimpleMode使用简单格式的 treeData,具体设置参考可设置的类型 (此时 treeData 应变为这样的数据结构: [{id:1, pId:0, value:’1’, label:”test1”,…},…], pId 是父节点的 id)false|Array<{ id: string, pId: string, rootPId: null }>false
treeDefaultExpandAll默认展开所有树节点booleanfalse
treeDefaultExpandedKeys默认展开的树节点string[] | number[]-
treeExpandedKeys(v-model)设置展开的树节点string[] | number[]-
treeNodeFilterProp输入项过滤对应的 treeNode 属性string‘value’
treeNodeLabelProp作为显示的 prop 设置string‘title’
value(v-model)指定当前选中的条目string/string[]-

事件

事件名称说明回调参数
change选中树节点时调用此函数function(value, label, extra)
search文本框值变化时回调function(value: string)
select被选中时调用function(value, node, extra)
treeExpand展开节点时调用function(expandedKeys)

Tree 方法

名称描述
blur()移除焦点
focus()获取焦点

TreeNode props

建议使用 treeData 来代替 TreeNode,免去手工构造麻烦

参数说明类型默认值版本
selectable是否可选booleantrue
checkable当树为 checkable 时,设置独立节点是否展示 Checkboxboolean-
disableCheckbox禁掉 checkboxbooleanfalse
disabled是否禁用booleanfalse
isLeaf是否是叶子节点booleanfalse
key此项必须设置(其值在整个树范围内唯一)string | number-
title树节点显示的内容string|slot‘—-‘
value默认根据此属性值进行筛选(其值在整个树范围内唯一)string-
scopedSlots使用 treeData 时,可以通过该属性配置支持 slot 的属性,如 scopedSlots: { title: ‘XXX’}object-