Tree树形控件 - 图1

Tree 树形控件

多层次的结构列表。

何时使用

文件夹、组织架构、生物分类、国家地区等等,世间万物的大多数结构都是树形结构。使用树控件可以完整展现其中的层级关系,并具有展开收起选择等交互功能。

代码演示

  • Tree树形控件 - 图2parent 1
    • Tree树形控件 - 图3parent 1-0
      • leaf
      • leaf
    • Tree树形控件 - 图4parent 1-1
      • sss

基本用法

最简单的用法,展示可勾选,可选中,禁用,默认展开等功能。

  1. <template>
  2. <a-tree
  3. checkable
  4. :tree-data="treeData"
  5. v-model:expandedKeys="expandedKeys"
  6. v-model:selectedKeys="selectedKeys"
  7. v-model:checkedKeys="checkedKeys"
  8. >
  9. <template #title0010><span style="color: #1890ff">sss</span></template>
  10. </a-tree>
  11. </template>
  12. <script lang="ts">
  13. import { defineComponent, ref, watch } from 'vue';
  14. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  15. const treeData: TreeDataItem[] = [
  16. {
  17. title: 'parent 1',
  18. key: '0-0',
  19. children: [
  20. {
  21. title: 'parent 1-0',
  22. key: '0-0-0',
  23. disabled: true,
  24. children: [
  25. { title: 'leaf', key: '0-0-0-0', disableCheckbox: true },
  26. { title: 'leaf', key: '0-0-0-1' },
  27. ],
  28. },
  29. {
  30. title: 'parent 1-1',
  31. key: '0-0-1',
  32. children: [{ key: '0-0-1-0', slots: { title: 'title0010' } }],
  33. },
  34. ],
  35. },
  36. ];
  37. export default defineComponent({
  38. setup() {
  39. const expandedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  40. const selectedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  41. const checkedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  42. watch(expandedKeys, () => {
  43. console.log('expandedKeys', expandedKeys);
  44. });
  45. watch(selectedKeys, () => {
  46. console.log('selectedKeys', selectedKeys);
  47. });
  48. watch(checkedKeys, () => {
  49. console.log('checkedKeys', checkedKeys);
  50. });
  51. return {
  52. treeData,
  53. expandedKeys,
  54. selectedKeys,
  55. checkedKeys,
  56. };
  57. },
  58. });
  59. </script>
  • Tree树形控件 - 图5Expand to load
  • Tree树形控件 - 图6Expand to load
  • Tree Node

异步数据加载

点击展开节点,动态加载数据。

  1. <template>
  2. <a-tree
  3. :load-data="onLoadData"
  4. :tree-data="treeData"
  5. v-model:expandedKeys="expandedKeys"
  6. v-model:selectedKeys="selectedKeys"
  7. />
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue';
  11. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  12. export default defineComponent({
  13. setup() {
  14. const expandedKeys = ref<string[]>([]);
  15. const selectedKeys = ref<string[]>([]);
  16. const treeData = ref<TreeDataItem[]>([
  17. { title: 'Expand to load', key: '0' },
  18. { title: 'Expand to load', key: '1' },
  19. { title: 'Tree Node', key: '2', isLeaf: true },
  20. ]);
  21. const onLoadData = (treeNode: any) => {
  22. return new Promise((resolve: (value?: unknown) => void) => {
  23. if (treeNode.dataRef.children) {
  24. resolve();
  25. return;
  26. }
  27. setTimeout(() => {
  28. treeNode.dataRef.children = [
  29. { title: 'Child Node', key: `${treeNode.eventKey}-0` },
  30. { title: 'Child Node', key: `${treeNode.eventKey}-1` },
  31. ];
  32. treeData.value = [...treeData.value];
  33. resolve();
  34. }, 1000);
  35. });
  36. };
  37. return {
  38. expandedKeys,
  39. selectedKeys,
  40. treeData,
  41. onLoadData,
  42. };
  43. },
  44. });
  45. </script>

Tree树形控件 - 图7

可搜索

