Cascader级联选择
级联选择框。
何时使用
- 需要从一组相关联的数据集合进行选择,例如省市区,公司层级,事物分类等。
- 从一个较大的数据集合中进行选择时,用多级分类进行分隔,方便选择。
- 比起 Select 组件,可以在同一个浮层中完成选择,有较好的体验。
import { NzCascaderModule } from 'ng-zorro-antd/cascader';
代码演示
基本
省市区级联。
// tslint:disable:no-any
import { Component, OnInit } from '@angular/core';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
const otherOptions = [
{
value: 'fujian',
label: 'Fujian',
children: [
{
value: 'xiamen',
label: 'Xiamen',
children: [
{
value: 'Kulangsu',
label: 'Kulangsu',
isLeaf: true
}
]
}
]
},
{
value: 'guangxi',
label: 'Guangxi',
children: [
{
value: 'guilin',
label: 'Guilin',
children: [
{
value: 'Lijiang',
label: 'Li Jiang River',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-basic',
template: `
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"></nz-cascader>
<a href="javascript:;" (click)="changeNzOptions()" class="change-options">
Change Options
</a>
`,
styles: [
`
.change-options {
display: inline-block;
font-size: 12px;
margin-top: 8px;
}
`
]
})
export class NzDemoCascaderBasicComponent implements OnInit {
nzOptions: any[] | null = null;
values: any[] | null = null;
ngOnInit(): void {
setTimeout(() => {
this.nzOptions = options;
}, 100);
}
changeNzOptions(): void {
if (this.nzOptions === options) {
this.nzOptions = otherOptions;
} else {
this.nzOptions = options;
}
}
onChanges(values: any): void {
console.log(values, this.values);
}
}
可以自定义显示
切换按钮和结果分开。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-trigger',
template: `
{{ text }}
<nz-cascader
[nzShowInput]="false"
[nzOptions]="nzOptions"
[(ngModel)]="values"
(ngModelChange)="onChanges($event)"
(nzSelectionChange)="onSelectionChange($event)"
>
<a href="javascript: void(0)">Change city</a>
</nz-cascader>
`
})
export class NzDemoCascaderTriggerComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
text = 'Unselect';
onChanges(values: string[]): void {
console.log(values, this.values);
}
onSelectionChange(selectedOptions: NzCascaderOption[]): void {
this.text = selectedOptions.map(o => o.label).join(', ');
}
}
禁用选项
通过指定 options 里的 disabled
字段。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
disabled: true,
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-disabled',
template: `
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderDisabledComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
大小
不同大小的级联选择器。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-size',
template: `
<nz-cascader [nzSize]="'large'" [nzOptions]="nzOptions" [(ngModel)]="value1" (ngModelChange)="onChanges($event)"> </nz-cascader>
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="value2" (ngModelChange)="onChanges($event)"> </nz-cascader>
<nz-cascader [nzSize]="'small'" [nzOptions]="nzOptions" [(ngModel)]="value3" (ngModelChange)="onChanges($event)"> </nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
margin-bottom: 8px;
}
`
]
})
export class NzDemoCascaderSizeComponent {
nzOptions: NzCascaderOption[] = options;
value1: string[] | null = null;
value2: string[] | null = null;
value3: string[] | null = null;
onChanges(values: string[]): void {
console.log(values);
}
}
响应表单
通过表单的重置
功能清空已选择的值。
import { Component, OnDestroy } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
import { Subscription } from 'rxjs';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-reactive-form',
template: `
<form [formGroup]="form" novalidate>
<nz-cascader [nzOptions]="nzOptions" [formControlName]="'name'"> </nz-cascader>
</form>
<br />
<button nz-button (click)="reset()">Reset</button>
<button nz-button (click)="submit()">Submit</button>
`,
styles: [
`
button {
margin-right: 8px;
}
`
]
})
export class NzDemoCascaderReactiveFormComponent implements OnDestroy {
form!: FormGroup;
nzOptions: NzCascaderOption[] = options;
changeSubscription: Subscription;
constructor(private fb: FormBuilder) {
this.createForm();
const control = this.form.get('name') as FormControl;
this.changeSubscription = control.valueChanges.subscribe(data => {
this.onChanges(data);
});
}
private createForm(): void {
this.form = this.fb.group({
name: [null, Validators.required]
});
}
reset(): void {
this.form.reset();
console.log(this.form.value);
}
submit(): void {
console.log(this.form.value);
}
onChanges(values: string[]): void {
console.log(values);
}
ngOnDestroy(): void {
this.changeSubscription.unsubscribe();
}
}
指定选择
通过函数来判断选项是否可以选择。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
children: [
{
value: 'dongqianlake',
label: 'Dongqian Lake',
isLeaf: true
}
]
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-change-on-function',
template: `
<nz-cascader [nzChangeOn]="validate" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderChangeOnFunctionComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
validate(option: NzCascaderOption, _index: number): boolean {
const value = option.value as string;
return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
}
}
搜索
可以直接搜索选项并选择。
import { Component, OnInit } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true,
disabled: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
const otherOptions = [
{
value: 'fujian',
label: 'Fujian',
children: [
{
value: 'xiamen',
label: 'Xiamen',
children: [
{
value: 'Kulangsu',
label: 'Kulangsu',
isLeaf: true
}
]
}
]
},
{
value: 'guangxi',
label: 'Guangxi',
children: [
{
value: 'guilin',
label: 'Guilin',
children: [
{
value: 'Lijiang',
label: 'Li Jiang River',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-search',
template: `
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" [nzShowSearch]="true" (ngModelChange)="onChanges($event)"> </nz-cascader>
<a href="javascript:;" (click)="changeNzOptions()" class="change-options">
Change Options
</a>
`,
styles: [
`
.change-options {
display: inline-block;
font-size: 12px;
margin-top: 8px;
}
`
]
})
export class NzDemoCascaderSearchComponent implements OnInit {
nzOptions: NzCascaderOption[] | null = null;
values: string[] | null = null;
ngOnInit(): void {
setTimeout(() => {
this.nzOptions = options;
}, 100);
}
changeNzOptions(): void {
if (this.nzOptions === options) {
this.nzOptions = otherOptions;
} else {
this.nzOptions = options;
}
}
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
默认值与延迟加载
默认值通过数组的方式指定,但nzOptions
没有赋值,通过nzLoadData
函数延迟加载。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const provinces = [
{
value: 'zhejiang',
label: 'Zhejiang'
},
{
value: 'jiangsu',
label: 'Jiangsu'
}
];
const cities: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
zhejiang: [
{
value: 'hangzhou',
label: 'Hangzhou'
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
],
jiangsu: [
{
value: 'nanjing',
label: 'Nanjing'
}
]
};
const scenicspots: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
hangzhou: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
],
nanjing: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
};
@Component({
selector: 'nz-demo-cascader-default-value-and-lazyload',
template: ` <nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)"> </nz-cascader> `
})
export class NzDemoCascaderDefaultValueAndLazyloadComponent {
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
onChanges(values: string[]): void {
console.log(values, this.values);
}
/** load data async execute by `nzLoadData` method */
loadData(node: NzCascaderOption, index: number): PromiseLike<void> {
return new Promise(resolve => {
setTimeout(() => {
if (index < 0) {
// if index less than 0 it is root node
node.children = provinces;
} else if (index === 0) {
node.children = cities[node.value];
} else {
node.children = scenicspots[node.value];
}
resolve();
}, 1000);
});
}
}
自定义字段名
自定义字段名。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
code: 'zhejiang',
name: 'Zhejiang',
children: [
{
code: 'hangzhou',
name: 'Hangzhou',
children: [
{
code: 'xihu',
name: 'West Lake',
isLeaf: true
}
]
},
{
code: 'ningbo',
name: 'Ningbo',
children: [
{
code: 'dongqianlake',
name: 'Dongqian Lake',
isLeaf: true
}
]
}
]
},
{
code: 'jiangsu',
name: 'Jiangsu',
children: [
{
code: 'nanjing',
name: 'Nanjing',
children: [
{
code: 'zhonghuamen',
name: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-custom-field-names',
template: `
<nz-cascader
[nzChangeOn]="validate"
[nzOptions]="nzOptions"
[nzLabelProperty]="'name'"
[nzValueProperty]="'code'"
[nzShowSearch]="true"
[(ngModel)]="values"
(ngModelChange)="onChanges($event)"
>
</nz-cascader>
`
})
export class NzDemoCascaderCustomFieldNamesComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
validate(option: NzCascaderOption, _index: number): boolean {
const value = option.value as string;
return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
}
}
默认值
默认值通过数组的方式指定。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-default-value',
template: `
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderDefaultValueComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
/* // or like this:
values: any[] = [{
value: 'zhejiang',
label: 'Zhejiang'
}, {
value: 'hangzhou',
label: 'Hangzhou'
}, {
value: 'xihu',
label: 'West Lake'
}]; */
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
移入展开
通过移入展开下级菜单,点击完成选择。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-hover',
template: `
<nz-cascader [nzExpandTrigger]="'hover'" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
</nz-cascader>
`
})
export class NzDemoCascaderHoverComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
选择即改变
这种交互允许只选中父级选项。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-change-on-select',
template: `
<nz-cascader nzChangeOnSelect [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderChangeOnSelectComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
自定义已选项
例如给最后一项加上邮编链接。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
code: 752100,
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
code: '315000',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
code: 453400,
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-custom-render',
template: `
<nz-cascader [nzLabelRender]="renderTpl" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
</nz-cascader>
<ng-template #renderTpl let-labels="labels" let-selectedOptions="selectedOptions">
<ng-container *ngFor="let label of labels; let i = index; let isLast = last">
<span *ngIf="!isLast">{{ label }} / </span>
<span *ngIf="isLast">
{{ label }} (<a href="javascript:;" (click)="handleAreaClick($event, label, selectedOptions[i])">
{{ selectedOptions[i].code }} </a
>)
</span>
</ng-container>
</ng-template>
`
})
export class NzDemoCascaderCustomRenderComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
handleAreaClick(e: Event, label: string, option: NzCascaderOption): void {
e.preventDefault();
e.stopPropagation();
console.log('clicked "', label, '"', option);
}
}
动态加载选项
使用 nzLoadData
实现动态加载选项。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const provinces = [
{
value: 'zhejiang',
label: 'Zhejiang'
},
{
value: 'jiangsu',
label: 'Jiangsu'
}
];
const cities: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
zhejiang: [
{
value: 'hangzhou',
label: 'Hangzhou'
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
],
jiangsu: [
{
value: 'nanjing',
label: 'Nanjing'
}
]
};
const scenicspots: { [key: string]: Array<{ value: string; label: string; isLeaf?: boolean }> } = {
hangzhou: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
],
nanjing: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
};
@Component({
selector: 'nz-demo-cascader-lazy',
template: `
<nz-cascader [(ngModel)]="values" [nzLoadData]="loadData" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderLazyComponent {
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values);
}
/** load data async execute by `nzLoadData` method */
loadData(node: NzCascaderOption, index: number): PromiseLike<void> {
return new Promise(resolve => {
setTimeout(() => {
if (index < 0) {
// if index less than 0 it is root node
node.children = provinces;
} else if (index === 0) {
node.children = cities[node.value];
} else {
node.children = scenicspots[node.value];
}
resolve();
}, 1000);
});
}
}
模态窗口
在模态窗口中使用级联控件。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-modal',
template: `
<nz-modal [(nzVisible)]="isVisible" nzTitle="Please select" (nzOnCancel)="handleCancel($event)" (nzOnOk)="handleOk($event)">
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
</nz-modal>
<button nz-button (click)="open()">Open Dialog</button>
`
})
export class NzDemoCascaderModalComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
isVisible = false;
onChanges(values: string[]): void {
console.log(values, this.values);
}
open(): void {
this.isVisible = true;
}
handleOk($event: MouseEvent): void {
console.log('Button ok clicked!', this.values, $event);
this.isVisible = false;
}
handleCancel($event: MouseEvent): void {
console.log('Button cancel clicked!', $event);
this.isVisible = false;
}
}
鼠标移入触发
鼠标移入触发显示菜单,移出隐藏菜单。
import { Component } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-trigger-action',
template: `
<nz-cascader
[nzTriggerAction]="'hover'"
[nzExpandTrigger]="'hover'"
[nzOptions]="nzOptions"
[(ngModel)]="values"
(ngModelChange)="onChanges($event)"
>
</nz-cascader>
`
})
export class NzDemoCascaderTriggerActionComponent {
nzOptions: NzCascaderOption[] = options;
values: string[] | null = null;
onChanges(values: string[]): void {
console.log(values, this.values);
}
}
默认值与异步列表
默认值通过数组的方式指定,但nzOptions
通过异步加载。
import { Component, OnInit } from '@angular/core';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-default-value-and-asyn-options',
template: `
<nz-cascader [(ngModel)]="values" [nzOptions]="nzOptions" (ngModelChange)="onChanges($event)"> </nz-cascader>
`
})
export class NzDemoCascaderDefaultValueAndAsynOptionsComponent implements OnInit {
nzOptions: NzCascaderOption[] | null = null;
values: string[] = ['zhejiang', 'hangzhou', 'xihu'];
onChanges(values: string[]): void {
console.log(values, this.values);
}
ngOnInit(): void {
setTimeout(() => {
this.nzOptions = options;
}, 500);
}
}
自定义选择项
自定义选项的模板。
// tslint:disable:no-any
import { Component } from '@angular/core';
const options = [
{
label: 'Ant Design',
value: 'antd',
children: [
{
label: 'ng-zorro-antd',
value: 'ng-zorro-antd',
isLeaf: true
}
]
},
{
label: 'Angular',
value: 'angular',
children: [
{
label: 'CDK',
value: 'cdk',
isLeaf: true
}
]
}
];
@Component({
selector: 'nz-demo-cascader-custom-template',
template: `
<nz-cascader [nzOptionRender]="renderTpl" [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
</nz-cascader>
<ng-template #renderTpl let-option let-index="index"> {{ index + 1 }}. {{ option.label }} </ng-template>
`
})
export class NzDemoCascaderCustomTemplateComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
}
API
<nz-cascader [nzOptions]="options" [(ngModel)]="values"></nz-cascader>
nz-cascadercomponent
参数 | 说明 | 类型 | 默认值 | 支持全局配置 |
---|---|---|---|---|
[ngModel] | 指定选中项 | any[] | - | |
[nzAllowClear] | 是否支持清除 | boolean | true | |
[nzAutoFocus] | 是否自动聚焦,当存在输入框时 | boolean | false | |
[nzChangeOn] | 点击父级菜单选项时,可通过该函数判断是否允许值的变化 | (option: any, index: number) => boolean | - | |
[nzChangeOnSelect] | 当此项为 true 时,点选每级菜单选项值都会发生变化,具体见上面的演示 | boolean | false | |
[nzColumnClassName] | 自定义浮层列类名 | string | - | |
[nzDisabled] | 禁用 | boolean | false | |
[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] | 是否显示箭头 | boolean | true | |
[nzShowInput] | 显示输入框 | boolean | true | |
[nzShowSearch] | 是否支持搜索,默认情况下对 label 进行全匹配搜索,不能和 [nzLoadData] 同时使用 | boolean|NzShowSearchOptions | false | |
[nzSize] | 输入框大小,可选 large default small | ‘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。