Cascader级联选择

级联选择框。

何时使用

  • 需要从一组相关联的数据集合进行选择,例如省市区,公司层级,事物分类等。
  • 从一个较大的数据集合中进行选择时,用多级分类进行分隔,方便选择。
  • 比起 Select 组件,可以在同一个浮层中完成选择,有较好的体验。
  1. import { NzCascaderModule } from 'ng-zorro-antd/cascader';

代码演示

Cascader级联选择 - 图1

基本

省市区级联。

  1. // tslint:disable:no-any
  2. import { Component, OnInit } from '@angular/core';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. const otherOptions = [
  45. {
  46. value: 'fujian',
  47. label: 'Fujian',
  48. children: [
  49. {
  50. value: 'xiamen',
  51. label: 'Xiamen',
  52. children: [
  53. {
  54. value: 'Kulangsu',
  55. label: 'Kulangsu',
  56. isLeaf: true
  57. }
  58. ]
  59. }
  60. ]
  61. },
  62. {
  63. value: 'guangxi',
  64. label: 'Guangxi',
  65. children: [
  66. {
  67. value: 'guilin',
  68. label: 'Guilin',
  69. children: [
  70. {
  71. value: 'Lijiang',
  72. label: 'Li Jiang River',
  73. isLeaf: true
  74. }
  75. ]
  76. }
  77. ]
  78. }
  79. ];
  80. @Component({
  81. selector: 'nz-demo-cascader-basic',
  82. template: `
  83. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"></nz-cascader>
  84. &nbsp;
  85. <a href="javascript:;" (click)="changeNzOptions()" class="change-options">
  86. Change Options
  87. </a>
  88. `,
  89. styles: [
  90. `
  91. .change-options {
  92. display: inline-block;
  93. font-size: 12px;
  94. margin-top: 8px;
  95. }
  96. `
  97. ]
  98. })
  99. export class NzDemoCascaderBasicComponent implements OnInit {
  100. nzOptions: any[] | null = null;
  101. values: any[] | null = null;
  102. ngOnInit(): void {
  103. setTimeout(() => {
  104. this.nzOptions = options;
  105. }, 100);
  106. }
  107. changeNzOptions(): void {
  108. if (this.nzOptions === options) {
  109. this.nzOptions = otherOptions;
  110. } else {
  111. this.nzOptions = options;
  112. }
  113. }
  114. onChanges(values: any): void {
  115. console.log(values, this.values);
  116. }
  117. }

Cascader级联选择 - 图2

可以自定义显示

切换按钮和结果分开。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-trigger',
  46. template: `
  47. {{ text }}
  48. <nz-cascader
  49. [nzShowInput]="false"
  50. [nzOptions]="nzOptions"
  51. [(ngModel)]="values"
  52. (ngModelChange)="onChanges($event)"
  53. (nzSelectionChange)="onSelectionChange($event)"
  54. >
  55. <a href="javascript: void(0)">Change city</a>
  56. </nz-cascader>
  57. `
  58. })
  59. export class NzDemoCascaderTriggerComponent {
  60. nzOptions: NzCascaderOption[] = options;
  61. values: string[] | null = null;
  62. text = 'Unselect';
  63. onChanges(values: string[]): void {
  64. console.log(values, this.values);
  65. }
  66. onSelectionChange(selectedOptions: NzCascaderOption[]): void {
  67. this.text = selectedOptions.map(o => o.label).join(', ');
  68. }
  69. }

Cascader级联选择 - 图3

禁用选项