可搜索的树。

  1. <template>
  2. <div>
  3. <a-input-search v-model:value="searchValue" style="margin-bottom: 8px" placeholder="Search" />
  4. <a-tree
  5. v-model:expandedKeys="expandedKeys"
  6. :auto-expand-parent="autoExpandParent"
  7. :tree-data="gData"
  8. >
  9. <template #title="{ title }">
  10. <span v-if="title.indexOf(searchValue) > -1">
  11. {{ title.substr(0, title.indexOf(searchValue)) }}
  12. <span style="color: #f50">{{ searchValue }}</span>
  13. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
  14. </span>
  15. <span v-else>{{ title }}</span>
  16. </template>
  17. </a-tree>
  18. </div>
  19. </template>
  20. <script lang="ts">
  21. import { defineComponent, ref, watch } from 'vue';
  22. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  23. const x = 3;
  24. const y = 2;
  25. const z = 1;
  26. const genData: TreeDataItem[] = [];
  27. const generateData = (_level: number, _preKey?: string, _tns?: TreeDataItem[]) => {
  28. const preKey = _preKey || '0';
  29. const tns = _tns || genData;
  30. const children = [];
  31. for (let i = 0; i < x; i++) {
  32. const key = `${preKey}-${i}`;
  33. tns.push({ title: key, key });
  34. if (i < y) {
  35. children.push(key);
  36. }
  37. }
  38. if (_level < 0) {
  39. return tns;
  40. }
  41. const level = _level - 1;
  42. children.forEach((key, index) => {
  43. tns[index].children = [];
  44. return generateData(level, key, tns[index].children);
  45. });
  46. };
  47. generateData(z);
  48. const dataList: TreeDataItem[] = [];
  49. const generateList = (data: TreeDataItem[]) => {
  50. for (let i = 0; i < data.length; i++) {
  51. const node = data[i];
  52. const key = node.key;
  53. dataList.push({ key, title: key as string });
  54. if (node.children) {
  55. generateList(node.children);
  56. }
  57. }
  58. };
  59. generateList(genData);
  60. const getParentKey = (key: string, tree: TreeDataItem[]): string | number | undefined => {
  61. let parentKey;
  62. for (let i = 0; i < tree.length; i++) {
  63. const node = tree[i];
  64. if (node.children) {
  65. if (node.children.some(item => item.key === key)) {
  66. parentKey = node.key;
  67. } else if (getParentKey(key, node.children)) {
  68. parentKey = getParentKey(key, node.children);
  69. }
  70. }
  71. }
  72. return parentKey;
  73. };
  74. export default defineComponent({
  75. setup() {
  76. const expandedKeys = ref<string[]>([]);
  77. const searchValue = ref<string>('');
  78. const autoExpandParent = ref<boolean>(true);
  79. const gData = ref<TreeDataItem[]>(genData);
  80. watch(expandedKeys, () => {
  81. autoExpandParent.value = false;
  82. });
  83. watch(searchValue, value => {
  84. const expanded = dataList
  85. .map((item: TreeDataItem) => {
  86. if (item.title.indexOf(value) > -1) {
  87. return getParentKey(item.key, gData.value);
  88. }
  89. return null;
  90. })
  91. .filter((item, i, self) => item && self.indexOf(item) === i);
  92. expandedKeys.value = expanded as string[];
  93. searchValue.value = value;
  94. autoExpandParent.value = true;
  95. });
  96. return {
  97. expandedKeys,
  98. searchValue,
  99. autoExpandParent,
  100. gData,
  101. };
  102. },
  103. });
  104. </script>
  • Tree树形控件 - 图8Tree树形控件 - 图9parent 0
    • Tree树形控件 - 图10leaf 0-0
    • Tree树形控件 - 图11leaf 0-1
  • Tree树形控件 - 图12Tree树形控件 - 图13parent 1
    • Tree树形控件 - 图14leaf 1-0
    • Tree树形控件 - 图15leaf 1-1

目录

内置的目录树,multiple 模式支持 ctrl(Windows) / command(Mac) 复选。

  1. <template>
  2. <a-directory-tree
  3. multiple
  4. v-model:expandedKeys="expandedKeys"
  5. v-model:selectedKeys="selectedKeys"
  6. >
  7. <a-tree-node key="0-0" title="parent 0">
  8. <a-tree-node key="0-0-0" title="leaf 0-0" is-leaf />
  9. <a-tree-node key="0-0-1" title="leaf 0-1" is-leaf />
  10. </a-tree-node>
  11. <a-tree-node key="0-1" title="parent 1">
  12. <a-tree-node key="0-1-0" title="leaf 1-0" is-leaf />
  13. <a-tree-node key="0-1-1" title="leaf 1-1" is-leaf />
  14. </a-tree-node>
  15. </a-directory-tree>
  16. </template>
  17. <script lang="ts">
  18. import { defineComponent, ref } from 'vue';
  19. export default defineComponent({
  20. setup() {
  21. const expandedKeys = ref<string[]>(['0-0', '0-1']);
  22. const selectedKeys = ref<string[]>([]);
  23. return {
  24. expandedKeys,
  25. selectedKeys,
  26. };
  27. },
  28. });
  29. </script>
  • Tree树形控件 - 图16parent 1
    • Tree树形控件 - 图17张晨成
      • leaf
      • leaf
    • Tree树形控件 - 图18parent 1-1
      • zcvc

