• Tree树形控件
    • 何时使用
    • 单独引入此组件
    • 代码演示
    • API
      • nz-treecomponent
        • 方法
        • NzTreeNodeOptions props
        • NzFormatEmitEvent props
        • NzFormatBeforeDropEvent props
        • NzTreeNode props
    • 注意

    Tree树形控件

    何时使用

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

    单独引入此组件

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

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

    代码演示

    Tree树形控件 - 图1

    基本

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

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

    Tree树形控件 - 图2

    拖动示例

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

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

    Tree树形控件 - 图6

    受控操作示例

    受控操作示例(默认)。

    1. import { Component, OnInit } from '@angular/core';
    2. import { NzFormatEmitEvent } from 'ng-zorro-antd/core';
    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/core';
    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/core';
    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 nzType="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 [nzType]="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. [nzType]="node.isExpanded ? 'folder-open' : 'folder'"
    16. class="ant-tree-switcher-line-icon"
    17. ></i>
    18. <i *ngIf="node.origin.isLeaf" nz-icon nzType="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

    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 版本取消兼容。