如果名称值重复,如何告诉.find()找到正确的id?

时间:2017-06-07 12:37:25

标签: angular typescript ngx-bootstrap

在我的应用中,我输入了typeahead(自动完成)。到目前为止,它工作得很好,但我发现重复name值存在问题。我必须发送id name的{​​{1}},我的预先输出建议name值。如果我键入一个重复的name,无论我选择哪种类型的重复值,它都会在列表中发送id第一个对象。

如何告诉.find()搜索选择的值,而不是列表中的第一个?现在,如果我输入' wolny'进入输入字段并按Enter键,它将传递464 WOLNY的值。即使我点击了,例如,' WOLNY'如果值为619,则它仍然会通过464

我不能建议id在预先输入,因为:

  • 项目需要输入名称,而不是ids
  • 给我的API不接受姓名

我对ngx-bootstrap库使用typeahead。 这就是typeahead的样子(灰色数字是特定id的{​​{1}}个}:

enter image description here

服务 - 预先

name

组件

    searchVehicleId(description?: string) {
    const url = (!description) ? this.defUrl : 'API=' + description;
    return this.http.get(url)
      .map(res => res.json().vehicles);
  }

模板

export class VehicleComponent implements OnInit {

  ngOnInit() {

this.vehicleIdDataSource = Observable.create((observer: any) => {
  this.vehicleService.searchVehicleId(this.vehicleIdAsyncSelected)
    .subscribe((result: any) => {
      observer.next(result);
    })
});
    this.searchQuery = new FormGroup({
      vehiclename: new FormControl('', Validators.required)
    });
  }
  public vehicle: Vehicle[];
  public description: Description[];
  public searchVehicleId: any;

  public vehicleIdDataSource: Observable<any>;
  public vehicleIdAsyncSelected: string = "";

    public searchByVehicleId(searchQuery) {
    this.vehicleService
      .searchVehicleId(searchQuery.value.vehiclename)
      .subscribe(searchQuery => {
        this.description = searchQuery;
        this.searchVehicleId = this.description.find(x => x.id === x.id).id;
        let qwe = this.description.filter(x => x.id === x.id).map(x => x.id);

        console.log(JSON.stringify(this.description));
        console.log("filter(): " + qwe);
        console.log("find(): " + this.searchVehicleId);

        this.vehicleService
          .getVehicleByVehicleId(this.searchVehicleId)
          .subscribe(searchQuery => {
            this.vehicle = searchQuery;
          })
      });

    interface Description {
      vehicle_id: number;
      custom_description: string;
      description: string;
    }

interface Vehicle {
  status: number;
  dallases: Vehicle[];
}

interface VehicleDetails {
  vehicle_id: number;
  dallassettings: string;
  dallasupdated: string;
  dallas_list: DallasList[];
}

interface DallasList {
  number: number;
  auth: number;
}

由于

0 个答案:

没有答案