How to use angular material 6 autocomplete with da

2020-07-11 06:01发布

I'm developing a simple page with Angular 6 and Material 6. I want to recover data from a service using the autocomplete of Material, but I don´t know how to do it well.

From the official example https://material.angular.io/components/autocomplete/overview I don't understand how to use a service to integrate it with the autocomplete.

Can anybody help me?

Thanks

3条回答
我只想做你的唯一
2楼-- · 2020-07-11 06:29

well let's assume u returning ur data from the server as Objects with a structure like IyourAwesomeData and for the purpose of this example, we will use field someName to filter the data

so ur ts component should look sth like this:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormControl } from '@angular/forms';
import { startWith, debounceTime, map, switchMap, distinctUntilChanged } from 'rxjs/operators';
import { Subscription } from 'rxjs';

interface IyourAwesomeData {
 someName: string; 
 someField: string;
 someOtherField: number;
}

export class YourAutcompleteComponent implements OnInit, OnDestroy {

  dataFiltered: IyourAwesomeData[]; // this data will be used inside HTML Template
  data: Observable<IyourAwesomeData[]>; 
  yourInputCtrl = new FormControl();
  private sub: Subscription;

  constructor() {}

  ngOnInit() {
    this.data = ??? // Pass your data as Observable<IyourAwesomeData[]>;
    this.sub = this.yourInputCtrl.valueChanges
      .pipe(
        debounceTime(500),
        distinctUntilChanged(), 
        startWith(''),
        switchMap((val) => {
          return this.filterData(val || '');
        })
      ).subscribe((filtered) => {
        this.dataFiltered = filtered;
      });
  }

  ngOnDestroy() {
     this.sub.unsubscribe();
  }

  filterData(value: string) {
    return this.data // like IyourAwesomeData[]
      .pipe(
        map((response) => response.filter((singleData: IyourAwesomeData) => {
          return singleData.someName.toLowerCase().includes(value.toLowerCase())
        })),
    );
  }
}

and ur HTML template should look sth like this:

<mat-form-field>
  <input matInput placeholder="some placeholder" [matAutocomplete]="auto" [formControl]="yourInputCtrl">
  <mat-autocomplete #auto="matAutocomplete">
    <mat-option *ngFor="let single of dataFiltered" [value]="single.someName">
      {{ single.someName }}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-07-11 06:32

You just need to fill the options from server data as soon as the user changes the input value.

<input type="text" matInput (input)="onInputChanged($event.target.value)" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
  <mat-option *ngFor="let option of options" [value]="option">{{ option.title }}</mat-option>
</mat-autocomplete>

In your component file, you need to handle onInputChanged(searchStr), and options.

onInputChanged(searchStr: string): void {
    this.options = [];
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
    this.subscription = this.yourService.getFilteredData(searchStr).subscribe((result) => {
        this.options = result;
      });
    }
查看更多
smile是对你的礼貌
4楼-- · 2020-07-11 06:39

Finally, I found a solution to what I wanted to do! To bind a FormArray to mat-table dataSource you have to: Briefly, the example is this:

<table mat-table [dataSource]="itemsDataSource">
  <ng-container matColumnDef="itemName">
    <td mat-cell *matCellDef="let element">{{ element.value.material?.name }}</td>
  </ng-container>
  <ng-container matColumnDef="itemCount">
    <td mat-cell *matCellDef="let element">{{ element.value.itemCount }}</td>
  </ng-container>
  <tr mat-row *matRowDef="let row; columns: itemColumns;"></tr>
</table>

and the code:

export class ItemListComponent implements OnInit {
  constructor(
    private fb: FormBuilder
  ) { }
  itemColumns = ['itemName', 'count'];
  itemForm: FormGroup;
  itemsDataSource = new MatTableDataSource();
  get itemsForm() {
    return this.itemForm.get('items') as FormArray;
  }
  newItem() {
    const a = this.fb.group({
      material: new FormControl(), //{ name:string }
      itemCount: new FormControl() // number 
    });
    this.itemsForm.push(a);
    this.itemsDataSource._updateChangeSubscription(); //neccessary to render the mat-table with the new row
  }
  ngOnInit() {
    this.itemForm = this.fb.group({
      items: this.fb.array([])
    });
    this.newItem();
    this.itemsDataSource.data = this.itemsForm.controls;
  }
}
查看更多
登录 后发表回答