通过指定 options 里的 disabled 字段。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. disabled: true,
  30. children: [
  31. {
  32. value: 'nanjing',
  33. label: 'Nanjing',
  34. children: [
  35. {
  36. value: 'zhonghuamen',
  37. label: 'Zhong Hua Men',
  38. isLeaf: true
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. ];
  45. @Component({
  46. selector: 'nz-demo-cascader-disabled',
  47. template: `
  48. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
  49. `
  50. })
  51. export class NzDemoCascaderDisabledComponent {
  52. nzOptions: NzCascaderOption[] = options;
  53. values: string[] | null = null;
  54. onChanges(values: string[]): void {
  55. console.log(values, this.values);
  56. }
  57. }

Cascader级联选择 - 图4

大小

不同大小的级联选择器。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-size',
  46. template: `
  47. <nz-cascader [nzSize]="'large'" [nzOptions]="nzOptions" [(ngModel)]="value1" (ngModelChange)="onChanges($event)"> </nz-cascader>
  48. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="value2" (ngModelChange)="onChanges($event)"> </nz-cascader>
  49. <nz-cascader [nzSize]="'small'" [nzOptions]="nzOptions" [(ngModel)]="value3" (ngModelChange)="onChanges($event)"> </nz-cascader>
  50. `,
  51. styles: [
  52. `
  53. .ant-cascader-picker {
  54. width: 300px;
  55. margin-bottom: 8px;
  56. }
  57. `
  58. ]
  59. })
  60. export class NzDemoCascaderSizeComponent {
  61. nzOptions: NzCascaderOption[] = options;
  62. value1: string[] | null = null;
  63. value2: string[] | null = null;
  64. value3: string[] | null = null;
  65. onChanges(values: string[]): void {
  66. console.log(values);
  67. }
  68. }

Cascader级联选择 - 图5

响应表单

通过表单的重置功能清空已选择的值。

  1. import { Component, OnDestroy } from '@angular/core';
  2. import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  3. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  4. import { Subscription } from 'rxjs';
  5. const options = [
  6. {
  7. value: 'zhejiang',
  8. label: 'Zhejiang',
  9. children: [
  10. {
  11. value: 'hangzhou',
  12. label: 'Hangzhou',
  13. children: [
  14. {
  15. value: 'xihu',
  16. label: 'West Lake',
  17. isLeaf: true
  18. }
  19. ]
  20. },
  21. {
  22. value: 'ningbo',
  23. label: 'Ningbo',
  24. isLeaf: true
  25. }
  26. ]
  27. },
  28. {
  29. value: 'jiangsu',
  30. label: 'Jiangsu',
  31. children: [
  32. {
  33. value: 'nanjing',
  34. label: 'Nanjing',
  35. children: [
  36. {
  37. value: 'zhonghuamen',
  38. label: 'Zhong Hua Men',
  39. isLeaf: true
  40. }
  41. ]
  42. }
  43. ]
  44. }
  45. ];
  46. @Component({
  47. selector: 'nz-demo-cascader-reactive-form',
  48. template: `
  49. <form [formGroup]="form" novalidate>
  50. <nz-cascader [nzOptions]="nzOptions" [formControlName]="'name'"> </nz-cascader>
  51. </form>
  52. <br />
  53. <button nz-button (click)="reset()">Reset</button>
  54. <button nz-button (click)="submit()">Submit</button>
  55. `,
  56. styles: [
  57. `
  58. button {
  59. margin-right: 8px;
  60. }
  61. `
  62. ]
  63. })
  64. export class NzDemoCascaderReactiveFormComponent implements OnDestroy {
  65. form!: FormGroup;
  66. nzOptions: NzCascaderOption[] = options;
  67. changeSubscription: Subscription;
  68. constructor(private fb: FormBuilder) {
  69. this.createForm();
  70. const control = this.form.get('name') as FormControl;
  71. this.changeSubscription = control.valueChanges.subscribe(data => {
  72. this.onChanges(data);
  73. });
  74. }
  75. private createForm(): void {
  76. this.form = this.fb.group({
  77. name: [null, Validators.required]
  78. });
  79. }
  80. reset(): void {
  81. this.form.reset();
  82. console.log(this.form.value);
  83. }
  84. submit(): void {
  85. console.log(this.form.value);
  86. }
  87. onChanges(values: string[]): void {
  88. console.log(values);
  89. }
  90. ngOnDestroy(): void {
  91. this.changeSubscription.unsubscribe();
  92. }
  93. }

Cascader级联选择 - 图6

指定选择

通过函数来判断选项是否可以选择。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. children: [
  23. {
  24. value: 'dongqianlake',
  25. label: 'Dongqian Lake',
  26. isLeaf: true
  27. }
  28. ]
  29. }
  30. ]
  31. },
  32. {
  33. value: 'jiangsu',
  34. label: 'Jiangsu',
  35. children: [
  36. {
  37. value: 'nanjing',
  38. label: 'Nanjing',
  39. children: [
  40. {
  41. value: 'zhonghuamen',
  42. label: 'Zhong Hua Men',
  43. isLeaf: true
  44. }
  45. ]
  46. }
  47. ]
  48. }
  49. ];
  50. @Component({
  51. selector: 'nz-demo-cascader-change-on-function',
  52. template: `
  53. <nz-cascader [nzChangeOn]="validate" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
  54. `
  55. })
  56. export class NzDemoCascaderChangeOnFunctionComponent {
  57. nzOptions: NzCascaderOption[] = options;
  58. values: string[] | null = null;
  59. onChanges(values: string[]): void {
  60. console.log(values, this.values);
  61. }
  62. validate(option: NzCascaderOption, _index: number): boolean {
  63. const value = option.value as string;
  64. return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
  65. }
  66. }

