Select选择器
下拉选择器。
何时使用
- 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
- 当选项少时(少于 5 项),建议直接将选项平铺,使用 Radio 是更好的选择。
import { NzSelectModule } from 'ng-zorro-antd/select';
代码演示
基本使用
基本使用。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-basic',
template: `
<nz-select ngModel="lucy">
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
<nz-option nzValue="disabled" nzLabel="Disabled" nzDisabled></nz-option>
</nz-select>
<nz-select ngModel="lucy" nzDisabled>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
<nz-select ngModel="lucy" nzLoading>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
<nz-select ngModel="lucy" nzAllowClear nzPlaceHolder="Choose">
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
margin: 0 8px 10px 0;
width: 120px;
}
`
]
})
export class NzDemoSelectBasicComponent {}
多选
多选,从已有条目中选择,例子中通过 nzMaxTagCount
限制最多显示3个选项。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-select-multiple',
template: `
<nz-select
[nzMaxTagCount]="3"
[nzMaxTagPlaceholder]="tagPlaceHolder"
nzMode="multiple"
nzPlaceHolder="Please select"
[(ngModel)]="listOfSelectedValue"
>
<nz-option *ngFor="let item of listOfOption" [nzLabel]="item" [nzValue]="item"></nz-option>
</nz-select>
<ng-template #tagPlaceHolder let-selectedList> and {{ selectedList.length }} more selected </ng-template>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectMultipleComponent implements OnInit {
listOfOption: string[] = [];
listOfSelectedValue = ['a10', 'c12'];
ngOnInit(): void {
const children: string[] = [];
for (let i = 10; i < 36; i++) {
children.push(`${i.toString(36)}${i}`);
}
this.listOfOption = children;
}
}
标签
tags select,随意输入的内容(scroll the menu)
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-select-tags',
template: `
<nz-select nzMode="tags" nzPlaceHolder="Tag Mode" [(ngModel)]="listOfTagOptions">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectTagsComponent implements OnInit {
listOfOption: Array<{ label: string; value: string }> = [];
listOfTagOptions = [];
ngOnInit(): void {
const children: Array<{ label: string; value: string }> = [];
for (let i = 10; i < 36; i++) {
children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
}
this.listOfOption = children;
}
}
联动
省市联动是典型的例子。
推荐使用 Cascader 组件。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-coordinate',
template: `
<div>
<nz-select [(ngModel)]="selectedProvince" (ngModelChange)="provinceChange($event)">
<nz-option *ngFor="let p of provinceData" [nzValue]="p" [nzLabel]="p"></nz-option>
</nz-select>
<nz-select [(ngModel)]="selectedCity">
<nz-option *ngFor="let c of cityData[selectedProvince]" [nzValue]="c" [nzLabel]="c"></nz-option>
</nz-select>
</div>
`,
styles: [
`
nz-select {
margin-right: 8px;
width: 120px;
}
`
]
})
export class NzDemoSelectCoordinateComponent {
selectedProvince = 'Zhejiang';
selectedCity = 'Hangzhou';
provinceData = ['Zhejiang', 'Jiangsu'];
cityData: { [place: string]: string[] } = {
Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang']
};
provinceChange(value: string): void {
this.selectedCity = this.cityData[value][0];
}
}
获得选项的对象
ngModel
取到的值为选中 nz-option
的 nzValue
值,当 nzValue
传入为一个对象时,ngModel
获取的值也是一个对象,compareWith
的用法参见 这里.
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-label-in-value',
template: `
<p>The selected option's age is {{ selectedValue?.age }}</p>
<br />
<nz-select [(ngModel)]="selectedValue" [compareWith]="compareFn" (ngModelChange)="log($event)" nzAllowClear nzPlaceHolder="Choose">
<nz-option *ngFor="let option of optionList" [nzValue]="option" [nzLabel]="option.label"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 120px;
}
`
]
})
export class NzDemoSelectLabelInValueComponent {
optionList = [
{ label: 'Lucy', value: 'lucy', age: 20 },
{ label: 'Jack', value: 'jack', age: 22 }
];
selectedValue = { label: 'Jack', value: 'jack', age: 22 };
// tslint:disable-next-line:no-any
compareFn = (o1: any, o2: any) => (o1 && o2 ? o1.value === o2.value : o1 === o2);
log(value: { label: string; value: string; age: number }): void {
console.log(value);
}
}
搜索用户
一个带有远程搜索,节流控制,请求时序控制,加载状态的多选示例。
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { debounceTime, map, switchMap } from 'rxjs/operators';
@Component({
selector: 'nz-demo-select-select-users',
template: `
<nz-select
nzMode="multiple"
nzPlaceHolder="Select users"
nzAllowClear
nzShowSearch
nzServerSearch
[(ngModel)]="selectedUser"
(nzOnSearch)="onSearch($event)"
>
<ng-container *ngFor="let o of optionList">
<nz-option *ngIf="!isLoading" [nzValue]="o" [nzLabel]="o"></nz-option>
</ng-container>
<nz-option *ngIf="isLoading" nzDisabled nzCustomContent>
<i nz-icon nzType="loading" class="loading-icon"></i> Loading Data...
</nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
.loading-icon {
margin-right: 8px;
}
`
]
})
export class NzDemoSelectSelectUsersComponent implements OnInit {
randomUserUrl = 'https://api.randomuser.me/?results=5';
searchChange$ = new BehaviorSubject('');
optionList: string[] = [];
selectedUser: string;
isLoading = false;
onSearch(value: string): void {
this.isLoading = true;
this.searchChange$.next(value);
}
constructor(private http: HttpClient) {}
ngOnInit(): void {
// tslint:disable:no-any
const getRandomNameList = (name: string) =>
this.http
.get(`${this.randomUserUrl}`)
.pipe(map((res: any) => res.results))
.pipe(
map((list: any) => {
return list.map((item: any) => `${item.name.first} ${name}`);
})
);
const optionList$: Observable<string[]> = this.searchChange$
.asObservable()
.pipe(debounceTime(500))
.pipe(switchMap(getRandomNameList));
optionList$.subscribe(data => {
this.optionList = data;
this.isLoading = false;
});
}
}
隐藏已选择选项
通过 nzHide
隐藏下拉列表中已选择的选项。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-hide-selected',
template: `
<nz-select nzMode="multiple" nzPlaceHolder="Inserted are removed" [(ngModel)]="listOfSelectedValue">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option" [nzValue]="option" [nzHide]="!isNotSelected(option)"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectHideSelectedComponent {
listOfOption = ['Apples', 'Nails', 'Bananas', 'Helicopters'];
listOfSelectedValue: string[] = [];
isNotSelected(value: string): boolean {
return this.listOfSelectedValue.indexOf(value) === -1;
}
}
下拉加载
一个带有下拉加载远程数据的例子。
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'nz-demo-select-scroll-load',
template: `
<nz-select
[(ngModel)]="selectedUser"
(nzScrollToBottom)="loadMore()"
nzPlaceHolder="Select users"
nzAllowClear
[nzDropdownRender]="renderTemplate"
>
<nz-option *ngFor="let o of optionList" [nzValue]="o" [nzLabel]="o"></nz-option>
</nz-select>
<ng-template #renderTemplate>
<nz-spin *ngIf="isLoading"></nz-spin>
</ng-template>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectScrollLoadComponent implements OnInit {
randomUserUrl = 'https://api.randomuser.me/?results=10';
optionList: string[] = [];
selectedUser = null;
isLoading = false;
// tslint:disable:no-any
getRandomNameList: Observable<string[]> = this.http
.get(`${this.randomUserUrl}`)
.pipe(map((res: any) => res.results))
.pipe(
map((list: any) => {
return list.map((item: any) => `${item.name.first}`);
})
);
loadMore(): void {
this.isLoading = true;
this.getRandomNameList.subscribe(data => {
this.isLoading = false;
this.optionList = [...this.optionList, ...data];
});
}
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.loadMore();
}
}
大量数据
组件使用了虚拟滚动技术,可以同时处理大量数据。
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-select-big-data',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<nz-select nzMode="multiple" nzPlaceHolder="Please select" [nzOptions]="listOfOption" [(ngModel)]="listOfSelectedValue"> </nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectBigDataComponent implements OnInit {
listOfOption: Array<{ value: string; label: string }> = [];
listOfSelectedValue = ['a10', 'c12'];
ngOnInit(): void {
const children: string[] = [];
for (let i = 10; i < 10000; i++) {
children.push(`${i.toString(36)}${i}`);
}
this.listOfOption = children.map(item => {
return {
value: item,
label: item
};
});
}
}
自定义选择标签
通过 nzCustomTemplate
自定义 nz-select 显示的内容。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-custom-template',
template: `
<nz-select nzAllowClear nzPlaceHolder="Select OS" [nzCustomTemplate]="defaultTemplate">
<nz-option nzLabel="Windows" nzValue="windows"></nz-option>
<nz-option nzLabel="Apple" nzValue="apple"></nz-option>
<nz-option nzLabel="Android" nzValue="android"></nz-option>
</nz-select>
<ng-template #defaultTemplate let-selected> <i nz-icon [nzType]="selected.nzValue"></i> {{ selected.nzLabel }} </ng-template>
<br />
<br />
<nz-select nzAllowClear nzPlaceHolder="Select OS" nzMode="multiple" [nzCustomTemplate]="multipleTemplate">
<nz-option nzLabel="Windows" nzValue="windows"></nz-option>
<nz-option nzLabel="Apple" nzValue="apple"></nz-option>
<nz-option nzLabel="Android" nzValue="android"></nz-option>
</nz-select>
<ng-template #multipleTemplate let-selected>
<div class="ant-select-selection-item-content"><i nz-icon [nzType]="selected.nzValue"></i> {{ selected.nzLabel }}</div>
</ng-template>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectCustomTemplateComponent {}
带搜索框
展开后可对选项进行搜索。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-search',
template: `
<nz-select nzShowSearch nzAllowClear nzPlaceHolder="Select a person" [(ngModel)]="selectedValue">
<nz-option nzLabel="Jack" nzValue="jack"></nz-option>
<nz-option nzLabel="Lucy" nzValue="lucy"></nz-option>
<nz-option nzLabel="Tom" nzValue="tom"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 200px;
}
`
]
})
export class NzDemoSelectSearchComponent {
selectedValue = null;
}
三种大小
三种大小的选择框,当 nzSize
分别为 large
和 small
时,输入框高度为 40px
和 24px
,默认高度为 32px
。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-select-size',
template: `
<nz-radio-group [(ngModel)]="size">
<label nz-radio-button nzValue="large"><span>Large</span></label>
<label nz-radio-button nzValue="default"><span>Default</span></label>
<label nz-radio-button nzValue="small"><span>Small</span></label>
</nz-radio-group>
<br /><br />
<nz-select [(ngModel)]="singleValue" [nzSize]="size">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
<br /><br />
<nz-select [(ngModel)]="singleValue" [nzSize]="size" nzShowSearch>
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
<br /><br />
<nz-select [(ngModel)]="multipleValue" [nzSize]="size" nzMode="multiple" nzPlaceHolder="Please select">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
<br /><br />
<nz-select [(ngModel)]="tagValue" [nzSize]="size" nzMode="tags" nzPlaceHolder="Please select">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectSizeComponent implements OnInit {
listOfOption: Array<{ label: string; value: string }> = [];
size = 'default';
singleValue = 'a10';
multipleValue = ['a10', 'c12'];
tagValue = ['a10', 'c12', 'tag'];
ngOnInit(): void {
const children: Array<{ label: string; value: string }> = [];
for (let i = 10; i < 36; i++) {
children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
}
this.listOfOption = children;
}
}
分组
用 nz-option-group
进行选项分组。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-optgroup',
template: `
<nz-select [(ngModel)]="selectedValue" nzAllowClear nzPlaceHolder="Choose">
<nz-option-group nzLabel="Manager">
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-option-group>
<nz-option-group nzLabel="Engineer">
<nz-option nzValue="tom" nzLabel="Tom"></nz-option>
</nz-option-group>
</nz-select>
`,
styles: [
`
nz-select {
width: 120px;
}
`
]
})
export class NzDemoSelectOptgroupComponent {
selectedValue = 'lucy';
}
搜索框
搜索和远程数据结合。
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-search-box',
template: `
<nz-select
nzShowSearch
nzServerSearch
nzPlaceHolder="input search text"
[(ngModel)]="selectedValue"
[nzShowArrow]="false"
[nzFilterOption]="nzFilterOption"
(nzOnSearch)="search($event)"
>
<nz-option *ngFor="let o of listOfOption" [nzLabel]="o.text" [nzValue]="o.value"> </nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 200px;
}
`
]
})
export class NzDemoSelectSearchBoxComponent {
selectedValue = null;
listOfOption: Array<{ value: string; text: string }> = [];
nzFilterOption = () => true;
constructor(private httpClient: HttpClient) {}
search(value: string): void {
this.httpClient
.jsonp<{ result: Array<[string, string]> }>(`https://suggest.taobao.com/sug?code=utf-8&q=${value}`, 'callback')
.subscribe(data => {
const listOfOption: Array<{ value: string; text: string }> = [];
data.result.forEach(item => {
listOfOption.push({
value: item[0],
text: item[0]
});
});
this.listOfOption = listOfOption;
});
}
}
自动分词
试下复制 露西,杰克
到输入框里。只在 tags 和 multiple 模式下可用。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-select-automatic-tokenization',
template: `
<nz-select [(ngModel)]="listOfTagOptions" nzMode="tags" [nzTokenSeparators]="[',']" nzPlaceHolder="automatic tokenization">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectAutomaticTokenizationComponent implements OnInit {
listOfOption: Array<{ label: string; value: string }> = [];
listOfTagOptions = [];
ngOnInit(): void {
const children: Array<{ label: string; value: string }> = [];
for (let i = 10; i < 36; i++) {
children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
}
this.listOfOption = children;
}
}
扩展菜单
使用 nzDropdownRender
对下拉菜单进行自由扩展。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-custom-dropdown-menu',
template: `
<nz-select nzShowSearch nzAllowClear [nzDropdownRender]="renderTemplate" nzPlaceHolder="custom dropdown render">
<nz-option *ngFor="let item of listOfItem" [nzLabel]="item" [nzValue]="item"></nz-option>
</nz-select>
<ng-template #renderTemplate>
<nz-divider></nz-divider>
<div class="container">
<input type="text" nz-input #inputElement />
<a class="add-item" (click)="addItem(inputElement)"><i nz-icon nzType="plus"></i> Add item</a>
</div>
</ng-template>
`,
styles: [
`
nz-select {
width: 240px;
}
nz-divider {
margin: 4px 0;
}
.container {
display: flex;
flex-wrap: nowrap;
padding: 8px;
}
input {
}
.add-item {
flex: 0 0 auto;
padding: 8px;
display: block;
}
`
]
})
export class NzDemoSelectCustomDropdownMenuComponent {
listOfItem = ['jack', 'lucy'];
index = 0;
addItem(input: HTMLInputElement): void {
const value = input.value;
if (this.listOfItem.indexOf(value) === -1) {
this.listOfItem = [...this.listOfItem, input.value || `New item ${this.index++}`];
}
}
}
自定义下拉菜单内容
通过 nzCustomContent
自定义下拉菜单选项显示的内容。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-custom-content',
template: `
<nz-select nzShowSearch nzAllowClear nzPlaceHolder="Select OS" [(ngModel)]="selectedOS">
<nz-option nzCustomContent nzLabel="Windows" nzValue="windows"><i nz-icon nzType="windows"></i> Windows</nz-option>
<nz-option nzCustomContent nzLabel="Mac" nzValue="mac"><i nz-icon nzType="apple"></i> Mac</nz-option>
<nz-option nzCustomContent nzLabel="Android" nzValue="android"><i nz-icon nzType="android"></i> Android </nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 200px;
}
`
]
})
export class NzDemoSelectCustomContentComponent {
selectedOS = null;
}
默认数据
当需要显示默认值,同时默认值又不在选项列表中时,可以使用 nzHide
在 nz-option
中将默认选项隐藏
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-default-value',
template: `
<nz-select nzMode="multiple" nzPlaceHolder="Inserted are removed" [(ngModel)]="listOfSelectedValue">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option" [nzValue]="option"></nz-option>
<nz-option *ngFor="let option of defaultOption" [nzLabel]="option" [nzValue]="option" nzHide></nz-option>
</nz-select>
<br />
<br />
<nz-select [(ngModel)]="selectedValue">
<nz-option *ngFor="let option of listOfOption" [nzLabel]="option" [nzValue]="option"></nz-option>
<nz-option nzLabel="Default Value" nzValue="Default" nzHide></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
width: 100%;
}
`
]
})
export class NzDemoSelectDefaultValueComponent {
listOfOption = ['Option 01', 'Option 02'];
listOfSelectedValue = ['Default 01', 'Default 02'];
defaultOption = [...this.listOfSelectedValue];
selectedValue = 'Default';
}
无边框
无边框样式。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-border-less',
template: `
<nz-select ngModel="lucy" nzBorderless>
<nz-option nzValue="jack" nzLabel="Jack"></nz-option>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
<nz-option nzValue="disabled" nzLabel="Disabled" nzDisabled></nz-option>
</nz-select>
<nz-select ngModel="lucy" nzDisabled nzBorderless>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
`,
styles: [
`
nz-select {
margin: 0 8px 10px 0;
width: 120px;
}
`
]
})
export class NzDemoSelectBorderLessComponent {}
传入 Options
通过 nzOptions
直接传入选项内容
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-select-options',
template: `
<nz-select ngModel="lucy" [nzOptions]="listOfOption"></nz-select>
<nz-select [(ngModel)]="selectedValue" nzAllowClear nzPlaceHolder="Choose" [nzOptions]="listOfGroupOption"></nz-select>
`,
styles: [
`
nz-select {
margin: 0 8px 10px 0;
width: 120px;
}
`
]
})
export class NzDemoSelectOptionsComponent {
selectedValue = 'lucy';
listOfOption = [
{ label: 'Jack', value: 'jack' },
{ label: 'Lucy', value: 'lucy' },
{ label: 'disabled', value: 'disabled', disabled: true }
];
listOfGroupOption = [
{ label: 'Jack', value: 'jack', groupLabel: 'Manager' },
{ label: 'Lucy', value: 'lucy', groupLabel: 'Manager' },
{ label: 'Tom', value: 'tom', groupLabel: 'Engineer' }
];
}
API
<nz-select>
<nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
</nz-select>
nz-selectcomponent
参数 | 说明 | 类型 | 默认值 | 全局配置 |
---|---|---|---|---|
[ngModel] | 当前选中的 nz-option 的 nzValue 值,可双向绑定,当 nzMode 为 multiple 或 tags 时,ngModel 为数组 | any | any[] | - | |
[compareWith] | 与 SelectControlValueAccessor 相同 | (o1: any, o2: any) => boolean | (o1: any, o2: any) => o1===o2 | |
[nzAutoClearSearchValue] | 是否在选中项后清空搜索框,只在 mode 为 multiple 或 tags 时有效。 | boolean | true | |
[nzAllowClear] | 支持清除 | boolean | false | |
[nzOpen] | 下拉菜单是否打开,可双向绑定 | boolean | false | |
[nzAutoFocus] | 默认获取焦点 | boolean | false | |
[nzDisabled] | 是否禁用 | boolean | false | |
[nzDropdownClassName] | 下拉菜单的 className 属性 | string | - | |
[nzDropdownMatchSelectWidth] | 下拉菜单和选择器同宽 | boolean | true | |
[nzDropdownStyle] | 下拉菜单的 style 属性 | object | - | |
[nzCustomTemplate] | 自定义选择框的Template内容 | TemplateRef<{ $implicit: NzOptionComponent }> | - | |
[nzServerSearch] | 是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤 | boolean | false | |
[nzFilterOption] | 是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true ,反之则返回 false 。 | (input?: string, option?: NzOptionComponent) => boolean; | - | |
[nzMaxMultipleCount] | 最多选中多少个标签 | number | Infinity | |
[nzMode] | 设置 nz-select 的模式 | ‘multiple’ | ‘tags’ | ‘default’ | ‘default’ | |
[nzNotFoundContent] | 当下拉列表为空时显示的内容 | string | TemplateRef<void> | - | |
[nzPlaceHolder] | 选择框默认文字 | string | - | |
[nzShowArrow] | 是否显示下拉小箭头 | boolean | true | |
[nzShowSearch] | 使单选模式可搜索 | boolean | false | |
[nzSize] | 选择框大小 | ‘large’ | ‘small’ | ‘default’ | ‘default’ | |
[nzSuffixIcon] | 自定义的选择框后缀图标 | TemplateRef<any> | string | - | ✅ |
[nzRemoveIcon] | 自定义的多选框清除图标 | TemplateRef<any> | - | |
[nzClearIcon] | 自定义的多选框清空图标 | TemplateRef<any> | - | |
[nzMenuItemSelectedIcon] | 自定义当前选中的条目图标 | TemplateRef<any> | - | |
[nzTokenSeparators] | 在 tags 和 multiple 模式下自动分词的分隔符 | string[] | [] | |
[nzLoading] | 加载中状态 | boolean | false | |
[nzMaxTagCount] | 最多显示多少个 tag | number | - | |
[nzMaxTagPlaceholder] | 隐藏 tag 时显示的内容 | TemplateRef<{ $implicit: any[] }> | - | |
[nzOptions] | option 列表,可以取代 nz-option,用法参见例子 | Array<{ label: string | TemplateRef<any>; value: any; disabled?: boolean; hide?: boolean; groupLabel?: string | TemplateRef<any>;}> | - | |
[nzOptionHeightPx] | 下拉菜单中每个 Option 的高度 | number | 32 | |
[nzOptionOverflowSize] | 下拉菜单中最多展示的 Option 个数,超出部分滚动 | number | 8 | |
(ngModelChange) | 选中的 nz-option 发生变化时,调用此函数 | EventEmitter<any[]> | - | |
(nzOpenChange) | 下拉菜单打开状态变化回调 | EventEmitter<boolean> | - | |
(nzScrollToBottom) | 下拉列表滚动到底部的回调 | EventEmitter<any> | - | |
(nzOnSearch) | 文本框值变化时回调 | EventEmitter<string> | - | |
(nzFocus) | focus时回调 | EventEmitter<any> | - | |
(nzBlur) | blur时回调 | EventEmitter<any> | - |
nz-optioncomponent
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzDisabled] | 是否禁用 | boolean | false |
[nzLabel] | 选中该 nz-option 后,nz-select 中显示的文字 | string | - |
[nzValue] | nz-select 中 ngModel 的值 | any | - |
[nzHide] | 是否在选项列表中隐藏改选项 | boolean | false |
[nzCustomContent] | 是否自定义在下拉菜单中的Template内容,当为 true 时,nz-option 包裹的内容将直接渲染在下拉菜单中 | boolean | false |
nz-option-groupcomponent
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzLabel] | 组名 | string | TemplateRef<void> | - |
方法
nz-selectcomponent
名称 | 说明 |
---|---|
blur() | 取消焦点 |
focus() | 获取焦点 |