Tree树形控件

何时使用

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

代码演示

Tree树形控件 - 图1

基本

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

  1. import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
  2. import { NzFormatEmitEvent, NzTreeComponent, NzTreeNodeOptions } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-basic',
  5. template: `
  6. <nz-tree
  7. #nzTreeComponent
  8. [nzData]="nodes"
  9. nzCheckable
  10. [nzCheckedKeys]="defaultCheckedKeys"
  11. [nzExpandedKeys]="defaultExpandedKeys"
  12. [nzSelectedKeys]="defaultSelectedKeys"
  13. (nzClick)="nzClick($event)"
  14. (nzCheckBoxChange)="nzCheck($event)"
  15. (nzExpandChange)="nzCheck($event)"
  16. >
  17. </nz-tree>
  18. `
  19. })
  20. export class NzDemoTreeBasicComponent implements OnInit, AfterViewInit {
  21. @ViewChild('nzTreeComponent') nzTreeComponent: NzTreeComponent;
  22. defaultCheckedKeys = ['10020'];
  23. defaultSelectedKeys = ['10010'];
  24. defaultExpandedKeys = ['100', '1001'];
  25. nodes: NzTreeNodeOptions[] = [
  26. {
  27. title: 'parent 1',
  28. key: '100',
  29. children: [
  30. {
  31. title: 'parent 1-0',
  32. key: '1001',
  33. disabled: true,
  34. children: [
  35. { title: 'leaf 1-0-0', key: '10010', disableCheckbox: true, isLeaf: true },
  36. { title: 'leaf 1-0-1', key: '10011', isLeaf: true }
  37. ]
  38. },
  39. {
  40. title: 'parent 1-1',
  41. key: '1002',
  42. children: [
  43. { title: 'leaf 1-1-0', key: '10020', isLeaf: true },
  44. { title: 'leaf 1-1-1', key: '10021', isLeaf: true }
  45. ]
  46. }
  47. ]
  48. }
  49. ];
  50. nzClick(event: NzFormatEmitEvent): void {
  51. console.log(event);
  52. }
  53. nzCheck(event: NzFormatEmitEvent): void {
  54. console.log(event);
  55. }
  56. // nzSelectedKeys change
  57. nzSelect(keys: string[]): void {
  58. console.log(keys, this.nzTreeComponent.getSelectedNodeList());
  59. }
  60. ngOnInit(): void {
  61. // TODO something
  62. }
  63. ngAfterViewInit(): void {
  64. // get node by key: '10011'
  65. console.log(this.nzTreeComponent.getTreeNodeByKey('10011'));
  66. // use tree methods
  67. console.log(
  68. this.nzTreeComponent.getTreeNodes(),
  69. this.nzTreeComponent.getCheckedNodeList(),
  70. this.nzTreeComponent.getSelectedNodeList(),
  71. this.nzTreeComponent.getExpandedNodeList()
  72. );
  73. }
  74. }

Tree树形控件 - 图2

