select 选择器

下拉选择器。

代码演示

select 选择器 - 图1

基础样例

最简单的用法。

  1. import { Component, ViewChild } from '@angular/core';
  2. import { SFSchema, SFSelectWidgetSchema, SFComponent } from '@delon/form';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. import { of } from 'rxjs';
  5. import { delay } from 'rxjs/operators';
  6. @Component({
  7. selector: 'form-select-simple',
  8. template: `
  9. <sf #sf [schema]="schema" (formSubmit)="submit($event)"></sf>
  10. <button nz-button (click)="updateStatus()">Update Status</button>
  11. `,
  12. })
  13. export class FormSelectSimpleComponent {
  14. @ViewChild('sf', { static: false }) private sf: SFComponent;
  15. schema: SFSchema = {
  16. properties: {
  17. status: {
  18. type: 'string',
  19. title: '状态',
  20. enum: [
  21. { label: '待支付', value: 'WAIT_BUYER_PAY' },
  22. { label: '已支付', value: 'TRADE_SUCCESS' },
  23. { label: '交易完成', value: 'TRADE_FINISHED' },
  24. ],
  25. default: 'WAIT_BUYER_PAY',
  26. ui: {
  27. widget: 'select',
  28. } as SFSelectWidgetSchema,
  29. },
  30. // 标签
  31. tags: {
  32. type: 'string',
  33. title: '标签',
  34. enum: [
  35. { label: '待支付', value: 'WAIT_BUYER_PAY' },
  36. { label: '已支付', value: 'TRADE_SUCCESS' },
  37. { label: '交易完成', value: 'TRADE_FINISHED' },
  38. ],
  39. ui: {
  40. widget: 'select',
  41. mode: 'tags',
  42. } as SFSelectWidgetSchema,
  43. default: null,
  44. },
  45. // 异步数据
  46. async: {
  47. type: 'string',
  48. title: 'Async',
  49. default: 'WAIT_BUYER_PAY',
  50. ui: {
  51. widget: 'select',
  52. asyncData: () =>
  53. of([
  54. {
  55. label: '订单状态',
  56. group: true,
  57. children: [
  58. { label: '待支付', value: 'WAIT_BUYER_PAY' },
  59. { label: '已支付', value: 'TRADE_SUCCESS' },
  60. { label: '交易完成', value: 'TRADE_FINISHED' },
  61. ],
  62. },
  63. ]).pipe(delay(1200)),
  64. } as SFSelectWidgetSchema,
  65. },
  66. },
  67. };
  68. constructor(public msg: NzMessageService) {}
  69. submit(value: any) {
  70. this.msg.success(JSON.stringify(value));
  71. }
  72. updateStatus() {
  73. const statusProperty = this.sf.getProperty('/status')!;
  74. statusProperty.schema.enum = ['1', '2', '3'];
  75. statusProperty.widget.reset('2');
  76. }
  77. }

select 选择器 - 图2

联动

省市联动是典型的例子。

  1. import { Component, ViewChild } from '@angular/core';
  2. import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. import { of } from 'rxjs';
  5. import { delay, tap } from 'rxjs/operators';
  6. @Component({
  7. selector: 'form-select-coordinate',
  8. template: ` <sf #sf [schema]="schema" [formData]="data" (formSubmit)="submit($event)"></sf> `,
  9. })
  10. export class FormSelectCoordinateComponent {
  11. @ViewChild('sf', { static: false }) private sf: SFComponent;
  12. data = {
  13. province: 'Zhejiang',
  14. city: 'Ningbo',
  15. };
  16. private cityData: { [place: string]: string[] } = {
  17. Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
  18. Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang'],
  19. };
  20. schema: SFSchema = {
  21. properties: {
  22. province: {
  23. type: 'string',
  24. title: 'Province',
  25. ui: {
  26. widget: 'select',
  27. asyncData: () =>
  28. of(['Zhejiang', 'Jiangsu']).pipe(
  29. delay(100),
  30. tap(() => this.updateCity(this.data.province, this.data.city)),
  31. ),
  32. change: i => this.updateCity(i),
  33. } as SFSelectWidgetSchema,
  34. },
  35. city: {
  36. type: 'string',
  37. title: 'City',
  38. ui: {
  39. widget: 'select',
  40. } as SFSelectWidgetSchema,
  41. },
  42. },
  43. };
  44. constructor(public msg: NzMessageService) {}
  45. submit(value: any) {
  46. this.msg.success(JSON.stringify(value));
  47. }
  48. private updateCity(province: string, city: string = ''): void {
  49. const cityProperty = this.sf.getProperty('/city')!;
  50. const items = this.cityData[province];
  51. cityProperty.schema.enum = items;
  52. cityProperty.widget.reset(city || items[0]);
  53. }
  54. }

select 选择器 - 图3

扩展菜单