Cascader级联选择 - 图7

搜索

可以直接搜索选项并选择。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true,
  23. disabled: true
  24. }
  25. ]
  26. },
  27. {
  28. value: 'jiangsu',
  29. label: 'Jiangsu',
  30. children: [
  31. {
  32. value: 'nanjing',
  33. label: 'Nanjing',
  34. children: [
  35. {
  36. value: 'zhonghuamen',
  37. label: 'Zhong Hua Men',
  38. isLeaf: true
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. ];
  45. const otherOptions = [
  46. {
  47. value: 'fujian',
  48. label: 'Fujian',
  49. children: [
  50. {
  51. value: 'xiamen',
  52. label: 'Xiamen',
  53. children: [
  54. {
  55. value: 'Kulangsu',
  56. label: 'Kulangsu',
  57. isLeaf: true
  58. }
  59. ]
  60. }
  61. ]
  62. },
  63. {
  64. value: 'guangxi',
  65. label: 'Guangxi',
  66. children: [
  67. {
  68. value: 'guilin',
  69. label: 'Guilin',
  70. children: [
  71. {
  72. value: 'Lijiang',
  73. label: 'Li Jiang River',
  74. isLeaf: true
  75. }
  76. ]
  77. }
  78. ]
  79. }
  80. ];
  81. @Component({
  82. selector: 'nz-demo-cascader-search',
  83. template: `
  84. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" [nzShowSearch]="true" (ngModelChange)="onChanges($event)"> </nz-cascader>
  85. &nbsp;
  86. <a href="javascript:;" (click)="changeNzOptions()" class="change-options">
  87. Change Options
  88. </a>
  89. `,
  90. styles: [
  91. `
  92. .change-options {
  93. display: inline-block;
  94. font-size: 12px;
  95. margin-top: 8px;
  96. }
  97. `
  98. ]
  99. })
  100. export class NzDemoCascaderSearchComponent implements OnInit {
  101. nzOptions: NzCascaderOption[] | null = null;
  102. values: string[] | null = null;
  103. ngOnInit(): void {
  104. setTimeout(() => {
  105. this.nzOptions = options;
  106. }, 100);
  107. }
  108. changeNzOptions(): void {
  109. if (this.nzOptions === options) {
  110. this.nzOptions = otherOptions;
  111. } else {
  112. this.nzOptions = options;
  113. }
  114. }
  115. onChanges(values: string[]): void {
  116. console.log(values, this.values);
  117. }
  118. }

Cascader级联选择 - 图8

默认值与延迟加载

默认值通过数组的方式指定,但nzOptions没有赋值,通过nzLoadData函数延迟加载。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const provinces = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang'
  7. },
  8. {
  9. value: 'jiangsu',
  10. label: 'Jiangsu'
  11. }
  12. ];
  13. const cities: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
  14. zhejiang: [
  15. {
  16. value: 'hangzhou',
  17. label: 'Hangzhou'
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ],
  25. jiangsu: [
  26. {
  27. value: 'nanjing',
  28. label: 'Nanjing'
  29. }
  30. ]
  31. };
  32. const scenicspots: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
  33. hangzhou: [
  34. {
  35. value: 'xihu',
  36. label: 'West Lake',
  37. isLeaf: true
  38. }
  39. ],
  40. nanjing: [
  41. {
  42. value: 'zhonghuamen',
  43. label: 'Zhong Hua Men',
  44. isLeaf: true
  45. }
  46. ]
  47. };
  48. @Component({
  49. selector: 'nz-demo-cascader-default-value-and-lazyload',
  50. template: ` <nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)"> </nz-cascader> `
  51. })
  52. export class NzDemoCascaderDefaultValueAndLazyloadComponent {
  53. values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
  54. onChanges(values: string[]): void {
  55. console.log(values, this.values);
  56. }
  57. /** load data async execute by `nzLoadData` method */
  58. loadData(node: NzCascaderOption, index: number): PromiseLike<void> {
  59. return new Promise(resolve => {
  60. setTimeout(() => {
  61. if (index < 0) {
  62. // if index less than 0 it is root node
  63. node.children = provinces;
  64. } else if (index === 0) {
  65. node.children = cities[node.value];
  66. } else {
  67. node.children = scenicspots[node.value];
  68. }
  69. resolve();
  70. }, 1000);
  71. });
  72. }
  73. }

Cascader级联选择 - 图9

自定义字段名

自定义字段名。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. code: 'zhejiang',
  6. name: 'Zhejiang',
  7. children: [
  8. {
  9. code: 'hangzhou',
  10. name: 'Hangzhou',
  11. children: [
  12. {
  13. code: 'xihu',
  14. name: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. code: 'ningbo',
  21. name: 'Ningbo',
  22. children: [
  23. {
  24. code: 'dongqianlake',
  25. name: 'Dongqian Lake',
  26. isLeaf: true
  27. }
  28. ]
  29. }
  30. ]
  31. },
  32. {
  33. code: 'jiangsu',
  34. name: 'Jiangsu',
  35. children: [
  36. {
  37. code: 'nanjing',
  38. name: 'Nanjing',
  39. children: [
  40. {
  41. code: 'zhonghuamen',
  42. name: 'Zhong Hua Men',
  43. isLeaf: true
  44. }
  45. ]
  46. }
  47. ]
  48. }
  49. ];
  50. @Component({
  51. selector: 'nz-demo-cascader-custom-field-names',
  52. template: `
  53. <nz-cascader
  54. [nzChangeOn]="validate"
  55. [nzOptions]="nzOptions"
  56. [nzLabelProperty]="'name'"
  57. [nzValueProperty]="'code'"
  58. [nzShowSearch]="true"
  59. [(ngModel)]="values"
  60. (ngModelChange)="onChanges($event)"
  61. >
  62. </nz-cascader>
  63. `
  64. })
  65. export class NzDemoCascaderCustomFieldNamesComponent {
  66. nzOptions: NzCascaderOption[] = options;
  67. values: string[] | null = null;
  68. onChanges(values: string[]): void {
  69. console.log(values, this.values);
  70. }
  71. validate(option: NzCascaderOption, _index: number): boolean {
  72. const value = option.value as string;
  73. return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
  74. }
  75. }

Cascader级联选择 - 图10

默认值

默认值通过数组的方式指定。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-default-value',
  46. template: `
  47. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
  48. `
  49. })
  50. export class NzDemoCascaderDefaultValueComponent {
  51. nzOptions: NzCascaderOption[] = options;
  52. values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
  53. /* // or like this:
  54. values: any[] = [{
  55. value: 'zhejiang',
  56. label: 'Zhejiang'
  57. }, {
  58. value: 'hangzhou',
  59. label: 'Hangzhou'
  60. }, {
  61. value: 'xihu',
  62. label: 'West Lake'
  63. }]; */
  64. onChanges(values: string[]): void {
  65. console.log(values, this.values);
  66. }
  67. }