自定义TreeNode字段

替换treeNode中 title,key,children字段为treeData中对应的字段

  1. <template>
  2. <a-tree
  3. checkable
  4. :tree-data="treeData"
  5. v-model:expandedKeys="expandedKeys"
  6. v-model:selectedKeys="selectedKeys"
  7. v-model:checkedKeys="checkedKeys"
  8. :replace-fields="replaceFields"
  9. />
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, ref, watch } from 'vue';
  13. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  14. export default defineComponent({
  15. setup() {
  16. const expandedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  17. const selectedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  18. const checkedKeys = ref<string[]>(['0-0-0', '0-0-1']);
  19. const replaceFields = {
  20. children: 'child',
  21. title: 'name',
  22. };
  23. const treeData: TreeDataItem[] = [
  24. {
  25. name: 'parent 1',
  26. key: '0-0',
  27. child: [
  28. {
  29. name: '张晨成',
  30. key: '0-0-0',
  31. disabled: true,
  32. child: [
  33. { name: 'leaf', key: '0-0-0-0', disableCheckbox: true },
  34. { name: 'leaf', key: '0-0-0-1' },
  35. ],
  36. },
  37. {
  38. name: 'parent 1-1',
  39. key: '0-0-1',
  40. child: [{ key: '0-0-1-0', name: 'zcvc' }],
  41. },
  42. ],
  43. },
  44. ];
  45. watch(expandedKeys, () => {
  46. console.log('expandedKeys', expandedKeys);
  47. });
  48. watch(selectedKeys, () => {
  49. console.log('selectedKeys', selectedKeys);
  50. });
  51. watch(checkedKeys, () => {
  52. console.log('checkedKeys', checkedKeys);
  53. });
  54. return {
  55. expandedKeys,
  56. selectedKeys,
  57. checkedKeys,
  58. replaceFields,
  59. treeData: ref(treeData),
  60. };
  61. },
  62. });
  63. </script>
  • Tree树形控件 - 图190-0
    • Tree树形控件 - 图200-0-0
      • 0-0-0-0
      • 0-0-0-1
      • 0-0-0-2
    • Tree树形控件 - 图210-0-1
    • 0-0-2
  • Tree树形控件 - 图220-1
  • 0-2

拖动示例