拖动示例

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

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatEmitEvent } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-draggable',
  5. template: `
  6. <nz-tree [nzData]="nodes" nzDraggable nzBlockNode (nzOnDrop)="nzEvent($event)"> </nz-tree>
  7. `
  8. })
  9. export class NzDemoTreeDraggableComponent implements OnInit {
  10. nodes = [
  11. {
  12. title: '0-0',
  13. key: '00',
  14. expanded: true,
  15. children: [
  16. {
  17. title: '0-0-0',
  18. key: '000',
  19. expanded: true,
  20. children: [
  21. { title: '0-0-0-0', key: '0000', isLeaf: true },
  22. { title: '0-0-0-1', key: '0001', isLeaf: true },
  23. { title: '0-0-0-2', key: '0002', isLeaf: true }
  24. ]
  25. },
  26. {
  27. title: '0-0-1',
  28. key: '001',
  29. children: [
  30. { title: '0-0-1-0', key: '0010', isLeaf: true },
  31. { title: '0-0-1-1', key: '0011', isLeaf: true },
  32. { title: '0-0-1-2', key: '0012', isLeaf: true }
  33. ]
  34. },
  35. {
  36. title: '0-0-2',
  37. key: '002'
  38. }
  39. ]
  40. },
  41. {
  42. title: '0-1',
  43. key: '01',
  44. children: [
  45. {
  46. title: '0-1-0',
  47. key: '010',
  48. children: [
  49. { title: '0-1-0-0', key: '0100', isLeaf: true },
  50. { title: '0-1-0-1', key: '0101', isLeaf: true },
  51. { title: '0-1-0-2', key: '0102', isLeaf: true }
  52. ]
  53. },
  54. {
  55. title: '0-1-1',
  56. key: '011',
  57. children: [
  58. { title: '0-1-1-0', key: '0110', isLeaf: true },
  59. { title: '0-1-1-1', key: '0111', isLeaf: true },
  60. { title: '0-1-1-2', key: '0112', isLeaf: true }
  61. ]
  62. }
  63. ]
  64. },
  65. {
  66. title: '0-2',
  67. key: '02',
  68. isLeaf: true
  69. }
  70. ];
  71. nzEvent(event: NzFormatEmitEvent): void {
  72. console.log(event);
  73. }
  74. ngOnInit(): void {}
  75. }

Tree树形控件 - 图3

异步数据加载

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

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatEmitEvent, NzTreeNodeOptions } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-dynamic',
  5. template: `
  6. <nz-tree [nzData]="nodes" nzAsyncData (nzClick)="nzEvent($event)" (nzExpandChange)="nzEvent($event)"> </nz-tree>
  7. `
  8. })
  9. export class NzDemoTreeDynamicComponent implements OnInit {
  10. nodes = [
  11. { title: 'Expand to load', key: '0' },
  12. { title: 'Expand to load', key: '1' },
  13. { title: 'Tree Node', key: '2', isLeaf: true }
  14. ];
  15. nzEvent(event: Required<NzFormatEmitEvent>): void {
  16. console.log(event);
  17. // load child async
  18. if (event.eventName === 'expand') {
  19. const node = event.node;
  20. if (node && node.getChildren().length === 0 && node.isExpanded) {
  21. this.loadNode().then(data => {
  22. node.addChildren(data);
  23. });
  24. }
  25. }
  26. }
  27. loadNode(): Promise<NzTreeNodeOptions[]> {
  28. return new Promise(resolve => {
  29. setTimeout(
  30. () =>
  31. resolve([
  32. { title: 'Child Node', key: `${new Date().getTime()}-0` },
  33. { title: 'Child Node', key: `${new Date().getTime()}-1` }
  34. ]),
  35. 1000
  36. );
  37. });
  38. }
  39. ngOnInit(): void {}
  40. }

Tree树形控件 - 图4

连接线

带连接线的树。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatEmitEvent } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-line',
  5. template: `
  6. <nz-tree [nzData]="nodes" nzShowLine (nzClick)="nzEvent($event)"> </nz-tree>
  7. `
  8. })
  9. export class NzDemoTreeLineComponent implements OnInit {
  10. nodes = [
  11. {
  12. title: 'parent 1',
  13. key: '100',
  14. expanded: true,
  15. children: [
  16. {
  17. title: 'parent 1-0',
  18. key: '1001',
  19. expanded: true,
  20. children: [
  21. { title: 'leaf', key: '10010', isLeaf: true },
  22. { title: 'leaf', key: '10011', isLeaf: true },
  23. { title: 'leaf', key: '10012', isLeaf: true }
  24. ]
  25. },
  26. {
  27. title: 'parent 1-1',
  28. key: '1002',
  29. children: [{ title: 'leaf', key: '10020', isLeaf: true }]
  30. },
  31. {
  32. title: 'parent 1-2',
  33. key: '1003',
  34. children: [{ title: 'leaf', key: '10030', isLeaf: true }, { title: 'leaf', key: '10031', isLeaf: true }]
  35. }
  36. ]
  37. }
  38. ];
  39. nzEvent(event: NzFormatEmitEvent): void {
  40. console.log(event);
  41. }
  42. ngOnInit(): void {}
  43. }

