Select选择器

    • 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
    • 当选项少时(少于 5 项),建议直接将选项平铺,使用 Radio 是更好的选择。

    想要了解更多关于单独引入组件的内容,可以在页面进行查看。

    基本使用

    基本使用。

    1. @Component({
    2. selector: 'nz-demo-select-basic',
    3. template: `
    4. <div>
    5. <nz-select style="width: 120px;" [(ngModel)]="selectedValue" nzAllowClear nzPlaceHolder="Choose">
    6. <nz-option nzValue="jack" nzLabel="Jack"></nz-option>
    7. <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
    8. <nz-option nzValue="disabled" nzLabel="Disabled" nzDisabled></nz-option>
    9. </nz-select>
    10. <nz-select style="width: 120px;" [ngModel]="'lucy'" nzDisabled>
    11. <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
    12. </nz-select>
    13. <nz-select style="width: 120px;" [ngModel]="'lucy'" nzLoading>
    14. <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
    15. </nz-select>
    16. </div>
    17. `,
    18. styles: [
    19. `
    20. nz-select {
    21. margin-right: 8px;
    22. }
    23. `
    24. ]
    25. })
    26. export class NzDemoSelectBasicComponent {
    27. selectedValue = 'lucy';
    28. }

    Select选择器 - 图2

    多选,从已有条目中选择,例子中通过 nzMaxTagCount 限制最多显示3个选项。

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-multiple',
    4. template: `
    5. <nz-select
    6. [nzMaxTagCount]="3"
    7. [nzMaxTagPlaceholder]="tagPlaceHolder"
    8. style="width: 100%"
    9. nzMode="multiple"
    10. nzPlaceHolder="Please select"
    11. [(ngModel)]="listOfSelectedValue"
    12. >
    13. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
    14. </nz-select>
    15. <ng-template #tagPlaceHolder let-selectedList> and {{ selectedList.length }} more selected </ng-template>
    16. `
    17. })
    18. export class NzDemoSelectMultipleComponent implements OnInit {
    19. listOfOption: Array<{ label: string; value: string }> = [];
    20. listOfSelectedValue = ['a10', 'c12'];
    21. ngOnInit(): void {
    22. const children: Array<{ label: string; value: string }> = [];
    23. for (let i = 10; i < 36; i++) {
    24. children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
    25. }
    26. this.listOfOption = children;
    27. }
    28. }

    标签

    tags select,随意输入的内容(scroll the menu)

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-tags',
    4. template: `
    5. <nz-select nzMode="tags" style="width: 100%;" nzPlaceHolder="Tag Mode" [(ngModel)]="listOfTagOptions">
    6. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"> </nz-option>
    7. </nz-select>
    8. `
    9. })
    10. export class NzDemoSelectTagsComponent implements OnInit {
    11. listOfOption: Array<{ label: string; value: string }> = [];
    12. listOfTagOptions = [];
    13. ngOnInit(): void {
    14. const children: Array<{ label: string; value: string }> = [];
    15. for (let i = 10; i < 36; i++) {
    16. children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
    17. }
    18. this.listOfOption = children;
    19. }
    20. }

    Select选择器 - 图4

    省市联动是典型的例子。

    推荐使用 Cascader 组件。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-coordinate',
    4. template: `
    5. <div>
    6. <nz-select style="width: 120px;" [(ngModel)]="selectedProvince" (ngModelChange)="provinceChange($event)">
    7. <nz-option *ngFor="let p of provinceData" [nzValue]="p" [nzLabel]="p"></nz-option>
    8. </nz-select>
    9. <nz-select style="width: 120px;" [(ngModel)]="selectedCity">
    10. <nz-option *ngFor="let c of cityData[selectedProvince]" [nzValue]="c" [nzLabel]="c"></nz-option>
    11. </nz-select>
    12. </div>
    13. `,
    14. styles: [
    15. `
    16. nz-select {
    17. margin-right: 8px;
    18. }
    19. `
    20. ]
    21. })
    22. export class NzDemoSelectCoordinateComponent {
    23. selectedProvince = 'Zhejiang';
    24. selectedCity = 'Hangzhou';
    25. provinceData = ['Zhejiang', 'Jiangsu'];
    26. cityData: { [place: string]: string[] } = {
    27. Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
    28. Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang']
    29. };
    30. provinceChange(value: string): void {
    31. this.selectedCity = this.cityData[value][0];
    32. }
    33. }

    ngModel 取到的值为选中 nz-optionnzValue 值,当 nzValue 传入为一个对象时,ngModel 获取的值也是一个对象,compareWith 的用法参见 这里.

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-label-in-value',
    4. template: `
    5. <p>The selected option's age is {{ selectedValue?.age }}</p>
    6. <br />
    7. <nz-select
    8. style="width: 120px;"
    9. [compareWith]="compareFn"
    10. [(ngModel)]="selectedValue"
    11. (ngModelChange)="log($event)"
    12. nzPlaceHolder="Choose"
    13. >
    14. <nz-option *ngFor="let option of optionList" [nzValue]="option" [nzLabel]="option.label"></nz-option>
    15. </nz-select>
    16. `
    17. })
    18. export class NzDemoSelectLabelInValueComponent {
    19. optionList = [{ label: 'Lucy', value: 'lucy', age: 20 }, { label: 'Jack', value: 'jack', age: 22 }];
    20. selectedValue = { label: 'Jack', value: 'jack', age: 22 };
    21. // tslint:disable-next-line:no-any
    22. compareFn = (o1: any, o2: any) => (o1 && o2 ? o1.value === o2.value : o1 === o2);
    23. log(value: { label: string; value: string; age: number }): void {
    24. console.log(value);
    25. }
    26. }

    Select选择器 - 图6

    一个带有远程搜索,节流控制,请求时序控制,加载状态的多选示例。

    隐藏已选择选项

    隐藏下拉列表中已选择的选项。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-hide-selected',
    4. template: `
    5. <nz-select
    6. style="width: 100%"
    7. nzMode="multiple"
    8. nzPlaceHolder="Inserted are removed"
    9. [(ngModel)]="listOfSelectedValue"
    10. >
    11. <ng-container *ngFor="let option of listOfOption">
    12. <nz-option [nzLabel]="option" [nzValue]="option" *ngIf="isNotSelected(option)"></nz-option>
    13. </ng-container>
    14. </nz-select>
    15. `
    16. })
    17. export class NzDemoSelectHideSelectedComponent {
    18. listOfOption = ['Apples', 'Nails', 'Bananas', 'Helicopters'];
    19. listOfSelectedValue: string[] = [];
    20. isNotSelected(value: string): boolean {
    21. return this.listOfSelectedValue.indexOf(value) === -1;
    22. }
    23. }

    Select选择器 - 图8

    一个带有下拉加载远程数据的例子。

    1. import { Component, OnInit } from '@angular/core';
    2. import { Observable } from 'rxjs';
    3. import { map } from 'rxjs/operators';
    4. @Component({
    5. selector: 'nz-demo-select-scroll-load',
    6. template: `
    7. <nz-select
    8. style="width: 100%;"
    9. [(ngModel)]="selectedUser"
    10. (nzScrollToBottom)="loadMore()"
    11. nzPlaceHolder="Select users"
    12. nzAllowClear
    13. >
    14. <nz-option *ngFor="let o of optionList" [nzValue]="o" [nzLabel]="o"></nz-option>
    15. <nz-option *ngIf="isLoading" nzDisabled nzCustomContent>
    16. <i nz-icon nzType="loading" class="loading-icon"></i> Loading Data...
    17. </nz-option>
    18. </nz-select>
    19. `,
    20. styles: [
    21. `
    22. .loading-icon {
    23. margin-right: 8px;
    24. }
    25. `
    26. ]
    27. })
    28. export class NzDemoSelectScrollLoadComponent implements OnInit {
    29. randomUserUrl = 'https://api.randomuser.me/?results=10';
    30. optionList: string[] = [];
    31. selectedUser = null;
    32. isLoading = false;
    33. // tslint:disable:no-any
    34. getRandomNameList: Observable<string[]> = this.http
    35. .get(`${this.randomUserUrl}`)
    36. .pipe(map((res: any) => res.results))
    37. .pipe(
    38. map((list: any) => {
    39. return list.map((item: any) => `${item.name.first}`);
    40. })
    41. );
    42. // tslint:enable:no-any
    43. loadMore(): void {
    44. this.isLoading = true;
    45. this.getRandomNameList.subscribe(data => {
    46. this.isLoading = false;
    47. this.optionList = [...this.optionList, ...data];
    48. });
    49. }
    50. constructor(private http: HttpClient) {}
    51. ngOnInit(): void {
    52. this.loadMore();
    53. }
    54. }

    带搜索框

    展开后可对选项进行搜索。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-search',
    4. template: `
    5. <nz-select
    6. style="width: 200px;"
    7. nzShowSearch
    8. nzAllowClear
    9. nzPlaceHolder="Select a person"
    10. [(ngModel)]="selectedValue"
    11. >
    12. <nz-option nzLabel="Jack" nzValue="jack"></nz-option>
    13. <nz-option nzLabel="Lucy" nzValue="lucy"></nz-option>
    14. <nz-option nzLabel="Tom" nzValue="tom"></nz-option>
    15. </nz-select>
    16. `
    17. })
    18. export class NzDemoSelectSearchComponent {
    19. selectedValue = null;
    20. }

    Select选择器 - 图10

    三种大小的选择框,当 nzSize 分别为 largesmall 时,输入框高度为 40px24px ,默认高度为 32px

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-size',
    4. template: `
    5. <nz-radio-group [(ngModel)]="size">
    6. <label nz-radio-button nzValue="large"><span>Large</span></label>
    7. <label nz-radio-button nzValue="default"><span>Default</span></label>
    8. <label nz-radio-button nzValue="small"><span>Small</span></label>
    9. </nz-radio-group>
    10. <br /><br />
    11. <nz-select style="width: 200px;" [(ngModel)]="singleValue" [nzSize]="size">
    12. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
    13. </nz-select>
    14. <br /><br />
    15. <nz-select style="width: 200px;" [(ngModel)]="singleValue" [nzSize]="size" nzShowSearch>
    16. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
    17. </nz-select>
    18. <br /><br />
    19. style="width: 100%"
    20. [(ngModel)]="multipleValue"
    21. [nzSize]="size"
    22. nzMode="multiple"
    23. nzPlaceHolder="Please select"
    24. >
    25. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
    26. </nz-select>
    27. <br /><br />
    28. <nz-select style="width: 100%" [(ngModel)]="tagValue" [nzSize]="size" nzMode="tags" nzPlaceHolder="Please select">
    29. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"></nz-option>
    30. </nz-select>
    31. `
    32. })
    33. export class NzDemoSelectSizeComponent implements OnInit {
    34. listOfOption: Array<{ label: string; value: string }> = [];
    35. size = 'default';
    36. singleValue = 'a10';
    37. multipleValue = ['a10', 'c12'];
    38. tagValue = ['a10', 'c12', 'tag'];
    39. ngOnInit(): void {
    40. const children: Array<{ label: string; value: string }> = [];
    41. for (let i = 10; i < 36; i++) {
    42. children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
    43. }
    44. this.listOfOption = children;
    45. }
    46. }

    nz-option-group 进行选项分组。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-optgroup',
    4. template: `
    5. <nz-select style="width: 120px;" [(ngModel)]="selectedValue" nzAllowClear nzPlaceHolder="Choose">
    6. <nz-option-group nzLabel="Manager">
    7. <nz-option nzValue="jack" nzLabel="Jack"></nz-option>
    8. <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
    9. </nz-option-group>
    10. <nz-option-group nzLabel="Engineer">
    11. <nz-option nzValue="Tom" nzLabel="tom"></nz-option>
    12. </nz-option-group>
    13. </nz-select>
    14. `
    15. })
    16. export class NzDemoSelectOptgroupComponent {
    17. selectedValue = 'lucy';
    18. }

    Select选择器 - 图12

    搜索框

    搜索和远程数据结合。

    试下复制 露西,杰克 到输入框里。只在 tags 和 multiple 模式下可用。

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-automatic-tokenization',
    4. template: `
    5. <nz-select
    6. nzMode="tags"
    7. [nzTokenSeparators]="[',']"
    8. style="width: 100%;"
    9. [(ngModel)]="listOfTagOptions"
    10. nzPlaceHolder="automatic tokenization"
    11. >
    12. <nz-option *ngFor="let option of listOfOption" [nzLabel]="option.label" [nzValue]="option.value"> </nz-option>
    13. </nz-select>
    14. `
    15. })
    16. export class NzDemoSelectAutomaticTokenizationComponent implements OnInit {
    17. listOfOption: Array<{ label: string; value: string }> = [];
    18. listOfTagOptions = [];
    19. ngOnInit(): void {
    20. const children: Array<{ label: string; value: string }> = [];
    21. children.push({ label: i.toString(36) + i, value: i.toString(36) + i });
    22. }
    23. this.listOfOption = children;
    24. }
    25. }

    Select选择器 - 图14

    扩展菜单

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

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-custom-dropdown-menu',
    4. template: `
    5. <nz-select style="width: 120px;" nzShowSearch nzAllowClear [ngModel]="'lucy'" [nzDropdownRender]="render">
    6. <nz-option nzValue="jack" nzLabel="Jack"></nz-option>
    7. <nz-option nzValue="lucy" nzLabel="Lucy"></nz-option>
    8. </nz-select>
    9. <ng-template #render>
    10. <nz-divider style="margin: 4px 0;"></nz-divider>
    11. <div style="padding: 8px; cursor: pointer"><i nz-icon nzType="plus"></i> Add item</div>
    12. </ng-template>
    13. `
    14. })
    15. export class NzDemoSelectCustomDropdownMenuComponent {}

    通过 nzCustomContent 自定义 nz-option 显示的内容。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-custom-content',
    4. template: `
    5. <nz-select style="width: 200px;" nzShowSearch nzAllowClear nzPlaceHolder="Select OS" [(ngModel)]="selectedOS">
    6. <nz-option nzCustomContent nzLabel="Windows" nzValue="windows"
    7. ><i nz-icon nzType="windows"></i> Windows</nz-option
    8. >
    9. <nz-option nzCustomContent nzLabel="Mac" nzValue="mac"><i nz-icon nzType="apple"></i> Mac</nz-option>
    10. <nz-option nzCustomContent nzLabel="Android" nzValue="android"
    11. ><i nz-icon nzType="android"></i> Android</nz-option
    12. >
    13. </nz-select>
    14. `
    15. })
    16. export class NzDemoSelectCustomContentComponent {
    17. selectedOS = null;
    18. }

    Select选择器 - 图16

    自定义选择器内容

    通过 nzCustomTemplate 自定义 nz-select 显示的内容。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-select-custom-template',
    4. template: `
    5. <nz-select
    6. style="width: 200px;"
    7. nzAllowClear
    8. nzPlaceHolder="Select OS"
    9. [(ngModel)]="selectedOS"
    10. [nzCustomTemplate]="custom"
    11. >
    12. <nz-option nzCustomContent nzLabel="Windows" nzValue="windows"
    13. ><i nz-icon nzType="windows"></i> Windows</nz-option
    14. >
    15. <nz-option nzCustomContent nzLabel="Mac" nzValue="mac"><i nz-icon nzType="apple"></i> Mac</nz-option>
    16. <nz-option nzCustomContent nzLabel="Android" nzValue="android"
    17. ><i nz-icon nzType="android"></i> Android</nz-option
    18. >
    19. </nz-select>
    20. <ng-template #custom let-selected>
    21. <span>Label: {{ selected.nzLabel }} Value: {{ selected.nzValue }}</span>
    22. </ng-template>
    23. `
    24. })
    25. export class NzDemoSelectCustomTemplateComponent {
    26. selectedOS = null;
    27. }
    1. <nz-select>
    2. </nz-select>