如何在AutoComplete Material2中使用[displayWith]显示 [英] How to display using [displayWith] in AutoComplete Material2

查看:174
本文介绍了如何在AutoComplete Material2中使用[displayWith]显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自我的API的array,并且我正在使用 Material2 #AutoComplete 进行过滤...到目前为止,它仍然可以正常工作,但是我很难显示另一个属性而不是 binded option 中的值.

I have an array that comes from my API and I'm using Material2#AutoComplete to filter this... it's working so far, however I'm in trouble to display the another property instead of the binded value in option.

我知道我必须使用 ,但是它没有按我期望的那样工作.称为[displayWith]="displayFn.bind(this)">的函数只是返回我id,如何获取完整的对象,然后返回该函数上的name.

I know I have to use displayWith, however it isn't working as I'm expecting. The function called as [displayWith]="displayFn.bind(this)"> just returns me the id, how can I get the full object and so return the name on function.

顺便说一句,我仍然想将ID绑定到我的 FormControl 中.

BTW, I still want to have the id binded in my FormControl.

某些代码:

组件:

Component:

export class AutocompleteOverviewExample {
  stateCtrl: FormControl;
  filteredStates: any;

  states = [
    { 
      id: 1,
      name: 'Alabama'
    },
    {
      id: 2,
      name: 'North Dakota'
    },
    {
      id: 3,
      name: 'Mississippi'
    }
  ];

  constructor() {
    this.stateCtrl = new FormControl();
    this.filteredStates = this.filterStates('');
  }

  onKeyUp(event: Event): void {
    this.filteredStates = this.filterStates(event.target.value);
  }

  filterStates(val: string): Observable<any> {
    let arr: any[];
    console.log(val)
    if (val) {
      arr = this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s.name));
    } else {
      arr = this.states;
    }

    // Simulates request
    return Observable.of(arr);
  }

  displayFn(value) {
    // I want to get the full object and display the name
    return value;
  }
}

模板:

Template:

<md-input-container>
  <input mdInput placeholder="State" (keyup)="onKeyUp($event)" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-input-container>

<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn.bind(this)">
  <md-option *ngFor="let state of filteredStates | async" [value]="state.id">
    {{ state.name }}
  </md-option>
</md-autocomplete>

基本上,它与 此问题 (不幸的是,两个答案都不正确或抛出错误).

Basically, it's almost the same as this question (unfortunately both answers are incorrect or throw errors).

这是 PLUNKER .

推荐答案

如果希望将整个对象与md-options绑定,则应使用state绑定到option并在displayFn返回state.name这样,您就不必绑定this.

If you want the entire object to be binded with md-options, then you should bind to option with state and return state.name at displayFn and this way you don't have to bind this.

<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn">
  <md-option *ngFor="let state of filteredStates | async" [value]="state">
    {{ state.name }}
  </md-option>
</md-autocomplete>

displayFn(state) {
  return state.name;
}

演示朋克 .

demo plunker.

,如果只想将state.id绑定到md-options,则必须遍历states才能基于state.id查找state.name,这样就需要绑定this.

and if you want to bind only state.id to md-options, you have to loop through states to find state.name based on state.id and this way binding this is needed.

<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayFn.bind(this)">
  <md-option *ngFor="let state of filteredStates | async" [value]="state.id">
    {{ state.name }}
  </md-option>
</md-autocomplete>

displayFn(id) {
  if (!id) return '';

  let index = this.states.findIndex(state => state.id === id);
  return this.states[index].name;
}

演示朋克 .

demo plunker.

这篇关于如何在AutoComplete Material2中使用[displayWith]显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