Tree树形控件 - 图5

目录

自定义目录树(使用nzTreeTemplate实现,支持右键)。

  1. import { Component, TemplateRef } from '@angular/core';
  2. import { NzDropdownContextComponent, NzDropdownService, NzFormatEmitEvent, NzTreeNode } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-directory',
  5. template: `
  6. <nz-tree [nzData]="nodes" (nzClick)="activeNode($event)" (nzDblClick)="openFolder($event)">
  7. <ng-template #contextTemplate>
  8. <ul nz-menu nzInDropDown>
  9. <li nz-menu-item (click)="selectDropdown()">Action 1</li>
  10. <li nz-menu-item (click)="selectDropdown()">Action 2</li>
  11. </ul>
  12. </ng-template>
  13. <ng-template #nzTreeTemplate let-node>
  14. <span class="custom-node" [class.active]="activedNode?.key === node.key">
  15. <span *ngIf="!node.isLeaf" (contextmenu)="contextMenu($event, contextTemplate)">
  16. <i nz-icon [type]="node.isExpanded ? 'folder-open' : 'folder'" (click)="openFolder(node)"></i>
  17. <span class="folder-name">{{ node.title }}</span>
  18. <span class="folder-desc">created by {{ node?.origin?.author | lowercase }}</span>
  19. </span>
  20. <span *ngIf="node.isLeaf" (contextmenu)="contextMenu($event, contextTemplate)">
  21. <i nz-icon type="file"></i>
  22. <span class="file-name">{{ node.title }}</span>
  23. <span class="file-desc">modified by {{ node?.origin?.author | lowercase }}</span>
  24. </span>
  25. </span>
  26. </ng-template>
  27. </nz-tree>
  28. `,
  29. styles: [
  30. `
  31. :host ::ng-deep .ant-tree {
  32. overflow: hidden;
  33. margin: 0 -24px;
  34. padding: 0 24px;
  35. }
  36. :host ::ng-deep .ant-tree li {
  37. padding: 4px 0 0 0;
  38. }
  39. .custom-node {
  40. cursor: pointer;
  41. line-height: 24px;
  42. margin-left: 4px;
  43. display: inline-block;
  44. margin: 0 -1000px;
  45. padding: 0 1000px;
  46. }
  47. .active {
  48. background: #1890ff;
  49. color: #fff;
  50. }
  51. .file-name,
  52. .folder-name {
  53. margin-left: 4px;
  54. }
  55. .file-desc,
  56. .folder-desc {
  57. padding: 0 8px;
  58. display: inline-block;
  59. background: #87ceff;
  60. color: #ffffff;
  61. position: relative;
  62. left: 12px;
  63. }
  64. `
  65. ]
  66. })
  67. export class NzDemoTreeDirectoryComponent {
  68. dropdown: NzDropdownContextComponent;
  69. // actived node
  70. activedNode: NzTreeNode;
  71. nodes = [
  72. {
  73. title: 'parent 0',
  74. key: '100',
  75. author: 'NG ZORRO',
  76. expanded: true,
  77. children: [
  78. { title: 'leaf 0-0', key: '1000', author: 'NG ZORRO', isLeaf: true },
  79. { title: 'leaf 0-1', key: '1001', author: 'NG ZORRO', isLeaf: true }
  80. ]
  81. },
  82. {
  83. title: 'parent 1',
  84. key: '101',
  85. author: 'NG ZORRO',
  86. children: [
  87. { title: 'leaf 1-0', key: '1010', author: 'NG ZORRO', isLeaf: true },
  88. { title: 'leaf 1-1', key: '1011', author: 'NG ZORRO', isLeaf: true }
  89. ]
  90. }
  91. ];
  92. openFolder(data: NzTreeNode | Required<NzFormatEmitEvent>): void {
  93. // do something if u want
  94. if (data instanceof NzTreeNode) {
  95. data.isExpanded = !data.isExpanded;
  96. } else {
  97. const node = data.node;
  98. if (node) {
  99. node.isExpanded = !node.isExpanded;
  100. }
  101. }
  102. }
  103. activeNode(data: NzFormatEmitEvent): void {
  104. this.activedNode = data.node!;
  105. }
  106. contextMenu($event: MouseEvent, template: TemplateRef<void>): void {
  107. this.dropdown = this.nzDropdownService.create($event, template);
  108. }
  109. selectDropdown(): void {
  110. this.dropdown.close();
  111. // do something
  112. }
  113. constructor(private nzDropdownService: NzDropdownService) {}
  114. }