Cascader级联选择 - 图11

移入展开

通过移入展开下级菜单,点击完成选择。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-hover',
  46. template: `
  47. <nz-cascader [nzExpandTrigger]="'hover'" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
  48. </nz-cascader>
  49. `
  50. })
  51. export class NzDemoCascaderHoverComponent {
  52. nzOptions: NzCascaderOption[] = options;
  53. values: string[] | null = null;
  54. onChanges(values: string[]): void {
  55. console.log(values, this.values);
  56. }
  57. }

Cascader级联选择 - 图12

选择即改变

这种交互允许只选中父级选项。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-change-on-select',
  46. template: `
  47. <nz-cascader nzChangeOnSelect [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
  48. `
  49. })
  50. export class NzDemoCascaderChangeOnSelectComponent {
  51. nzOptions: NzCascaderOption[] = options;
  52. values: string[] | null = null;
  53. onChanges(values: string[]): void {
  54. console.log(values, this.values);
  55. }
  56. }

Cascader级联选择 - 图13

自定义已选项

例如给最后一项加上邮编链接。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. code: 752100,
  16. isLeaf: true
  17. }
  18. ]
  19. },
  20. {
  21. value: 'ningbo',
  22. label: 'Ningbo',
  23. code: '315000',
  24. isLeaf: true
  25. }
  26. ]
  27. },
  28. {
  29. value: 'jiangsu',
  30. label: 'Jiangsu',
  31. children: [
  32. {
  33. value: 'nanjing',
  34. label: 'Nanjing',
  35. children: [
  36. {
  37. value: 'zhonghuamen',
  38. label: 'Zhong Hua Men',
  39. code: 453400,
  40. isLeaf: true
  41. }
  42. ]
  43. }
  44. ]
  45. }
  46. ];
  47. @Component({
  48. selector: 'nz-demo-cascader-custom-render',
  49. template: `
  50. <nz-cascader [nzLabelRender]="renderTpl" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
  51. </nz-cascader>
  52. <ng-template #renderTpl let-labels="labels" let-selectedOptions="selectedOptions">
  53. <ng-container *ngFor="let label of labels; let i = index; let isLast = last">
  54. <span *ngIf="!isLast">{{ label }} / </span>
  55. <span *ngIf="isLast">
  56. {{ label }} (<a href="javascript:;" (click)="handleAreaClick($event, label, selectedOptions[i])">
  57. {{ selectedOptions[i].code }} </a
  58. >)
  59. </span>
  60. </ng-container>
  61. </ng-template>
  62. `
  63. })
  64. export class NzDemoCascaderCustomRenderComponent {
  65. nzOptions: NzCascaderOption[] = options;
  66. values: string[] | null = null;
  67. onChanges(values: string[]): void {
  68. console.log(values, this.values);
  69. }
  70. handleAreaClick(e: Event, label: string, option: NzCascaderOption): void {
  71. e.preventDefault();
  72. e.stopPropagation();
  73. console.log('clicked "', label, '"', option);
  74. }
  75. }

