Angular Material 2 AutoFill with Angular 5

I am afraid by adding an autocomplete selection box to my site. I would like the autocomplete select box to be populated with values ​​from my mongoDB. To get these values, I use my function:

component.ts

this.availableFirmware = [];

this.terminalService.getFirmware().subscribe(firmware => {
  this.availableFirmware = firmware.firmware;

component.html

<select class="form-control" id="sel2" [(ngModel)]="firmware" name="firmware">
    <option *ngFor="let firmware of availableFirmware" [value]="firmware._id">
        {{firmware.name}}
    </option>
</select>

this still works, but I need the field to be an autocomplete field that searches for everything. Therefore, if my array would look like this:

[
 'John Doe',
 'Christian Bale'
 'Jenny Doehler'
]

I want the function to return John Doe and Jenny Doehler when I type oe.

, , angular 2 http://material.angular.io. , , .. Pp. 'm , MongoDB.

, - !


, : →

this.availableFirmware = [];

    this.terminalService.getFirmware().subscribe(firmware => {
      this.availableFirmware = firmware.firmware;
      console.log(this.availableFirmware);
    });

: enter image description here, . , , this.availableFirmware , , - , .

+4
2

, .

StackBlitz, , , , > -1 === 0.

ngOnInit() {
  this.filteredOptions = this.myControl.valueChanges
    .pipe(
      startWith(''),
      map(val => this.filter(val))
    );
}

filter(val: string): string[] {
  return this.options.filter(option =>
    option.toLowerCase().indexOf(val.toLowerCase()) > -1);
}

( , StackBlitz , , ).

, async, filtersOptions - , .

, MongoDB init, , .

, select , , angular, angular Material StackBlitz.

<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" 
      [(ngModel)]="selectedName">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let firmware of filteredOptions | async" [value]="firmware.name">
        {{ firmware.name }}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

@Component({
  ...
})
export class MyComponent {
  myControl: FormControl = new FormControl();
  availableFirmware = [];
  filteredOptions: Observable<any[]>;
  selectedFirmware = null;
  selectedName = '';

  ngOnInit() {
    this.terminalService.getFirmware().subscribe(firmware => {
      this.availableFirmware = firmware.firmware;
    }
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(val => this.filter(val))
      );
  }

  filter(val: any): any[] {
    return this.availableFirmware.filter(firmware => {
      return firmware.name.toLowerCase().indexOf(val.toLowerCase()) > -1;
    });
  }

}
+1

:

html:

(input)="filterfirmware($event.target.value)

:

private filterItems: any[] = [];
constructor(){}

private(_name: any){
  this.filterItems = []
  this.filterItems = this.availableFirmware.filter((firmware: any) =>
       firmware.name.toLowerCase().indexOf(_name.toLowerCase()) === 0);
}

this.filterItems .

0

Source: https://habr.com/ru/post/1690705/


All Articles