Tree树形控件 - 图6

受控操作示例

受控操作示例(默认)。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatEmitEvent } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-basic-controlled',
  5. template: `
  6. <nz-tree
  7. [nzData]="nodes"
  8. nzCheckable
  9. nzMultiple
  10. [nzCheckedKeys]="defaultCheckedKeys"
  11. [nzExpandedKeys]="defaultExpandedKeys"
  12. [nzSelectedKeys]="defaultSelectedKeys"
  13. (nzClick)="nzEvent($event)"
  14. (nzExpandChange)="nzEvent($event)"
  15. (nzCheckBoxChange)="nzEvent($event)"
  16. >
  17. </nz-tree>
  18. `
  19. })
  20. export class NzDemoTreeBasicControlledComponent implements OnInit {
  21. defaultCheckedKeys = ['0-0-0'];
  22. defaultSelectedKeys = ['0-0-0'];
  23. defaultExpandedKeys = ['0-0', '0-0-0', '0-0-1'];
  24. nodes = [
  25. {
  26. title: '0-0',
  27. key: '0-0',
  28. expanded: true,
  29. children: [
  30. {
  31. title: '0-0-0',
  32. key: '0-0-0',
  33. children: [
  34. { title: '0-0-0-0', key: '0-0-0-0', isLeaf: true },
  35. { title: '0-0-0-1', key: '0-0-0-1', isLeaf: true },
  36. { title: '0-0-0-2', key: '0-0-0-2', isLeaf: true }
  37. ]
  38. },
  39. {
  40. title: '0-0-1',
  41. key: '0-0-1',
  42. children: [
  43. { title: '0-0-1-0', key: '0-0-1-0', isLeaf: true },
  44. { title: '0-0-1-1', key: '0-0-1-1', isLeaf: true },
  45. { title: '0-0-1-2', key: '0-0-1-2', isLeaf: true }
  46. ]
  47. },
  48. {
  49. title: '0-0-2',
  50. key: '0-0-2',
  51. isLeaf: true
  52. }
  53. ]
  54. },
  55. {
  56. title: '0-1',
  57. key: '0-1',
  58. children: [
  59. { title: '0-1-0-0', key: '0-1-0-0', isLeaf: true },
  60. { title: '0-1-0-1', key: '0-1-0-1', isLeaf: true },
  61. { title: '0-1-0-2', key: '0-1-0-2', isLeaf: true }
  62. ]
  63. },
  64. {
  65. title: '0-2',
  66. key: '0-2',
  67. isLeaf: true
  68. }
  69. ];
  70. nzEvent(event: NzFormatEmitEvent): void {
  71. console.log(event);
  72. }
  73. ngOnInit(): void {}
  74. }

Tree树形控件 - 图7

拖动示例-二次校验

