【问题标题】:ngx-mat-select-search doesn't worksngx-mat-select-search 不起作用
【发布时间】:2019-04-21 20:39:27
【问题描述】:

我想以我使用的表单实现 ngx-mat-select-search,但不幸的是,它不起作用(我无法打开下拉列表 - 它没有显示任何值)。

我的html:

<div class="form">
  <mat-form-field>
    <mat-select placeholder="Kunde" name="customer" #customer="ngModel" [(ngModel)]="currentCustomer"
      (ngModelChange)="dofilterCustomer()">
      <ngx-mat-select-search></ngx-mat-select-search>
      <mat-option *ngFor="let customer of customers | async" [value]="customer">
        {{customer}}
      </mat-option>
    </mat-select>
  </mat-form-field>
</div>

我的 .ts 文件:

import { Component, Inject, OnInit, ViewChild } from "@angular/core";
import {
  MAT_DIALOG_DATA,
  MatDialogRef,
  MatDialogModule,
  MatSelect
} from "@angular/material";
import { TableService } from "../table.service";
import { FormControl, Validators } from "@angular/forms";
import { EntryDTO, Customer } from "../../models";
import { SecurityService } from "src/app/security/security.service";
import { MatAutocompleteModule } from "@angular/material/autocomplete";
import { Observable } from "rxjs";
import { map, startWith } from "rxjs/operators";

@Component({
  selector: "app-add-dialog",
  templateUrl: "./add-dialog.component.html",
  styleUrls: ["./add-dialog.component.css"]
})
export class AddDialogComponent implements OnInit {
  constructor(
    public dialogRef: MatDialogRef<AddDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: EntryDTO,
    public dataService: TableService
  ) {}

  customers!: Observable<string[]>;
  currentCustomer = "";
  forwarders!: Observable<string[]>;
  currentForwarder = "";
  bins!: Observable<string[]>;
  currentBin = "";

  @ViewChild("singleSelect") singleSelect!: MatSelect;

  ngOnInit() {}

  dofilterCustomer() {
    this.customers = this.dataService
      .getAllCustomers()
      .pipe(map(customers => this.filter(customers, this.currentCustomer)));
  }

  dofilterForwarder() {
    this.forwarders = this.dataService
      .getAllForwarders()
      .pipe(map(forwarders => this.filter(forwarders, this.currentForwarder)));
  }

  dofilterBins() {
    this.bins = this.dataService
      .getAllBins()
      .pipe(map(bins => this.filter(bins, this.currentBin)));
  }

  formControl = new FormControl("", [
    Validators.required
    // Validators.email,
  ]);

  getErrorMessage() {
    return this.formControl.hasError("required")
      ? "Required field"
      : this.formControl.hasError("email")
      ? "Not a valid email"
      : "";
  }

  submit() {
    this.dataService.addEntry(this.data);
  }

  onNoClick(): void {
    this.dialogRef.close();
  }

  public confirmAdd(): void {
    this.dataService.addEntry(this.data);
  }

  filter(values: string[], current: string) {
    return values.filter(value => value.toLowerCase().includes(current));
  }
}

最初,我的 html 中有以下代码。然后它起作用了,但我没有下拉菜单,只有一个“自动完成列表”:

<div class="form">
  <mat-form-field class="example-full-width accent">
    <input type="text" #input placeholder="Kunde" aria-label="Number" name="customer" #customer="ngModel" matInput
      [(ngModel)]="currentCustomer" (ngModelChange)="dofilterCustomer()" [matAutocomplete]="auto1" required>
    <mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
    <mat-autocomplete #auto1="matAutocomplete">
      <mat-option *ngFor="let customer of customers | async" [value]="customer">
        {{customer}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</div>

也许有人能看出我做错了什么?

提前谢谢你。

【问题讨论】:

    标签: angular typescript angular-material


    【解决方案1】:

    你需要初始化this.customers。我建议最初将 observable 设置为重播主题并最初加载客户,如下所示:

    customers: ReplaySubject<string[]> = new ReplaySubject<string[]>(1);
    
    ngOnInit() {
      this.dataService
      .getAllCustomers()
      .subscribe(customers => this.customers.next(customers));
    }
    

    dofilterCustomer() {
      this.dataService
        .getAllCustomers()
        .subscribe(customers => this.customers.next(this.filter(customers, this.currentCustomer)));
    }
    

    另见示例实现https://stackblitz.com/github/bithost-gmbh/ngx-mat-select-search-examplehttps://github.com/bithost-gmbh/ngx-mat-select-search/issues/83

    【讨论】:

    • 我收到以下错误:“'Observable' 类型上不存在属性 'next'。”所以我改变了行“客户!:Observable = new ReplaySubject(1);”到以下内容:客户!:ReplaySubject = new ReplaySubject(1);现在它可以工作了!非常感谢
    • 此外,您应该将(ngModelChange)="dofilterCustomer()" 放在&lt;ngx-mat-select-search&gt; 元素上以在搜索关键字更改时触发过滤:` `
    • 感谢您的信息。 tbh 我没有检查过滤器选项。现在我的休息服务也不再起作用了;)。我明天去看看,然后更新这个帖子
    • 请忘记我的最后评论。现在一切正常!
    【解决方案2】:

    我更喜欢在我的代码中避免重播主题(或一般主题),因此请使用以下内容:

      this.options$ = this.searchControl.valueChanges.pipe(
           startWith(''),
           map(searchValue => this.getFilteredOptions(allOptions, searchValue))
    

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 2019-10-25
      • 2020-12-01
      • 2020-09-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2022-12-08
      相关资源
      最近更新 更多