- Cascader级联选择
- 何时使用
- 单独引入此组件
- 代码演示
- API
- nz-cascadercomponent
- 方法
- 方法
- nz-cascadercomponent
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: [
`
.ant-cascader-picker {
width: 300px;
}
.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);
}
}
可以自定义显示
切换按钮和结果分开。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderTriggerComponent {
nzOptions = options;
values: any[] | null = null;
text = 'Unselect';
onChanges(values: any): void {
console.log(values, this.values);
}
onSelectionChange(selectedOptions: any[]): void {
this.text = selectedOptions.map(o => o.label).join(', ');
}
}
禁用选项
通过指定 options 里的 disabled
字段。
// tslint:disable:no-any
import { Component } 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',
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderDisabledComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
}
大小
不同大小的级联选择器。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@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 = options;
value1: any[] | null = null;
value2: any[] | null = null;
value3: any[] | null = null;
onChanges(values: any): void {
console.log(values);
}
}
响应表单
通过表单的重置
功能清空已选择的值。
// tslint:disable:no-any
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
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" (nzChange)="onChanges($event)" [formControlName]="'name'"> </nz-cascader>
</form>
<br />
<button nz-button (click)="reset()">Reset</button>
<button nz-button (click)="submit()">Submit</button>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
button {
margin-right: 8px;
}
`
]
})
export class NzDemoCascaderReactiveFormComponent {
form: FormGroup;
nzOptions = options;
constructor(private fb: FormBuilder) {
this.createForm();
}
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: any): void {
console.log(values);
}
}
指定选择
通过函数来判断选项是否可以选择。
// tslint:disable:no-any
import { Component } 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',
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderChangeOnFunctionComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
validate(option: any, _index: number): boolean {
const value = option.value;
return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
}
}
搜索
可以直接搜索选项并选择。
// 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,
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: [
`
.ant-cascader-picker {
width: 300px;
}
.change-options {
display: inline-block;
font-size: 12px;
margin-top: 8px;
}
`
]
})
export class NzDemoCascaderSearchComponent implements OnInit {
nzOptions: any = 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);
}
}
默认值与延迟加载
默认值通过数组的方式指定,但nzOptions
没有赋值,通过nzLoadData
函数延迟加载。
// tslint:disable:no-any
import { Component } from '@angular/core';
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderDefaultValueAndLazyloadComponent {
values: any[] = ['zhejiang', 'hangzhou', 'xihu'];
onChanges(values: any): void {
console.log(values, this.values);
}
/** load data async execute by `nzLoadData` method */
loadData(node: any, index: number): PromiseLike<any> {
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);
});
}
}
自定义字段名
自定义字段名。
// tslint:disable:no-any
import { Component } from '@angular/core';
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderCustomFieldNamesComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
validate(option: any, _index: number): boolean {
const value = option.value;
return ['hangzhou', 'xihu', 'nanjing', 'zhonghuamen'].indexOf(value) >= 0;
}
}
默认值
默认值通过数组的方式指定。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-default-value',
template: `
<nz-cascader [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)"> </nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderDefaultValueComponent {
nzOptions = options;
values: any[] = ['zhejiang', 'hangzhou', 'xihu'];
/* // or like this:
values: any[] = [{
value: 'zhejiang',
label: 'Zhejiang'
}, {
value: 'hangzhou',
label: 'Hangzhou'
}, {
value: 'xihu',
label: 'West Lake'
}]; */
onChanges(values: any): void {
console.log(values, this.values);
}
}
移入展开
通过移入展开下级菜单,点击完成选择。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-hover',
template: `
<nz-cascader
[nzExpandTrigger]="'hover'"
[nzOptions]="nzOptions"
[(ngModel)]="values"
(ngModelChange)="onChanges($event)"
>
</nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderHoverComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
}
选择即改变
这种交互允许只选中父级选项。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-change-on-select',
template: `
<nz-cascader nzChangeOnSelect [nzOptions]="nzOptions" [(ngModel)]="values" (ngModelChange)="onChanges($event)">
</nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderChangeOnSelectComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
}
自定义已选项
例如给最后一项加上邮编链接。
// tslint:disable:no-any
import { Component } from '@angular/core';
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderCustomRenderComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
handleAreaClick(e: Event, label: string, option: any): void {
e.preventDefault();
e.stopPropagation();
console.log('clicked "', label, '"', option);
}
}
动态加载选项
使用 nzLoadData
实现动态加载选项。
// tslint:disable:no-any
import { Component } from '@angular/core';
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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderLazyComponent {
values: any[] | null = null;
onChanges(values: any): void {
console.log(values);
}
/** load data async execute by `nzLoadData` method */
loadData(node: any, index: number): PromiseLike<any> {
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);
});
}
}
模态窗口
在模态窗口中使用级联控件。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@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>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderModalComponent {
nzOptions = options;
values: any[] | null = null;
isVisible = false;
onChanges(values: any): 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;
}
}
鼠标移入触发
鼠标移入触发显示菜单,移出隐藏菜单。
// tslint:disable:no-any
import { Component } 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
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-trigger-action',
template: `
<nz-cascader
[nzTriggerAction]="'hover'"
[nzExpandTrigger]="'hover'"
[nzOptions]="nzOptions"
[(ngModel)]="values"
(ngModelChange)="onChanges($event)"
>
</nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderTriggerActionComponent {
nzOptions = options;
values: any[] | null = null;
onChanges(values: any): void {
console.log(values, this.values);
}
}
默认值与异步列表
默认值通过数组的方式指定,但nzOptions
通过异步加载。
// 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
}
]
}
]
}
];
@Component({
selector: 'nz-demo-cascader-default-value-and-asyn-options',
template: `
<nz-cascader [(ngModel)]="values" [nzOptions]="nzOptions" (ngModelChange)="onChanges($event)"> </nz-cascader>
`,
styles: [
`
.ant-cascader-picker {
width: 300px;
}
`
]
})
export class NzDemoCascaderDefaultValueAndAsynOptionsComponent implements OnInit {
nzOptions: any[] | null = null;
values: any[] = ['zhejiang', 'hangzhou', 'xihu'];
onChanges(values: any): void {
console.log(values, this.values);
}
ngOnInit(): void {
setTimeout(() => {
this.nzOptions = options;
}, 500);
}
}
API
<nz-cascader [nzOptions]="options" [(ngModel)]="values"></nz-cascader>
nz-cascadercomponent
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[ngModel] | 指定选中项 | any[] | - |
[nzAllowClear] | 是否支持清除 | boolean | true |
[nzAutoFocus] | 是否自动聚焦,当存在输入框时 | boolean | false |
[nzChangeOn] | 点击父级菜单选项时,可通过该函数判断是否允许值的变化 | function(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> | - |
[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[]> | - |
(nzClear) | 清空值时触发 | EventEmitter<void> | - |
(nzVisibleChange) | 菜单浮层的显示/隐藏 | EventEmitter<boolean> | - |
(nzSelect) | 选中菜单选项时触发 | EventEmitter<{option: any, index: number}> | - |
(nzSelectionChange) | 选中菜单选项时触发 | EventEmitter<any[]> | - |
nzShowSearch
为对象时需遵守 NzShowSearchOptions
接口:
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
filter | 可选,选择是否保留选项的过滤函数,每级菜单的选项都会被匹配 | (inputValue: string, path: CascaderOption[]): boolean | - |
sorter | 可选,按照到每个最终选项的路径进行排序,默认按照原始数据的顺序 | (a: CascaderOption[], b: CascaderOption[], inputValue: string): number | - |
方法
名称 | 描述 |
---|---|
blur() | 移除焦点 |
focus() | 获取焦点 |
closeMenu() | 隐藏菜单 |
注意,如果需要获得中国省市区数据,可以参考 china-division。