Angular 5 Angular Material 2 - 使用 minLength 自动完成

问题描述 投票:0回答:2

我在表单上添加了自动完成字段以选择患者。 患者数据来自数据库。问题是有 40,000 名患者

所以我想在用户输入至少 3 个字符后加载数据。 但我不知道如何检查以及如何将输入传递给函数(过滤器参数)。 这就是我所做的。目前,当我单击输入字段时,数据已加载:

HTML:

<mat-form-field class="index-full-width">
                    <input
                      matInput
                      type="text"
                      [(ngModel)]="patientChoice"
                      placeholder="Patient"
                      aria-label="Patient"
                      [matAutocomplete]="autoPatient"
                      [formControl]="myControl"
                      (click)="getPatients()">
                    <mat-autocomplete (optionSelected)="selectPat()" #autoPatient="matAutocomplete" [displayWith]="displayFnPat">

                      <mat-option *ngFor="let patient of filteredPatients | async" [value]="patient">
                        <span>{{ patient.lastName }}</span>
                        <small>{{patient.firstName}}</small> |
                        <span>né(e) le {{ patient.dateNaissance }}</span> |
                        <small>IPP: {{patient.ipp}}</small>
                      </mat-option>
                    </mat-autocomplete>
                  </mat-form-field>

TS:

 getPatients() {
let searchTerm = '*';
let success: any = {};
this.klinckServices.getPatients(searchTerm)
  .then((webScriptdata) => {
      success = webScriptdata;
      this.listPatients = success.data.items ;
      console.log(this.listPatients);
    },
    msg => {
      alert(msg);
    });

}

 ngOnInit() {
this.filteredPatients = this.myControl.valueChanges.pipe(
  startWith<string | Patient>(''),
  map(patient => typeof patient === 'string' ? patient : patient.name),
  map(name => name ? this.filterPatient(name) : this.listPatients.slice())
);

 }
  displayFnPat(patient: Patient): string | undefined {
return patient ? patient.name : undefined;
 }


 filterPatient(name: string) {
   return this.listPatients.filter(patient =>
    patient.name.toLowerCase().includes(name.toLowerCase()));
 }
autocomplete angular5 angular-material2 loaddata
2个回答
2
投票

还有另一种方法,推荐这里

它基本上是检查你的

filter
方法的长度。

  filterPatient(name: string) {
    if (name.length < 2) {
      return [];
    }

    return this.listPatients.filter(patient =>
      patient.name.toLowerCase().includes(name.toLowerCase()));
  }

1
投票

好的,通过添加 HTML (keyup)="getPatients($event)" 来解决:

<input
    matInput
    type="text"
    [(ngModel)]="patientChoice"
    placeholder="Patient"
    aria-label="Patient"
    [matAutocomplete]="autoPatient"
    [formControl]="myControl"
    (keyup)="getPatients($event)"
 >

在 TS 文件中:

    getPatients(event: any) {
    let searchTerm = '';
    searchTerm += event.target.value;
    console.log(searchTerm);
    if (searchTerm.length === 2) {
      let success: any = {};
      this.klinckServices.getPatients(searchTerm)
        .then((webScriptdata) => {
            success = webScriptdata;
            this.listPatients = success.data.items;
            console.log(this.listPatients);
          },
          msg => {
            alert(msg);
          });
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.