将节点拖拽到其他节点内部或前后。

  1. <template>
  2. <a-tree
  3. class="draggable-tree"
  4. v-model:expandedKeys="expandedKeys"
  5. draggable
  6. :tree-data="gData"
  7. @dragenter="onDragEnter"
  8. @drop="onDrop"
  9. />
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, ref, VNode } from 'vue';
  13. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  14. interface DragEnterEvent {
  15. event: DragEvent;
  16. expandedKeys: string[];
  17. node: VNode;
  18. }
  19. interface DropEvent {
  20. dragNode: any;
  21. dragNodesKeys: string[];
  22. dropPosition: number;
  23. dropToGap: boolean;
  24. event: DragEvent;
  25. node: any;
  26. }
  27. const x = 3;
  28. const y = 2;
  29. const z = 1;
  30. const genData: TreeDataItem[] = [];
  31. const generateData = (_level: number, _preKey?: string, _tns?: TreeDataItem[]) => {
  32. const preKey = _preKey || '0';
  33. const tns = _tns || genData;
  34. const children = [];
  35. for (let i = 0; i < x; i++) {
  36. const key = `${preKey}-${i}`;
  37. tns.push({ title: key, key });
  38. if (i < y) {
  39. children.push(key);
  40. }
  41. }
  42. if (_level < 0) {
  43. return tns;
  44. }
  45. const level = _level - 1;
  46. children.forEach((key, index) => {
  47. tns[index].children = [];
  48. return generateData(level, key, tns[index].children);
  49. });
  50. };
  51. generateData(z);
  52. export default defineComponent({
  53. setup() {
  54. const expandedKeys = ref<string[]>(['0-0', '0-0-0', '0-0-0-0']);
  55. const gData = ref<TreeDataItem[]>(genData);
  56. const onDragEnter = (info: DragEnterEvent) => {
  57. console.log(info);
  58. // expandedKeys 需要展开时
  59. // expandedKeys.value = info.expandedKeys
  60. };
  61. const onDrop = (info: DropEvent) => {
  62. console.log(info);
  63. const dropKey = info.node.eventKey;
  64. const dragKey = info.dragNode.eventKey;
  65. const dropPos = info.node.pos.split('-');
  66. const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
  67. const loop = (data: TreeDataItem[], key: string, callback: any) => {
  68. data.forEach((item, index, arr) => {
  69. if (item.key === key) {
  70. return callback(item, index, arr);
  71. }
  72. if (item.children) {
  73. return loop(item.children, key, callback);
  74. }
  75. });
  76. };
  77. const data = [...gData.value];
  78. // Find dragObject
  79. let dragObj: TreeDataItem = {};
  80. loop(data, dragKey, (item: TreeDataItem, index: number, arr: TreeDataItem[]) => {
  81. arr.splice(index, 1);
  82. dragObj = item;
  83. });
  84. if (!info.dropToGap) {
  85. // Drop on the content
  86. loop(data, dropKey, (item: TreeDataItem) => {
  87. item.children = item.children || [];
  88. // where to insert 示例添加到尾部,可以是随意位置
  89. item.children.push(dragObj);
  90. });
  91. } else if (
  92. (info.node.children || []).length > 0 && // Has children
  93. info.node.expanded && // Is expanded
  94. dropPosition === 1 // On the bottom gap
  95. ) {
  96. loop(data, dropKey, (item: TreeDataItem) => {
  97. item.children = item.children || [];
  98. // where to insert 示例添加到尾部,可以是随意位置
  99. item.children.unshift(dragObj);
  100. });
  101. } else {
  102. let ar: TreeDataItem[] = [];
  103. let i = 0;
  104. loop(data, dropKey, (item: TreeDataItem, index: number, arr: TreeDataItem[]) => {
  105. ar = arr;
  106. i = index;
  107. });
  108. if (dropPosition === -1) {
  109. ar.splice(i, 0, dragObj);
  110. } else {
  111. ar.splice(i + 1, 0, dragObj);
  112. }
  113. }
  114. gData.value = data;
  115. };
  116. return {
  117. expandedKeys,
  118. gData,
  119. onDragEnter,
  120. onDrop,
  121. };
  122. },
  123. });
  124. </script>
  • Tree树形控件 - 图23Tree树形控件 - 图24parent 1
    • Tree树形控件 - 图25leaf
    • Tree树形控件 - 图26leaf

自定义图标

可以针对不同的节点定制图标。

  1. <template>
  2. <a-tree :tree-data="treeData" show-icon default-expand-all v-model:selectedKeys="selectedKeys">
  3. <template #switcherIcon>
  4. <down-outlined />
  5. </template>
  6. <template #smile>
  7. <smile-outlined />
  8. </template>
  9. <template #meh>
  10. <smile-outlined />
  11. </template>
  12. <template #custom="{ selected }">
  13. <frown-filled v-if="selected" />
  14. <frown-outlined v-else />
  15. </template>
  16. </a-tree>
  17. </template>
  18. <script lang="ts">
  19. import { DownOutlined, SmileOutlined, FrownOutlined, FrownFilled } from '@ant-design/icons-vue';
  20. import { defineComponent, ref } from 'vue';
  21. import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  22. const treeData: TreeDataItem[] = [
  23. {
  24. title: 'parent 1',
  25. key: '0-0',
  26. slots: {
  27. icon: 'smile',
  28. },
  29. children: [
  30. { title: 'leaf', key: '0-0-0', slots: { icon: 'meh' } },
  31. { title: 'leaf', key: '0-0-1', slots: { icon: 'custom' } },
  32. ],
  33. },
  34. ];
  35. export default defineComponent({
  36. components: {
  37. DownOutlined,
  38. SmileOutlined,
  39. FrownOutlined,
  40. FrownFilled,
  41. },
  42. setup() {
  43. return {
  44. selectedKeys: ref(['0-0-0']),
  45. treeData,
  46. };
  47. },
  48. });
  49. </script>

Tree树形控件 - 图27

连接线

