Autocomplete自动完成

    需要自动完成时。

    基本使用

    基本使用。通过 设置自动完成的数据源

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-basic',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <input placeholder="input here" nz-input [(ngModel)]="inputValue" (input)="onInput($event)" [nzAutocomplete]="auto" />
    8. <nz-autocomplete [nzDataSource]="options" nzBackfill #auto></nz-autocomplete>
    9. </div>
    10. `
    11. })
    12. export class NzDemoAutoCompleteBasicComponent {
    13. inputValue?: string;
    14. options: string[] = [];
    15. onInput(event: Event): void {
    16. const value = (event.target as HTMLInputElement).value;
    17. this.options = value ? [value, value + value, value + value + value] : [];
    18. }
    19. }

    Autocomplete自动完成 - 图4

    自定义选项

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-options',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <input placeholder="input here" nz-input [(ngModel)]="inputValue" (input)="onInput($event)" [nzAutocomplete]="auto" />
    8. <nz-autocomplete #auto>
    9. <nz-auto-option *ngFor="let option of options" [nzValue]="option">{{ option }}</nz-auto-option>
    10. </nz-autocomplete>
    11. </div>
    12. `
    13. })
    14. export class NzDemoAutoCompleteOptionsComponent {
    15. inputValue?: string;
    16. options: string[] = [];
    17. onInput(e: Event): void {
    18. const value = (e.target as HTMLInputElement).value;
    19. if (!value || value.indexOf('@') >= 0) {
    20. this.options = [];
    21. } else {
    22. this.options = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
    23. }
    24. }
    25. }

    不区分大小写

    不区分大小写的 AutoComplete

    Autocomplete自动完成 - 图6

    查询模式 - 不确定类目

    查询模式: 不确定类目 示例。

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-auto-complete-uncertain-category',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <div class="example-input">
    7. <nz-input-group nzSearch nzSize="large" [nzAddOnAfter]="suffixIconButton">
    8. <input placeholder="input here" nz-input [(ngModel)]="inputValue" (input)="onChange($event)" [nzAutocomplete]="auto" />
    9. </nz-input-group>
    10. <ng-template #suffixIconButton>
    11. <button nz-button nzType="primary" nzSize="large" nzSearch>
    12. <i nz-icon nzType="search" nzTheme="outline"></i>
    13. </button>
    14. </ng-template>
    15. <nz-autocomplete #auto>
    16. <nz-auto-option class="global-search-item" *ngFor="let option of options" [nzValue]="option.category">
    17. Found {{ option.value }} on
    18. <a
    19. class="global-search-item-desc"
    20. [href]="'https://s.taobao.com/search?q=' + option.value"
    21. target="_blank"
    22. rel="noopener noreferrer"
    23. >
    24. {{ option.category }}
    25. </a>
    26. <span class="global-search-item-count">{{ option.count }} results</span>
    27. </nz-auto-option>
    28. </nz-autocomplete>
    29. </div>
    30. `,
    31. styles: [
    32. `
    33. display: flex;
    34. }
    35. .global-search-item-desc {
    36. flex: auto;
    37. text-overflow: ellipsis;
    38. overflow: hidden;
    39. }
    40. .global-search-item-count {
    41. flex: none;
    42. `
    43. ]
    44. })
    45. export class NzDemoAutoCompleteUncertainCategoryComponent {
    46. inputValue?: string;
    47. options: Array<{ value: string; category: string; count: number }> = [];
    48. onChange(e: Event): void {
    49. const value = (e.target as HTMLInputElement).value;
    50. this.options = new Array(this.getRandomInt(5, 15))
    51. .join('.')
    52. .split('.')
    53. .map((_item, idx) => ({
    54. value,
    55. category: `${value}${idx}`,
    56. count: this.getRandomInt(200, 100)
    57. }));
    58. }
    59. private getRandomInt(max: number, min: number = 0): number {
    60. return Math.floor(Math.random() * (max - min + 1)) + min;
    61. }
    62. }

    使用对象类型选项

    nzValuengModel 类型为 object 时使用 compareWith().

    1. import { Component, ViewEncapsulation } from '@angular/core';
    2. interface Option {
    3. label: string;
    4. value: string;
    5. age: number;
    6. }
    7. @Component({
    8. selector: 'nz-demo-auto-complete-object-value',
    9. encapsulation: ViewEncapsulation.None,
    10. template: `
    11. <div class="example-input">
    12. <input placeholder="input here" nz-input [(ngModel)]="inputValue" [nzAutocomplete]="auto" />
    13. <nz-autocomplete #auto [compareWith]="compareFun">
    14. <nz-auto-option *ngFor="let option of options" [nzValue]="option" [nzLabel]="option.label">
    15. {{ option.label }}
    16. </nz-auto-option>
    17. </nz-autocomplete>
    18. </div>
    19. `
    20. })
    21. export class NzDemoAutoCompleteObjectValueComponent {
    22. inputValue: Option = { label: 'Lucy', value: 'lucy', age: 20 };
    23. options: Option[] = [
    24. { label: 'Lucy', value: 'lucy', age: 20 },
    25. { label: 'Jack', value: 'jack', age: 22 }
    26. ];
    27. compareFun = (o1: Option | string, o2: Option) => {
    28. if (o1) {
    29. return typeof o1 === 'string' ? o1 === o2.label : o1.value === o2.value;
    30. } else {
    31. return false;
    32. }
    33. };
    34. }

    自定义输入组件

    自定义输入组件。

    Autocomplete自动完成 - 图9

    查询模式: 确定类目 示例。

    1. import { Component, OnInit, ViewEncapsulation } from '@angular/core';
    2. export interface AutocompleteOptionGroups {
    3. title: string;
    4. count?: number;
    5. children?: AutocompleteOptionGroups[];
    6. }
    7. @Component({
    8. selector: 'nz-demo-auto-complete-certain-category',
    9. encapsulation: ViewEncapsulation.None,
    10. template: `
    11. <div class="example-input">
    12. <nz-input-group nzSize="large" [nzSuffix]="suffixIcon">
    13. <input placeholder="input here" nz-input [(ngModel)]="inputValue" (ngModelChange)="onChange($event)" [nzAutocomplete]="auto" />
    14. <ng-template #suffixIcon>
    15. <i nz-icon nzType="search"></i>
    16. </ng-template>
    17. <nz-autocomplete #auto>
    18. <nz-auto-optgroup *ngFor="let group of optionGroups" [nzLabel]="groupTitle">
    19. <ng-template #groupTitle>
    20. <span
    21. >{{ group.title }}
    22. <a class="more-link" href="https://www.google.com/search?q=ng+zorro" target="_blank">更多</a>
    23. </span>
    24. </ng-template>
    25. <nz-auto-option *ngFor="let option of group.children" [nzLabel]="option.title" [nzValue]="option.title">
    26. {{ option.title }}
    27. <span class="certain-search-item-count">{{ option.count }} 人 关注</span>
    28. </nz-auto-option>
    29. </nz-auto-optgroup>
    30. </div>
    31. `,
    32. styles: [
    33. `
    34. .certain-search-item-count {
    35. position: absolute;
    36. color: #999;
    37. right: 16px;
    38. }
    39. .more-link {
    40. float: right;
    41. }
    42. `
    43. ]
    44. })
    45. export class NzDemoAutoCompleteCertainCategoryComponent implements OnInit {
    46. inputValue?: string;
    47. optionGroups: AutocompleteOptionGroups[] = [];
    48. constructor() {}
    49. onChange(value: string): void {
    50. console.log(value);
    51. }
    52. ngOnInit(): void {
    53. setTimeout(() => {
    54. this.optionGroups = [
    55. {
    56. title: '话题',
    57. children: [
    58. {
    59. title: 'AntDesign',
    60. count: 10000
    61. },
    62. {
    63. title: 'AntDesign UI',
    64. count: 10600
    65. }
    66. ]
    67. },
    68. {
    69. title: '问题',
    70. children: [
    71. {
    72. title: 'AntDesign UI 有多好',
    73. count: 60100
    74. },
    75. {
    76. title: 'AntDesign 是啥',
    77. count: 30010
    78. }
    79. ]
    80. },
    81. {
    82. title: '文章',
    83. children: [
    84. {
    85. title: 'AntDesign 是一个设计语言',
    86. count: 100000
    87. }
    88. ]
    89. }
    90. ];
    91. }, 1000);
    92. }
    93. }
    1. <input nz-input [(ngModel)]="value" [nzAutocomplete]="auto">
    2. <nz-autocomplete [nzDataSource]="['12345', '23456', '34567']" #auto></nz-autocomplete>