Cascader级联选择 - 图14

动态加载选项

使用 nzLoadData 实现动态加载选项。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const provinces = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang'
  7. },
  8. {
  9. value: 'jiangsu',
  10. label: 'Jiangsu'
  11. }
  12. ];
  13. const cities: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
  14. zhejiang: [
  15. {
  16. value: 'hangzhou',
  17. label: 'Hangzhou'
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ],
  25. jiangsu: [
  26. {
  27. value: 'nanjing',
  28. label: 'Nanjing'
  29. }
  30. ]
  31. };
  32. const scenicspots: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
  33. hangzhou: [
  34. {
  35. value: 'xihu',
  36. label: 'West Lake',
  37. isLeaf: true
  38. }
  39. ],
  40. nanjing: [
  41. {
  42. value: 'zhonghuamen',
  43. label: 'Zhong Hua Men',
  44. isLeaf: true
  45. }
  46. ]
  47. };
  48. @Component({
  49. selector: 'nz-demo-cascader-lazy',
  50. template: `
  51. <nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)"> </nz-cascader>
  52. `
  53. })
  54. export class NzDemoCascaderLazyComponent {
  55. values: string[] | null = null;
  56. onChanges(values: string[]): void {
  57. console.log(values);
  58. }
  59. /** load data async execute by `nzLoadData` method */
  60. loadData(node: NzCascaderOption, index: number): PromiseLike<void> {
  61. return new Promise(resolve => {
  62. setTimeout(() => {
  63. if (index < 0) {
  64. // if index less than 0 it is root node
  65. node.children = provinces;
  66. } else if (index === 0) {
  67. node.children = cities[node.value];
  68. } else {
  69. node.children = scenicspots[node.value];
  70. }
  71. resolve();
  72. }, 1000);
  73. });
  74. }
  75. }