nzBeforeDrop: 在拖拽放置事件(drop事件)之前做校验, 例如仅允许拖拽到内部(延时一秒)、放置前结合modal验证等。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatBeforeDropEvent } from 'ng-zorro-antd';
  3. import { of, Observable } from 'rxjs';
  4. import { delay } from 'rxjs/operators';
  5. @Component({
  6. selector: 'nz-demo-tree-draggable-confirm',
  7. template: `
  8. <nz-tree [nzData]="nodes" nzDraggable nzBlockNode [nzBeforeDrop]="beforeDrop"> </nz-tree>
  9. `
  10. })
  11. export class NzDemoTreeDraggableConfirmComponent implements OnInit {
  12. nodes = [
  13. {
  14. title: '0-0',
  15. key: '100',
  16. expanded: true,
  17. children: [
  18. {
  19. title: '0-0-0',
  20. key: '1001',
  21. children: [{ title: '0-0-0-0', key: '10010', isLeaf: true }, { title: '0-0-0-1', key: '10011', isLeaf: true }]
  22. },
  23. {
  24. title: '0-0-1',
  25. key: '1002',
  26. children: [{ title: '0-0-1-0', key: '10020', isLeaf: true }]
  27. }
  28. ]
  29. }
  30. ];
  31. beforeDrop(arg: NzFormatBeforeDropEvent): Observable<boolean> {
  32. // if insert node into another node, wait 1s
  33. if (arg.pos === 0) {
  34. return of(true).pipe(delay(1000));
  35. } else {
  36. return of(false);
  37. }
  38. }
  39. ngOnInit(): void {}
  40. }

Tree树形控件 - 图8

可搜索

