select 选择器
select 选择器
下拉选择器。
代码演示
基础样例
最简单的用法。
import { Component, ViewChild } from '@angular/core';import { SFSchema, SFSelectWidgetSchema, SFComponent } from '@delon/form';import { NzMessageService } from 'ng-zorro-antd/message';import { of } from 'rxjs';import { delay } from 'rxjs/operators';@Component({ selector: 'form-select-simple', template: ` <sf #sf [schema]="schema" (formSubmit)="submit($event)"></sf> <button nz-button (click)="updateStatus()">Update Status</button> `,})export class FormSelectSimpleComponent { @ViewChild('sf', { static: false }) private sf: SFComponent; schema: SFSchema = { properties: { status: { type: 'string', title: '状态', enum: [ { label: '待支付', value: 'WAIT_BUYER_PAY' }, { label: '已支付', value: 'TRADE_SUCCESS' }, { label: '交易完成', value: 'TRADE_FINISHED' }, ], default: 'WAIT_BUYER_PAY', ui: { widget: 'select', } as SFSelectWidgetSchema, }, // 标签 tags: { type: 'string', title: '标签', enum: [ { label: '待支付', value: 'WAIT_BUYER_PAY' }, { label: '已支付', value: 'TRADE_SUCCESS' }, { label: '交易完成', value: 'TRADE_FINISHED' }, ], ui: { widget: 'select', mode: 'tags', } as SFSelectWidgetSchema, default: null, }, // 异步数据 async: { type: 'string', title: 'Async', default: 'WAIT_BUYER_PAY', ui: { widget: 'select', asyncData: () => of([ { label: '订单状态', group: true, children: [ { label: '待支付', value: 'WAIT_BUYER_PAY' }, { label: '已支付', value: 'TRADE_SUCCESS' }, { label: '交易完成', value: 'TRADE_FINISHED' }, ], }, ]).pipe(delay(1200)), } as SFSelectWidgetSchema, }, }, }; constructor(public msg: NzMessageService) {} submit(value: any) { this.msg.success(JSON.stringify(value)); } updateStatus() { const statusProperty = this.sf.getProperty('/status')!; statusProperty.schema.enum = ['1', '2', '3']; statusProperty.widget.reset('2'); }}
联动
省市联动是典型的例子。
import { Component, ViewChild } from '@angular/core';import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';import { NzMessageService } from 'ng-zorro-antd/message';import { of } from 'rxjs';import { delay, tap } from 'rxjs/operators';@Component({ selector: 'form-select-coordinate', template: ` <sf #sf [schema]="schema" [formData]="data" (formSubmit)="submit($event)"></sf> `,})export class FormSelectCoordinateComponent { @ViewChild('sf', { static: false }) private sf: SFComponent; data = { province: 'Zhejiang', city: 'Ningbo', }; private cityData: { [place: string]: string[] } = { Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'], Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang'], }; schema: SFSchema = { properties: { province: { type: 'string', title: 'Province', ui: { widget: 'select', asyncData: () => of(['Zhejiang', 'Jiangsu']).pipe( delay(100), tap(() => this.updateCity(this.data.province, this.data.city)), ), change: i => this.updateCity(i), } as SFSelectWidgetSchema, }, city: { type: 'string', title: 'City', ui: { widget: 'select', } as SFSelectWidgetSchema, }, }, }; constructor(public msg: NzMessageService) {} submit(value: any) { this.msg.success(JSON.stringify(value)); } private updateCity(province: string, city: string = ''): void { const cityProperty = this.sf.getProperty('/city')!; const items = this.cityData[province]; cityProperty.schema.enum = items; cityProperty.widget.reset(city || items[0]); }}
扩展菜单
使用 dropdownRender 对下拉菜单进行自由扩展。
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';import { NzMessageService } from 'ng-zorro-antd/message';@Component({ selector: 'form-select-custom-dropdown-menu', template: ` <sf #sf *ngIf="schema" [schema]="schema" (formSubmit)="submit($event)"></sf> <ng-template #dropdownRender> <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-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 FormSelectCustomDropdownMenuComponent implements OnInit { @ViewChild('sf', { static: false }) private sf: SFComponent; @ViewChild('dropdownRender', { static: true }) private dropdownRender!: TemplateRef<void>; schema: SFSchema; statusList: string[] = ['1', '2', '3']; constructor(private msg: NzMessageService) {} submit(value: any) { this.msg.success(JSON.stringify(value)); } ngOnInit(): void { this.schema = { properties: { status: { type: 'string', title: '状态', enum: this.statusList, default: '1', ui: { widget: 'select', dropdownRender: this.dropdownRender, } as SFSelectWidgetSchema, }, }, }; } addItem(input: HTMLInputElement): void { this.statusList.push(input.value); const statusProperty = this.sf.getProperty('/status')!; statusProperty.schema.enum = this.statusList; this.sf.setValue('/status', input.value); }}
API
schema 属性
| [enum] |
数据源 |
SFSchemaEnumType[] |
- |
|
|
|
|
| [readOnly] |
禁用状态 |
boolean |
- |
|
|
|
|
ui 属性
| [asyncData] |
异步数据源 |
() => Observable<SFSchemaEnumType[]> |
- |
|
|
|
|
| [size] |
大小,等同 nzSize |
string |
- |
|
|
|
|
| [compareWith] |
与 SelectControlValueAccessor 相同 |
(o1: any, o2: any) => boolean |
(o1: any, o2: any) => o1===o2 |
|
|
|
|
| [placeholder] |
在文字框中显示提示讯息 |
string |
- |
|
|
|
|
| [autoClearSearchValue] |
是否在选中项后清空搜索框,只在 mode 为 multiple 或 tags 时有效。 |
boolean |
true |
|
|
|
|
| [allowClear] |
支持清除 |
boolean |
false |
|
|
|
|
| [borderless] |
是否无边框 |
boolean |
false |
|
|
|
|
| [autoFocus] |
默认获取焦点 |
boolean |
false |
|
|
|
|
| [dropdownClassName] |
下拉菜单的 className 属性 |
string |
- |
|
|
|
|
| [dropdownMatchSelectWidth] |
下拉菜单和选择器同宽 |
boolean |
true |
|
|
|
|
| [dropdownStyle] |
下拉菜单的 style 属性 |
object |
- |
|
|
|
|
| [serverSearch] |
是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤 |
boolean |
false |
|
|
|
|
| [maxMultipleCount] |
最多选中多少个标签 |
number |
Infinity |
|
|
|
|
| [mode] |
设置 nz-select 的模式,tags 建议增加 default: null,否则可能会遇到初始化有一个空的标签。 |
multiple,tags,default |
default |
|
|
|
|
| [notFoundContent] |
当下拉列表为空时显示的内容 |
string |
- |
|
|
|
|
| [showSearch] |
使单选模式可搜索 |
boolean |
false |
|
|
|
|
| [onSearch] |
搜索内容变化回调函数,参数为搜索内容,必须返回 Promise 对象 |
(text: string) => Promise<SFSchemaEnum[]> |
- |
|
|
|
|
| [tokenSeparators] |
在 tags 和 multiple 模式下自动分词的分隔符 |
string[] |
[] |
|
|
|
|
| [maxTagCount] |
最多显示多少个 tag |
number |
- |
|
|
|
|
| [change] |
选中的 nz-option 发生变化时,调用此函数 |
(ngModel:any丨any[])=>void |
- |
|
|
|
|
| [openChange] |
下拉菜单打开关闭回调函数 |
(status: boolean) => void |
- |
|
|
|
|
| [scrollToBottom] |
下拉菜单滚动到底部回调,可用于作为动态加载的触发条件 |
() => void |
- |
|
|
|
|