Cascader级联选择 - 图15

模态窗口

在模态窗口中使用级联控件。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-modal',
  46. template: `
  47. <nz-modal [(nzVisible)]="isVisible" nzTitle="Please select" (nzOnCancel)="handleCancel($event)" (nzOnOk)="handleOk($event)">
  48. <nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
  49. </nz-modal>
  50. <button nz-button (click)="open()">Open Dialog</button>
  51. `
  52. })
  53. export class NzDemoCascaderModalComponent {
  54. nzOptions: NzCascaderOption[] = options;
  55. values: string[] | null = null;
  56. isVisible = false;
  57. onChanges(values: string[]): void {
  58. console.log(values, this.values);
  59. }
  60. open(): void {
  61. this.isVisible = true;
  62. }
  63. handleOk($event: MouseEvent): void {
  64. console.log('Button ok clicked!', this.values, $event);
  65. this.isVisible = false;
  66. }
  67. handleCancel($event: MouseEvent): void {
  68. console.log('Button cancel clicked!', $event);
  69. this.isVisible = false;
  70. }
  71. }

Cascader级联选择 - 图16

鼠标移入触发

鼠标移入触发显示菜单,移出隐藏菜单。

  1. import { Component } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-trigger-action',
  46. template: `
  47. <nz-cascader
  48. [nzTriggerAction]="'hover'"
  49. [nzExpandTrigger]="'hover'"
  50. [nzOptions]="nzOptions"
  51. [(ngModel)]="values"
  52. (ngModelChange)="onChanges($event)"
  53. >
  54. </nz-cascader>
  55. `
  56. })
  57. export class NzDemoCascaderTriggerActionComponent {
  58. nzOptions: NzCascaderOption[] = options;
  59. values: string[] | null = null;
  60. onChanges(values: string[]): void {
  61. console.log(values, this.values);
  62. }
  63. }

Cascader级联选择 - 图17

默认值与异步列表

默认值通过数组的方式指定,但nzOptions通过异步加载。

  1. import { Component, OnInit } from '@angular/core';
  2. import { NzCascaderOption } from 'ng-zorro-antd/cascader';
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. isLeaf: true
  16. }
  17. ]
  18. },
  19. {
  20. value: 'ningbo',
  21. label: 'Ningbo',
  22. isLeaf: true
  23. }
  24. ]
  25. },
  26. {
  27. value: 'jiangsu',
  28. label: 'Jiangsu',
  29. children: [
  30. {
  31. value: 'nanjing',
  32. label: 'Nanjing',
  33. children: [
  34. {
  35. value: 'zhonghuamen',
  36. label: 'Zhong Hua Men',
  37. isLeaf: true
  38. }
  39. ]
  40. }
  41. ]
  42. }
  43. ];
  44. @Component({
  45. selector: 'nz-demo-cascader-default-value-and-asyn-options',
  46. template: `
  47. <nz-cascader [(ngModel)]="values" [nzOptions]="nzOptions" (ngModelChange)="onChanges($event)"> </nz-cascader>
  48. `
  49. })
  50. export class NzDemoCascaderDefaultValueAndAsynOptionsComponent implements OnInit {
  51. nzOptions: NzCascaderOption[] | null = null;
  52. values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
  53. onChanges(values: string[]): void {
  54. console.log(values, this.values);
  55. }
  56. ngOnInit(): void {
  57. setTimeout(() => {
  58. this.nzOptions = options;
  59. }, 500);
  60. }
  61. }

Cascader级联选择 - 图18

自定义选择项

