Skip to content

Instantly share code, notes, and snippets.

@clebersonfalk
Last active January 31, 2020 13:56
Show Gist options
  • Save clebersonfalk/57513575addf80dcf8210da85a45fff5 to your computer and use it in GitHub Desktop.
Save clebersonfalk/57513575addf80dcf8210da85a45fff5 to your computer and use it in GitHub Desktop.
Angular Material Autocomplete
public buscarCidades(): void {
this.form.get('genCidadeId').valueChanges.pipe(
debounceTime(600),
filter((termo) => typeof termo === 'string'),
map(termo => termo.toString().trim()),
distinctUntilChanged(),
tap(() => this.carregandoCidades = true),
switchMap(termo => this.cidadeService.getCidadeAutocomplete(0, 50, termo)
.pipe(
finalize(() => this.carregandoCidades = false)
)
)
)
.subscribe(cidades => this.listCidade = cidades.content);
}
<div fxLayout="row wrap" fxLayout.lt-sm="column" fxFlex="1 0 auto">
<mat-form-field appearance="outline" fxFlex="50" class="pr-8">
<mat-label>{{ 'pessoa.label.cidade' | translate }}</mat-label>
<input matInput formControlName="genCidadeId" [matAutocomplete]="autocompleteCidade">
<mat-icon matSuffix class="secondary-text">search</mat-icon>
<mat-autocomplete #autocompleteCidade="matAutocomplete" (optionSelected)="selecionouCidade($event)" [displayWith]="displayFn">
<mat-option *ngIf="carregandoCidades" class="is-loading">
<mat-spinner diameter="30"></mat-spinner></mat-option>
<ng-container *ngIf="!carregandoCidades">
<mat-option *ngFor="let cidade of listCidade" [value]="cidade" [attr.data-genEstadoId]="cidade.genEstadoId">
{{ cidade.nmCidade }} - {{ cidade.dsSiglaCidade }}
</mat-option>
</ng-container>
</mat-autocomplete>
</mat-form-field>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment