• TreeSelect树选择
    • 何时使用
    • 代码演示
    • API
      • 单独引入此组件
      • nz-tree-selectcomponent
        • 方法

    TreeSelect树选择

    树型选择控件。

    何时使用

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

    代码演示

    TreeSelect树选择 - 图1

    基本

    最简单的用法。

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-tree-select-basic',
    4. template: `
    5. <nz-tree-select
    6. style="width: 250px"
    7. [nzDefaultExpandedKeys]="expandKeys"
    8. [nzNodes]="nodes"
    9. nzShowSearch
    10. nzPlaceHolder="Please select"
    11. [(ngModel)]="value"
    12. (ngModelChange)="onChange($event)"
    13. >
    14. </nz-tree-select>
    15. `
    16. })
    17. export class NzDemoTreeSelectBasicComponent implements OnInit {
    18. expandKeys = ['100', '1001'];
    19. value: string;
    20. nodes = [
    21. {
    22. title: 'parent 1',
    23. key: '100',
    24. children: [
    25. {
    26. title: 'parent 1-0',
    27. key: '1001',
    28. children: [
    29. { title: 'leaf 1-0-0', key: '10010', isLeaf: true },
    30. { title: 'leaf 1-0-1', key: '10011', isLeaf: true }
    31. ]
    32. },
    33. {
    34. title: 'parent 1-1',
    35. key: '1002',
    36. children: [{ title: 'leaf 1-1-0', key: '10020', isLeaf: true }]
    37. }
    38. ]
    39. }
    40. ];
    41. onChange($event: string): void {
    42. console.log($event);
    43. }
    44. ngOnInit(): void {
    45. // mock async
    46. setTimeout(() => {
    47. this.value = '1001';
    48. }, 1000);
    49. }
    50. }

    TreeSelect树选择 - 图2

    异步数据加载

    点击展开节点,动态加载数据,直到执行 addChildren() 方法取消加载状态。

    1. import { Component, OnInit } from '@angular/core';
    2. import { NzFormatEmitEvent } from 'ng-zorro-antd';
    3. @Component({
    4. selector: 'nz-demo-tree-select-async',
    5. template: `
    6. <nz-tree-select
    7. style="width: 250px"
    8. nzPlaceHolder="Please select"
    9. [nzDefaultExpandedKeys]="expandKeys"
    10. [nzDropdownMatchSelectWidth]="true"
    11. [nzDropdownStyle]="{ 'max-height': '300px' }"
    12. [(ngModel)]="value"
    13. [nzNodes]="nodes"
    14. [nzAsyncData]="true"
    15. (nzExpandChange)="onExpandChange($event)"
    16. >
    17. </nz-tree-select>
    18. `
    19. })
    20. export class NzDemoTreeSelectAsyncComponent implements OnInit {
    21. expandKeys = ['0-0'];
    22. value: string;
    23. nodes = [
    24. {
    25. title: 'Node1',
    26. value: '0-0',
    27. key: '0-0',
    28. children: [
    29. {
    30. title: 'Child Node1',
    31. value: '0-0-1',
    32. key: '0-0-1'
    33. },
    34. {
    35. title: 'Child Node2',
    36. value: '0-0-2',
    37. key: '0-0-2'
    38. }
    39. ]
    40. },
    41. {
    42. title: 'Node2',
    43. value: '0-1',
    44. key: '0-1'
    45. }
    46. ];
    47. onExpandChange(e: Required<NzFormatEmitEvent>): void {
    48. const node = e.node;
    49. if (node && node.getChildren().length === 0 && node.isExpanded) {
    50. this.loadNode().then(data => {
    51. node.addChildren(data);
    52. });
    53. }
    54. }
    55. loadNode(): Promise<any[]> {
    56. return new Promise(resolve => {
    57. setTimeout(
    58. () =>
    59. resolve([
    60. { title: 'Child Node', key: `${new Date().getTime()}-0` },
    61. { title: 'Child Node', key: `${new Date().getTime()}-1` }
    62. ]),
    63. 1000
    64. );
    65. });
    66. }
    67. ngOnInit(): void {}
    68. }

    TreeSelect树选择 - 图3

    多选

    多选的树选择,例子中通过 nzMaxTagCount 限制最多显示3个选项。

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-tree-select-multiple',
    4. template: `
    5. <nz-tree-select
    6. style="width: 250px"
    7. nzPlaceHolder="Please select"
    8. [nzMaxTagCount]="3"
    9. [nzMaxTagPlaceholder]="omittedPlaceHolder"
    10. [nzNodes]="nodes"
    11. [nzDefaultExpandAll]="true"
    12. [nzAllowClear]="false"
    13. [(ngModel)]="value"
    14. [nzMultiple]="true"
    15. (ngModelChange)="onChange($event)"
    16. >
    17. </nz-tree-select>
    18. <ng-template #omittedPlaceHolder let-omittedValues> and {{ omittedValues.length }} more... </ng-template>
    19. `
    20. })
    21. export class NzDemoTreeSelectMultipleComponent implements OnInit {
    22. value: string[] = [];
    23. nodes = [
    24. {
    25. title: 'parent 1',
    26. key: '100',
    27. children: [
    28. {
    29. title: 'parent 1-0',
    30. key: '1001',
    31. children: [
    32. { title: 'leaf 1-0-0', key: '10010', isLeaf: true },
    33. { title: 'leaf 1-0-1', key: '10011', isLeaf: true }
    34. ]
    35. },
    36. {
    37. title: 'parent 1-1',
    38. key: '1002',
    39. children: [{ title: 'leaf 1-1-0', key: '10020', isLeaf: true }]
    40. }
    41. ]
    42. }
    43. ];
    44. onChange($event: string[]): void {
    45. console.log($event);
    46. }
    47. ngOnInit(): void {}
    48. }

    TreeSelect树选择 - 图4

    可勾选

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

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-tree-select-checkable',
    4. template: `
    5. <nz-tree-select
    6. style="width: 250px"
    7. [nzNodes]="nodes"
    8. nzShowSearch
    9. nzCheckable
    10. nzPlaceHolder="Please select"
    11. [(ngModel)]="value"
    12. (ngModelChange)="onChange($event)"
    13. >
    14. </nz-tree-select>
    15. `
    16. })
    17. export class NzDemoTreeSelectCheckableComponent implements OnInit {
    18. value: string[] = ['0-0-0'];
    19. nodes = [
    20. {
    21. title: 'Node1',
    22. value: '0-0',
    23. key: '0-0',
    24. children: [
    25. {
    26. title: 'Child Node1',
    27. value: '0-0-0',
    28. key: '0-0-0',
    29. isLeaf: true
    30. }
    31. ]
    32. },
    33. {
    34. title: 'Node2',
    35. value: '0-1',
    36. key: '0-1',
    37. children: [
    38. {
    39. title: 'Child Node3',
    40. value: '0-1-0',
    41. key: '0-1-0',
    42. isLeaf: true
    43. },
    44. {
    45. title: 'Child Node4',
    46. value: '0-1-1',
    47. key: '0-1-1',
    48. isLeaf: true
    49. },
    50. {
    51. title: 'Child Node5',
    52. value: '0-1-2',
    53. key: '0-1-2',
    54. isLeaf: true
    55. }
    56. ]
    57. }
    58. ];
    59. onChange($event: string[]): void {
    60. console.log($event);
    61. }
    62. ngOnInit(): void {}
    63. }

    API

    单独引入此组件

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

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

    nz-tree-selectcomponent

    参数说明类型默认值
    [nzAllowClear]显示清除按钮booleanfalse
    [nzPlaceHolder]选择框默认文字string-
    [nzDisabled]禁用选择器booleanfalse
    [nzShowSearch]显示搜索框booleanfalse
    [nzNotFoundContent]当下拉列表为空时显示的内容string-
    [nzDropdownMatchSelectWidth]下拉菜单和选择器同宽booleantrue
    [nzDropdownStyle]下拉菜单的样式{ [key: string]: string; }-
    [nzMultiple]支持多选(当设置 nzCheckable 时自动变为true)booleanfalse
    [nzSize]选择框大小'large'|'small'|'default''default'
    [nzCheckable]节点前添加 Checkbox 复选框booleanfalse
    [nzShowExpand]节点前添加展开图标booleantrue
    [nzShowLine]是否展示连接线booleanfalse
    [nzAsyncData]是否异步加载(显示加载状态)booleanfalse
    [nzNodes]treeNodes 数据NzTreeNodeOptions[][]
    [nzDefaultExpandAll]默认展开所有树节点booleanfalse
    [nzDefaultExpandedKeys]默认展开指定的树节点string[]-
    [nzDisplayWith]如何在输入框显示所选的节点值的方法(node: NzTreeNode) => string(node: NzTreeNode) => node.title
    [nzMaxTagCount]最多显示多少个 tagnumber-
    [nzMaxTagPlaceholder]隐藏 tag 时显示的内容TemplateRef<{ $implicit: NzTreeNode[] }>-
    (nzExpandChange)点击展开树节点图标调用EventEmitter<NzFormatEmitEvent>-

    方法

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