可搜索的树。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzFormatEmitEvent } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-tree-search',
  5. template: `
  6. <nz-input-group [nzSuffix]="suffixIcon">
  7. <input type="text" nz-input placeholder="Search" [(ngModel)]="searchValue" />
  8. </nz-input-group>
  9. <ng-template #suffixIcon>
  10. <i nz-icon type="search"></i>
  11. </ng-template>
  12. <nz-tree
  13. [nzData]="nodes"
  14. [nzSearchValue]="searchValue"
  15. (nzClick)="nzEvent($event)"
  16. (nzExpandChange)="nzEvent($event)"
  17. (nzSearchValueChange)="nzEvent($event)"
  18. >
  19. </nz-tree>
  20. `,
  21. styles: [
  22. `
  23. nz-input-group {
  24. padding: 10px 0;
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoTreeSearchComponent implements OnInit {
  30. searchValue = '';
  31. nodes = [
  32. {
  33. title: '0-0',
  34. key: '0-0',
  35. children: [
  36. {
  37. title: '0-0-0',
  38. key: '0-0-0',
  39. children: [
  40. { title: '0-0-0-0', key: '0-0-0-0', isLeaf: true },
  41. { title: '0-0-0-1', key: '0-0-0-1', isLeaf: true },
  42. { title: '0-0-0-2', key: '0-0-0-2', isLeaf: true }
  43. ]
  44. },
  45. {
  46. title: '0-0-1',
  47. key: '0-0-1',
  48. children: [
  49. { title: '0-0-1-0', key: '0-0-1-0', isLeaf: true },
  50. { title: '0-0-1-1', key: '0-0-1-1', isLeaf: true },
  51. { title: '0-0-1-2', key: '0-0-1-2', isLeaf: true }
  52. ]
  53. },
  54. {
  55. title: '0-0-2',
  56. key: '0-0-2',
  57. isLeaf: true
  58. }
  59. ]
  60. },
  61. {
  62. title: '0-1',
  63. key: '0-1',
  64. children: [
  65. { title: '0-1-0-0', key: '0-1-0-0', isLeaf: true },
  66. { title: '0-1-0-1', key: '0-1-0-1', isLeaf: true },
  67. { title: '0-1-0-2', key: '0-1-0-2', isLeaf: true }
  68. ]
  69. },
  70. {
  71. title: '0-2',
  72. key: '0-2',
  73. isLeaf: true
  74. }
  75. ];
  76. nzEvent(event: NzFormatEmitEvent): void {
  77. console.log(event);
  78. }
  79. ngOnInit(): void {}
  80. }

Tree树形控件 - 图9

自定义图标

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

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-tree-customized-icon',
  4. template: `
  5. <nz-tree [nzData]="nodes" nzShowIcon [nzExpandedIcon]="expandedIconTpl">
  6. <ng-template #expandedIconTpl let-node>
  7. <i nz-icon [type]="node.origin.icon" class="ant-tree-switcher-icon"></i>
  8. </ng-template>
  9. </nz-tree>
  10. <nz-tree [nzData]="nodes" nzShowIcon [nzExpandedIcon]="mutiExpandedIconTpl">
  11. <ng-template #mutiExpandedIconTpl let-node>
  12. <i
  13. *ngIf="!node.origin.isLeaf"
  14. nz-icon
  15. [type]="node.isExpanded ? 'folder-open' : 'folder'"
  16. class="ant-tree-switcher-line-icon"
  17. ></i>
  18. <i *ngIf="node.origin.isLeaf" nz-icon type="file" class="ant-tree-switcher-line-icon"></i>
  19. </ng-template>
  20. </nz-tree>
  21. `
  22. })
  23. export class NzDemoTreeCustomizedIconComponent implements OnInit {
  24. nodes = [
  25. {
  26. title: 'parent 1',
  27. key: '100',
  28. expanded: true,
  29. icon: 'anticon anticon-smile-o',
  30. children: [
  31. { title: 'leaf', key: '1001', icon: 'anticon anticon-meh-o', isLeaf: true },
  32. { title: 'leaf', key: '1002', icon: 'anticon anticon-frown-o', isLeaf: true }
  33. ]
  34. }
  35. ];
  36. ngOnInit(): void {}
  37. }

API

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzTreeModule } from 'ng-zorro-antd';

nz-treecomponent

参数说明类型默认值
[nzData]元数据NzTreeNodeOptions[]|NzTreeNode[][]
[nzBlockNode]是否节点占据一行booleanfalse
[nzCheckable]节点前添加 Checkbox 复选框booleanfalse
[nzShowExpand]节点前添加展开图标booleantrue
[nzShowLine]是否展示连接线booleanfalse
[nzExpandedIcon]自定义展开图标TemplateRef<{ $implicit: NzTreeNode }>-
[nzShowIcon]是否展示 TreeNode title 前的图标,没有默认样式booleanfalse
[nzAsyncData]是否异步加载(显示加载状态)booleanfalse
[nzDraggable]设置节点可拖拽(IE>8)booleanfalse
[nzMultiple]支持点选多个节点(节点本身)booleanfalse
[nzHideUnMatched]搜索隐藏未匹配的节点booleanfalse
[nzCheckStrictly]checkable状态下节点选择完全受控(父子节点选中状态不再关联)booleanfalse
[nzExpandAll]默认展开所有树节点booleanfalse
[nzExpandedKeys]展开指定的树节点string[][]
[nzCheckedKeys]指定选中复选框的树节点string[][]
[nzSelectedKeys]指定选中的树节点string[][]
[nzSearchValue]按需筛选树高亮节点(参考可搜索的树),双向绑定stringnull
[nzBeforeDrop]drop前二次校验,允许用户自行决定是否允许放置(confirm: NzFormatBeforeDropEvent) => Observable<boolean>-
(nzClick)点击树节点触发EventEmitter<NzFormatEmitEvent>-
(nzDblClick)双击树节点触发EventEmitter<NzFormatEmitEvent>-
(nzContextMenu)右键树节点触发EventEmitter<NzFormatEmitEvent>-
(nzCheckBoxChange)点击树节点 Checkbox 触发EventEmitter<NzFormatEmitEvent>-
(nzExpandChange)点击展开树节点图标触发EventEmitter<NzFormatEmitEvent>-
(nzSearchValueChange)搜索节点时调用(与nzSearchValue配合使用)EventEmitter<NzFormatEmitEvent>-
(nzOnDragStart)开始拖拽时调用EventEmitter<NzFormatEmitEvent>-
(nzOnDragEnter)dragenter 触发时调用EventEmitter<NzFormatEmitEvent>-
(nzOnDragOver)dragover 触发时调用EventEmitter<NzFormatEmitEvent>-
(nzOnDragLeave)dragleave 触发时调用EventEmitter<NzFormatEmitEvent>-
(nzOnDrop)drop 触发时调用EventEmitter<NzFormatEmitEvent>-
(nzOnDragEnd)dragend 触发时调用EventEmitter<NzFormatEmitEvent>-

方法

方法名说明返回值
getTreeNodes获取组件 NzTreeNode 节点NzTreeNode[]
getTreeNodeByKey按 key 获取 NzTreeNode 节点NzTreeNode
getCheckedNodeList获取组件 checkBox 被点击选中的节点NzTreeNode[]
getSelectedNodeList获取组件被选中的节点NzTreeNode[]
getHalfCheckedNodeList获取组件半选状态节点NzTreeNode[]
getExpandedNodeList获取组件展开状态节点NzTreeNode[]
getMatchedNodeList获取组搜索匹配到的节点NzTreeNode[]

NzTreeNodeOptions props

参数说明类型默认值
title标题string-
key整个树范围内的所有节点的 key 值不能重复且不为空!string-
icon节点前的图标,与 nzShowIcon 组合使用string-
children子节点NzTreeNodeOptions[]-
isLeaf设置为叶子节点(叶子节点不可被拖拽模式放置)booleanfalse
checked设置节点 Checkbox 是否选中booleanfalse
selected设置节点本身是否选中booleanfalse
expanded设置节点是否展开(叶子节点无效)booleanfalse
selectable设置节点是否可被选中booleantrue
disabled设置是否禁用节点(不可进行任何操作)booleanfalse
disableCheckbox设置节点禁用 Checkboxbooleanfalse
[key: string]自定义数据,可通过 NzTreeNode 的 origin 字段获取any-

NzFormatEmitEvent props

属性说明类型默认值
eventName事件名enum: clickdblclickcontextmenucheckexpandsearch & dragstartdragenterdragoverdragleavedropdragend-
node当前操作节点(拖拽时表示目标节点)NzTreeNodenull
event原生事件'MouseEvent'|'DragEvent'null
dragNode?当前拖拽节点(拖拽时存在)NzTreeNodenull
selectedKeys?已选中的节点key(单击时存在)NzTreeNode[][]
checkedKeys?checkBox 已选中的节点key(点击 checkBox 存在)NzTreeNode[][]
matchedKeys?搜索时匹配到的节点keyNzTreeNode[][]
keys?非拖拽事件相关的全部节点的key数组string[][]
nodes?非拖拽事件相关的全部节点NzTreeNode[][]

NzFormatBeforeDropEvent props

属性说明类型默认值
dragNode当前拖拽节点(拖拽时存在)NzTreeNode-
node当前操作节点(拖拽时表示目标节点)NzTreeNode-
pos放置位置(-1:目标节点前面, 0: 目标节点内部, 1: 目标节点后面)number-

NzTreeNode props

属性说明类型默认值
title标题stringNzTreeNodeOptions.title
keykey值stringNzTreeNodeOptions.key
level层级(最顶层为0,子节点逐层加1)numbernumber
children子节点NzTreeNode[][]
origin原始节点树结构(用户提供,用于展示额外信息)NzTreeNodeOptions-
getParentNode获取父节点functionnull
isLeaf是否为叶子节点booleanfalse
isExpanded是否已展开booleanfalse
isDisabled是否禁用booleanfalse
isDisableCheckbox是否禁用 checkBoxbooleanfalse
isSelectable是否可选中booleantrue
isChecked是否选中 checkBoxbooleanfalse
isHalfChecked子节点有选中但未全选booleanfalse
isSelected是否已选中booleanfalse
isLoading是否异步加载状态(影响展开图标展示)booleanfalse
isMatchedtitle是否包含nzSearchValue(搜索使用)booleanfalse
setSyncChecked设置 checked 状态并同步其他节点状态function-
getChildren获取子节点,返回NzTreeNode数组function-
addChildren添加子节点,接收NzTreeNode或NzTreeNodeOptions数组,第二个参数为插入的索引位置,默认插入末尾(children: array, index?: number )=>{}-
clearChildren清除子节点function-
remove清除当前节点(非根节点)function-

注意

  • NzTreeNodeOptions 可以接受用户自定义属性,可通过 NzTreeNodeorigin 属性取得。
  • 使用 ViewChild 时,Tree 方法需要在 ngAfterViewInit 中调用。
  • nzData 属性请传递 NzTreeNodeOptions 数组,传递 NzTreeNode 数组模式将在 8.x 版本取消兼容。