节点之间带连接线的树,常用于文件目录结构展示。使用 showLine 开启,可以用 switcherIcon 修改默认图标。

  1. <template>
  2. <div>
  3. <div style="margin-bottom: 16px">
  4. showLine:
  5. <a-switch v-model:checked="showLine" />
  6. <br />
  7. <br />
  8. showIcon:
  9. <a-switch v-model:checked="showIcon" />
  10. </div>
  11. <a-tree
  12. :show-line="showLine"
  13. :show-icon="showIcon"
  14. :default-expanded-keys="['0-0-0', '0-0-1', '0-0-2']"
  15. @select="onSelect"
  16. >
  17. <template #icon><carry-out-outlined /></template>
  18. <a-tree-node key="0-0">
  19. <template #icon><carry-out-outlined /></template>
  20. <template #title><span style="color: #1890ff">parent 1</span></template>
  21. <a-tree-node title="parent 1-0" key="0-0-0">
  22. <template #icon><carry-out-outlined /></template>
  23. <a-tree-node title="leaf" key="0-0-0-0">
  24. <template #icon><carry-out-outlined /></template>
  25. </a-tree-node>
  26. <a-tree-node title="leaf" key="0-0-0-1">
  27. <template #icon><carry-out-outlined /></template>
  28. </a-tree-node>
  29. <a-tree-node title="leaf" key="0-0-0-2">
  30. <template #icon><carry-out-outlined /></template>
  31. </a-tree-node>
  32. </a-tree-node>
  33. <a-tree-node title="parent 1-1" key="0-0-1">
  34. <template #icon><carry-out-outlined /></template>
  35. <a-tree-node title="leaf" key="0-0-1-0">
  36. <template #icon><carry-out-outlined /></template>
  37. </a-tree-node>
  38. </a-tree-node>
  39. <a-tree-node title="parent 1-2" key="0-0-2">
  40. <template #icon><carry-out-outlined /></template>
  41. <a-tree-node title="leaf" key="0-0-2-0">
  42. <template #icon><carry-out-outlined /></template>
  43. </a-tree-node>
  44. <a-tree-node title="leaf" key="0-0-2-1">
  45. <template #icon><carry-out-outlined /></template>
  46. <template #switcherIcon><form-outlined /></template>
  47. </a-tree-node>
  48. </a-tree-node>
  49. </a-tree-node>
  50. </a-tree>
  51. </div>
  52. </template>
  53. <script lang="ts">
  54. import { CarryOutOutlined, FormOutlined } from '@ant-design/icons-vue';
  55. import { SelectEvent } from 'ant-design-vue/es/tree/Tree';
  56. import { defineComponent, ref } from 'vue';
  57. export default defineComponent({
  58. components: {
  59. CarryOutOutlined,
  60. FormOutlined,
  61. },
  62. setup() {
  63. const showLine = ref<boolean>(true);
  64. const showIcon = ref<boolean>(false);
  65. const onSelect = (selectedKeys: string[], info: SelectEvent) => {
  66. console.log('selected', selectedKeys, info);
  67. };
  68. return {
  69. showLine,
  70. showIcon,
  71. onSelect,
  72. };
  73. },
  74. });
  75. </script>
  • Tree树形控件 - 图28parent 1
    • Tree树形控件 - 图29parent 1-0
      • Tree树形控件 - 图30leaf
      • Tree树形控件 - 图31leaf
      • Tree树形控件 - 图32leaf
    • Tree树形控件 - 图33parent 1-1
    • Tree树形控件 - 图34parent 1-2

自定义展开/折叠图标

自定义展开/折叠图标。

  1. <template>
  2. <a-tree showLine v-model:expandedKeys="expandedKeys" v-model:selectedKeys="selectedKeys">
  3. <template #switcherIcon><down-outlined /></template>
  4. <a-tree-node title="parent 1" key="0-0">
  5. <a-tree-node title="parent 1-0" key="0-0-0">
  6. <a-tree-node title="leaf" key="0-0-0-0" />
  7. <a-tree-node title="leaf" key="0-0-0-1" />
  8. <a-tree-node title="leaf" key="0-0-0-2" />
  9. </a-tree-node>
  10. <a-tree-node key="0-0-1" title="parent 1-1">
  11. <a-tree-node key="0-0-1-0" title="leaf" />
  12. </a-tree-node>
  13. <a-tree-node key="0-0-2" title="parent 1-2">
  14. <a-tree-node key="0-0-2-0" title="leaf" />
  15. <a-tree-node key="0-0-2-1" title="leaf" />
  16. </a-tree-node>
  17. </a-tree-node>
  18. </a-tree>
  19. </template>
  20. <script lang="ts">
  21. import { DownOutlined } from '@ant-design/icons-vue';
  22. import { defineComponent, ref } from 'vue';
  23. export default defineComponent({
  24. components: {
  25. DownOutlined,
  26. },
  27. setup() {
  28. const expandedKeys = ref<string[]>(['0-0-0']);
  29. const selectedKeys = ref<string[]>([]);
  30. return {
  31. expandedKeys,
  32. selectedKeys,
  33. };
  34. },
  35. });
  36. </script>

