Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

md-autocomplete angular2 getting data from server (with a service)

I would like to fetch data from server using Autocomplete Component with angular2 / material2. (https://material.angular.io/components/component/autocomplete)

ts

  emailCtrl: FormControl;
  filteredEmails: any;

  constructor(
    private companieService: CompanieService,
  ) {
    this.emailCtrl = new FormControl();
    this.filteredEmails = this.emailCtrl.valueChanges
        .startWith(null)
        .map(email => this.filterEmails(email));
  }


  filterEmails(email: string) {
    this.userService.getUsersByEmail(email)
      .subscribe(
        res => {
          return res
        },
        error => {
          console.log(error);
        }
      )
  }

html

    <md-input-container>
      <input mdInput placeholder="Email" [mdAutocomplete]="auto" [formControl]="emailCtrl" [(ngModel)]="fetchedUser.profile.email">
    </md-input-container>

    <md-autocomplete #auto="mdAutocomplete">
      <md-option *ngFor="let email of filteredEmails | async" [value]="email">
        {{email}}
      </md-option>
    </md-autocomplete>

Service: userService.getUsersByEmail(email) is pulling this kind of data:

 ['[email protected]','[email protected]','[email protected]']

I have no errors but no results in the autocomplete. I see in debugger of chrome (tab network) Data are pulled correctly for each changes in the input

like image 614
Alan Avatar asked Apr 23 '17 19:04

Alan


2 Answers

This is how i done.

.html

       <input formControlName="search" [mdAutocomplete]="auto" type="text" class="form-control">
 <md-autocomplete #auto="mdAutocomplete">
     <md-option *ngFor="let data of filteredData | async" [value]="data.text">
    {{ data.text }}
     </md-option>
 </md-autocomplete>

.ts

 filteredData: Observable<any[]>; // async pipe needs to be an Observable
 myContent: any[] = [];

 this.filteredData = this.myformGroup.get('search').valueChanges
 .debounceTime(400)
 .switchMap(value => {

  // get data from the server. my response is an array [{id:1, text:'hello world'}] as an Observable
  return  this.myApiService.getSearch(value); 

}).map(res => this.myContent = res);

let me know if this works for you.

like image 131
Robin Avatar answered Nov 16 '22 01:11

Robin


ill give you my example that i usually use,

this.SearchForm.controls['city_id'].valueChanges
  .debounceTime(CONFIG.DEBOUNCE_TIME)
  .subscribe(name => {
    this.domain = [['name', 'ilike', name]];
    this.DataService.getAutoComplete('res.city', this.domain)
      .subscribe(res => {
        return this._filteredCity = res['result']['records']
    })
  })

HTML

<div class="mb-1 ml-1 mt-1" fxFlex="30">
  <md-input-container style="width: 100%">
    <input mdInput placeholder="Kota" (blur)="checkAutoComplete('city_id')" [mdAutocomplete]="city_id" [required]="true" [formControl]="SearchForm.controls['city_id']">
  </md-input-container>
  <md-autocomplete #city_id="mdAutocomplete" [displayWith]="displayFn">
    <md-option *ngFor="let city of _filteredCity" [value]="city">
      <span>{{ city.name }}</span>
    </md-option>
  </md-autocomplete>
  <div *ngIf="SearchForm.controls['city_id'].hasError('required') && SearchForm.controls['city_id'].touched" class="mat-text-warn text-sm">Kolom ini wajib diisi.</div>
</div>

just like that

like image 45
kazuyahiko Avatar answered Nov 16 '22 00:11

kazuyahiko