自定义选项的模板。

  1. // tslint:disable:no-any
  2. import { Component } from '@angular/core';
  3. const options = [
  4. {
  5. label: 'Ant Design',
  6. value: 'antd',
  7. children: [
  8. {
  9. label: 'ng-zorro-antd',
  10. value: 'ng-zorro-antd',
  11. isLeaf: true
  12. }
  13. ]
  14. },
  15. {
  16. label: 'Angular',
  17. value: 'angular',
  18. children: [
  19. {
  20. label: 'CDK',
  21. value: 'cdk',
  22. isLeaf: true
  23. }
  24. ]
  25. }
  26. ];
  27. @Component({
  28. selector: 'nz-demo-cascader-custom-template',
  29. template: `
  30. <nz-cascader [nzOptionRender]="renderTpl" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
  31. </nz-cascader>
  32. <ng-template #renderTpl let-option let-index="index"> {{ index + 1 }}. {{ option.label }} </ng-template>
  33. `
  34. })
  35. export class NzDemoCascaderCustomTemplateComponent {
  36. nzOptions = options;
  37. values: any[] | null = null;
  38. onChanges(values: any): void {
  39. console.log(values, this.values);
  40. }
  41. }

API

  1. <nz-cascader [nzOptions]="options" [(ngModel)]="values"></nz-cascader>

nz-cascadercomponent

参数说明类型默认值支持全局配置
[ngModel]指定选中项any[]-
[nzAllowClear]是否支持清除booleantrue
[nzAutoFocus]是否自动聚焦,当存在输入框时booleanfalse
[nzChangeOn]点击父级菜单选项时,可通过该函数判断是否允许值的变化(option: any, index: number) => boolean-
[nzChangeOnSelect]当此项为 true 时,点选每级菜单选项值都会发生变化,具体见上面的演示booleanfalse
[nzColumnClassName]自定义浮层列类名string-
[nzDisabled]禁用booleanfalse
[nzExpandTrigger]次级菜单的展开方式,可选 ‘click’ 和 ‘hover’‘click’|’hover’‘click’
[nzMenuClassName]自定义浮层类名string-
[nzMenuStyle]自定义浮层样式object-
[nzNotFoundContent]当下拉列表为空时显示的内容string|TemplateRef<void>-
[nzLabelProperty]选项的显示值的属性名string‘label’
[nzLabelRender]选择后展示的渲染模板TemplateRef<any>-
[nzOptionRender]选项的渲染模板TemplateRef<{ $implicit: NzCascaderOption, index: number }>
[nzLoadData]用于动态加载选项。如果提供了ngModel初始值,且未提供nzOptions值,则会立即触发动态加载。(option: any, index?: index) => PromiseLike<any>-
[nzOptions]可选项数据源object[]-
[nzPlaceHolder]输入框占位文本string‘请选择’
[nzShowArrow]是否显示箭头booleantrue
[nzShowInput]显示输入框booleantrue
[nzShowSearch]是否支持搜索,默认情况下对 label 进行全匹配搜索,不能和 [nzLoadData] 同时使用boolean|NzShowSearchOptionsfalse
[nzSize]输入框大小,可选 largedefaultsmall‘large’|’small’|’default’‘default’
[nzValueProperty]选项的实际值的属性名string‘value’
(ngModelChange)值发生变化时触发EventEmitter<any[]>-
(nzVisibleChange)菜单浮层的显示/隐藏EventEmitter<boolean>-
(nzSelectionChange)值发生变化时触发EventEmitter<NzCascaderOption[]>-

nzShowSearch 为对象时需遵守 NzShowSearchOptions 接口:

参数说明类型默认值
filter可选,选择是否保留选项的过滤函数,每级菜单的选项都会被匹配(inputValue: string, path: NzCascaderOption[]): boolean-
sorter可选,按照到每个最终选项的路径进行排序,默认按照原始数据的顺序(a: NzCascaderOption[], b: NzCascaderOption[], inputValue: string): number-

方法

名称描述
blur()移除焦点
focus()获取焦点
closeMenu()隐藏菜单

注意,如果需要获得中国省市区数据,可以参考 china-division