首页 文章

mat自动完成非异步数据

提问于
浏览
0

我有一个小时列表框(角度材料列表框,由数字1到12组成),我将转换为自动完成的垫子 .

列表框是 not in a reactive form .

下面是我创建模板的html,用于引入静态列表选项 .

<table>
  <tr>
    <td>
      <mat-form-field>
        <input type="text" [(ngModel)]="fromCreateDateTime.Hour" [ngModelOptions]="{standalone: true}" placeholder="Hour" matInput [matAutocomplete]="autoFromCreateDateTimeHour">

        <mat-autocomplete #autoFromCreateDateTimeHour="matAutocomplete" placeholder="Hour" [displayWith]="displayFn">
          <mat-option *ngFor="let hour of hoursList" [value]=" hour ">{{hour.name}}
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>
    </td>
  </tr>
</table>

这里 hoursList 是一个如下定义的数组 . 这不是一个可观察的 .

hoursList: Array, any > =
  export const hoursList: Array < any > = [{
    name: "00",
    value: 0
  }, {
    name: "01",
    value: 1
  }, {
    name: "02",
    value: 2
  }, {
    name: "03",
    value: 3
  }, {
    name: "04",
    value: 4
  }, {
    name: "05",
    value: 5
  }, {
    name: "06",
    value: 6
  }, {
    name: "07",
    value: 7
  }, {
    name: "08",
    value: 8
  }, {
    name: "09",
    value: 9
  }, {
    name: "10",
    value: 10
  }, {
    name: "11",
    value: 11
  }, {
    name: "12",
    value: 12
  }];

enter image description here

如何将过滤器(在mat输入中键入)应用于mat自动完成,因为此处的数据是非异步数据 .

1 回答

  • 0

    如果你考虑使用一个字符串数组,那么非常简单:

    import {Component, OnInit} from '@angular/core';
    import {FormControl} from '@angular/forms';
    import {Observable} from 'rxjs';
    import {map, startWith} from 'rxjs/operators';
    
    @Component({
      selector: 'autocomplete-filter-example',
      templateUrl: 'autocomplete-filter-example.html',
      styleUrls: ['autocomplete-filter-example.css'],
    })
    export class AutocompleteFilterExample implements OnInit {
      myControl = new FormControl();
      options: string[] = [
        "00",
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
      ];
    
      filteredOptions: Observable < string[] > ;
    
      ngOnInit() {
        this.filteredOptions = this.myControl.valueChanges
          .pipe(
            startWith(''),
            map(value => this._filter(value))
          );
      }
    
      private _filter(value: string): string[] {
        const filterValue = value.toLowerCase();
        return this.options.filter(
          option => option.toLowerCase().includes(filterValue)
        );
      }
    
    }
    

    而你的模板:

    <form class="example-form">
      <mat-form-field class="example-full-width">
        <input type="text" 
          placeholder="Pick one" 
          aria-label="Number" 
          matInput 
          [formControl]="myControl" 
          [matAutocomplete]="auto">
        <mat-autocomplete #auto="matAutocomplete">
          <mat-option 
            *ngFor="let option of filteredOptions | async" 
            [value]="option">
            {{option}}
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>
    </form>
    

    这是你的参考Sample StackBlitz .

相关问题