API

Tree props

参数说明类型默认值版本
blockNode是否节点占据一行booleanfalse
treeDatatreeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一)array<{key, title, children, [disabled, selectable]}>
replaceFields替换 treeNode 中 title,key,children 字段为 treeData 中对应的字段object{children:’children’, title:’title’, key:’key’ }
autoExpandParent是否自动展开父节点booleantrue
checkable节点前添加 Checkbox 复选框booleanfalse
checkedKeys(v-model)(受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点 key,则子节点自动选中;相应当子节点 key 都传入,父节点也自动选中。当设置checkablecheckStrictly,它是一个有checkedhalfChecked属性的对象,并且父子节点的选中与否不再关联string[] | number[] | {checked: string[] | number[], halfChecked: string[] | number[]}[]
checkStrictlycheckable 状态下节点选择完全受控(父子节点选中状态不再关联)booleanfalse
defaultExpandAll默认展开所有树节点, 如果是异步数据,需要在数据返回后再实例化,建议用 v-if=”data.length”;当有 expandedKeys 时,defaultExpandAll 将失效booleanfalse
disabled将树禁用boolfalse
draggable设置节点可拖拽booleanfalse
expandedKeys(v-model)(受控)展开指定的树节点string[] | number[][]
filterTreeNode按需筛选树节点(高亮),返回 truefunction(node)-
loadData异步加载数据function(node)-
loadedKeys(受控)已经加载的节点,需要配合 loadData 使用string[] | number[][]
multiple支持点选多个节点(节点本身)booleanfalse
selectable是否可选中booleantrue
selectedKeys(v-model)(受控)设置选中的树节点string[] | number[]-
showIcon是否展示 TreeNode title 前的图标,没有默认样式,如设置为 true,需要自行定义图标相关样式booleanfalse
switcherIcon自定义树节点的展开/折叠图标slot-
showLine是否展示连接线booleanfalse
title标题slot2.0.0

事件

事件名称说明回调参数
check点击复选框触发function(checkedKeys, e:{checked: bool, checkedNodes, node, event})
dragenddragend 触发时调用function({event, node})
dragenterdragenter 触发时调用function({event, node, expandedKeys})
dragleavedragleave 触发时调用function({event, node})
dragoverdragover 触发时调用function({event, node})
dragstart开始拖拽时调用function({event, node})
dropdrop 触发时调用function({event, node, dragNode, dragNodesKeys})
expand展开/收起节点时触发function(expandedKeys, {expanded: bool, node})
load节点加载完毕时触发function(loadedKeys, {event, node})
rightClick响应右键点击function({event, node})
select点击树节点触发function(selectedKeys, e:{selected: bool, selectedNodes, node, event})

TreeNode props

结点描述数据对象,是 treeNodes 中的一项,TreeNode 使用相同的 API。

参数说明类型默认值版本
class节点的 classstring-
style节点的 stylestring|object-
checkable当树为 checkable 时,设置独立节点是否展示 Checkboxboolean-
disableCheckbox禁掉 checkboxbooleanfalse
disabled禁掉响应booleanfalse
icon自定义图标。可接收组件,props 为当前节点 propsslot|slot-scope-
isLeaf设置为叶子节点(设置了loadData时有效)booleanfalse
key被树的 (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys 属性所用。注意:整个树范围内的所有节点的 key 值不能重复!string | number内部计算出的节点位置
selectable设置节点是否可被选中booleantrue
title标题string|slot|slot-scope‘—-‘
slots使用 treeNodes 时,可以通过该属性配置支持 slot 的属性,如 slots: { title: ‘XXX’}object-

DirectoryTree props

参数说明类型默认值
expandAction目录展开逻辑,可选 false ‘click’ ‘dblclick’stringclick

FAQ

在 showLine 时,如何隐藏子节点图标?

文件图标通过 switcherIcon 来实现,如果不需要你可以覆盖对应的样式