使用 dropdownRender 对下拉菜单进行自由扩展。

  1. import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
  2. import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. @Component({
  5. selector: 'form-select-custom-dropdown-menu',
  6. template: `
  7. <sf #sf *ngIf="schema" [schema]="schema" (formSubmit)="submit($event)"></sf>
  8. <ng-template #dropdownRender>
  9. <nz-divider></nz-divider>
  10. <div class="container">
  11. <input type="text" nz-input #inputElement />
  12. <a class="add-item" (click)="addItem(inputElement)"><i nz-icon nzType="plus"></i> Add item</a>
  13. </div>
  14. </ng-template>
  15. `,
  16. styles: [
  17. `
  18. nz-divider {
  19. margin: 4px 0;
  20. }
  21. .container {
  22. display: flex;
  23. flex-wrap: nowrap;
  24. padding: 8px;
  25. }
  26. input {
  27. }
  28. .add-item {
  29. flex: 0 0 auto;
  30. padding: 8px;
  31. display: block;
  32. }
  33. `,
  34. ],
  35. })
  36. export class FormSelectCustomDropdownMenuComponent implements OnInit {
  37. @ViewChild('sf', { static: false }) private sf: SFComponent;
  38. @ViewChild('dropdownRender', { static: true }) private dropdownRender!: TemplateRef<void>;
  39. schema: SFSchema;
  40. statusList: string[] = ['1', '2', '3'];
  41. constructor(private msg: NzMessageService) {}
  42. submit(value: any) {
  43. this.msg.success(JSON.stringify(value));
  44. }
  45. ngOnInit(): void {
  46. this.schema = {
  47. properties: {
  48. status: {
  49. type: 'string',
  50. title: '状态',
  51. enum: this.statusList,
  52. default: '1',
  53. ui: {
  54. widget: 'select',
  55. dropdownRender: this.dropdownRender,
  56. } as SFSelectWidgetSchema,
  57. },
  58. },
  59. };
  60. }
  61. addItem(input: HTMLInputElement): void {
  62. this.statusList.push(input.value);
  63. const statusProperty = this.sf.getProperty('/status')!;
  64. statusProperty.schema.enum = this.statusList;
  65. this.sf.setValue('/status', input.value);
  66. }
  67. }

API

schema 属性

成员说明类型默认值
[enum]数据源SFSchemaEnumType[]-
[readOnly]禁用状态boolean-

ui 属性

成员说明类型默认值
[asyncData]异步数据源() => Observable<SFSchemaEnumType[]>-
[size]大小,等同 nzSizestring-
[compareWith]SelectControlValueAccessor 相同(o1: any, o2: any) => boolean(o1: any, o2: any) => o1===o2
[placeholder]在文字框中显示提示讯息string-
[autoClearSearchValue]是否在选中项后清空搜索框,只在 modemultipletags 时有效。booleantrue
[allowClear]支持清除booleanfalse
[borderless]是否无边框booleanfalse
[autoFocus]默认获取焦点booleanfalse
[dropdownClassName]下拉菜单的 className 属性string-
[dropdownMatchSelectWidth]下拉菜单和选择器同宽booleantrue
[dropdownStyle]下拉菜单的 style 属性object-
[serverSearch]是否使用服务端搜索,当为 true 时,将不再在前端对 nz-option 进行过滤booleanfalse
[maxMultipleCount]最多选中多少个标签numberInfinity
[mode]设置 nz-select 的模式,tags 建议增加 default: null,否则可能会遇到初始化有一个空的标签。multiple,tags,defaultdefault
[notFoundContent]当下拉列表为空时显示的内容string-
[showSearch]使单选模式可搜索booleanfalse
[onSearch]搜索内容变化回调函数,参数为搜索内容,必须返回 Promise 对象(text: string) => Promise<SFSchemaEnum[]>-
[tokenSeparators]在 tags 和 multiple 模式下自动分词的分隔符string[][]
[maxTagCount]最多显示多少个 tagnumber-
[change]选中的 nz-option 发生变化时,调用此函数(ngModel:any丨any[])=>void-
[openChange]下拉菜单打开关闭回调函数(status: boolean) => void-
[scrollToBottom]下拉菜单滚动到底部回调,可用于作为动态加载的触发条件() => void-
[customTemplate]自定义选择框的Template内容TemplateRef<{ $implicit: NzOptionComponent }>-
[suffixIcon]自定义的选择框后缀图标TemplateRef<any>, string-
[removeIcon]自定义的多选框清除图标TemplateRef<any>-
[clearIcon]自定义的多选框清空图标TemplateRef<any>-
[menuItemSelectedIcon]自定义当前选中的条目图标TemplateRef<any>-
[maxTagPlaceholder]隐藏 tag 时显示的内容TemplateRef<{ $implicit: any[] }>-
[optionHeightPx]下拉菜单中每个 Option 的高度number32
[optionOverflowSize]下拉菜单中最多展示的 Option 个数,超出部分滚动number8