Angular mat chips with auto complete not working.

huangapple go评论89阅读模式
英文:

Angular mat chips with auto complete not working

问题

我想在我的Angular项目中实现matAutoComplete功能。
我查看了一些参考资料 https://stackblitz.com/edit/mat-chips-angularmaterial?file=package.json

但是我没有找到任何关于autoComplete的选项。

//component.html




{{fruit}}
cancel





{{fruit}}


{{fruits|json}}

//component.ts

export class ChipsOverviewExample {
visible = true;
selectable = true;
removable = true;
addOnBlur = false;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = [];
allFruits: any= ['hi','hello','apple'];

@ViewChild('fruitInput') fruitInput: ElementRef;

constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
console.log(this.filteredFruits)
}

add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;

// Add our fruit
if ((value || '').trim()) {
  this.fruits.push(value.trim());
}

// Reset the input value
if (input) {
  input.value = '';
}

this.fruitCtrl.setValue(null);

}

remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);

if (index >= 0) {
  this.fruits.splice(index, 1);
}

}

selected(event: MatAutocompleteSelectedEvent): void {
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}

private _filter(value: string): string[] {
const filterValue = value.toLowerCase();

return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);

}
}

即使我已经在app.module.ts中导入了MatAutocompleteModule包。

英文:

i want to implement the matAutoComplete in my angular project
i gone through some references
https://stackblitz.com/edit/mat-chips-angularmaterial?file=package.json

but i didn't get any options for autoComplete.

//component.html

&lt;mat-form-field class=&quot;example-chip-list&quot;&gt;
  &lt;mat-chip-list #chipList&gt;
    &lt;mat-chip
      *ngFor=&quot;let fruit of fruits&quot;
      [selectable]=&quot;selectable&quot;
      [removable]=&quot;removable&quot;
      (removed)=&quot;remove(fruit)&quot;&gt;
      {{fruit}}
      &lt;mat-icon matChipRemove *ngIf=&quot;removable&quot;&gt;cancel&lt;/mat-icon&gt;
    &lt;/mat-chip&gt;
    &lt;input
      placeholder=&quot;New fruit...&quot;
      #fruitInput
      [formControl]=&quot;fruitCtrl&quot;
      [matAutocomplete]=&quot;auto&quot;
      [matChipInputFor]=&quot;chipList&quot;
      [matChipInputSeparatorKeyCodes]=&quot;separatorKeysCodes&quot;
      [matChipInputAddOnBlur]=&quot;addOnBlur&quot;
      (matChipInputTokenEnd)=&quot;add($event)&quot;&gt;
  &lt;/mat-chip-list&gt;
  &lt;mat-autocomplete #auto=&quot;matAutocomplete&quot; (optionSelected)=&quot;selected($event)&quot;&gt;
    &lt;mat-option *ngFor=&quot;let fruit of filteredFruits | async&quot; [value]=&quot;fruit&quot;&gt;
      {{fruit}}
    &lt;/mat-option&gt;
  &lt;/mat-autocomplete&gt;
&lt;/mat-form-field&gt;


&lt;pre&gt;{{fruits|json}}&lt;/pre&gt;

//component.ts

export class ChipsOverviewExample {
visible = true;
  selectable = true;
  removable = true;
  addOnBlur = false;
  separatorKeysCodes: number[] = [ENTER, COMMA];
  fruitCtrl = new FormControl();
  filteredFruits: Observable&lt;string[]&gt;;
  fruits: string[] = [];
  allFruits: any= [&#39;hi&#39;,&#39;hello&#39;,&#39;apple&#39;];

  @ViewChild(&#39;fruitInput&#39;) fruitInput: ElementRef;

  constructor() {
    this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
        startWith(null),
        map((fruit: string | null) =&gt; fruit ? this._filter(fruit) : this.allFruits.slice()));
        console.log(this.filteredFruits)
  }

  add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;

    // Add our fruit
    if ((value || &#39;&#39;).trim()) {
      this.fruits.push(value.trim());
    }

    // Reset the input value
    if (input) {
      input.value = &#39;&#39;;
    }

    this.fruitCtrl.setValue(null);
  }

  remove(fruit: string): void {
    const index = this.fruits.indexOf(fruit);

    if (index &gt;= 0) {
      this.fruits.splice(index, 1);
    }
  }

  selected(event: MatAutocompleteSelectedEvent): void {
    this.fruits.push(event.option.viewValue);
    this.fruitInput.nativeElement.value = &#39;&#39;;
    this.fruitCtrl.setValue(null);
  }

  private _filter(value: string): string[] {        
    const filterValue = value.toLowerCase();

    return this.allFruits.filter(fruit =&gt; fruit.toLowerCase().indexOf(filterValue) === 0);
  }
}

even i already imported the MatAutocompleteModule package in app.module.ts

答案1

得分: 2

请尝试以下方式:

.ts

constructor() {
  this.fruitCtrl.valueChanges.subscribe(search => {
    this.filteredFruits = of(this.allFruits.filter(item =>
      item.name.toLowerCase().includes(search)
    ));
  });
}

.html

<mat-form-field class="example-chip-list">
  <mat-chip-list #chipList>
    <mat-chip
      *ngFor="let fruit of fruits"
      [selectable]="selectable"
      [removable]="removable"
      (removed)="remove(fruit)">
      {{fruit}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input
      placeholder="New fruit..."
      #fruitInput
      [formControl]="fruitCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      [matChipInputAddOnBlur]="addOnBlur"
      (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
      {{fruit.name}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

<pre>{{fruits|json}}</pre>

演示链接

英文:

Try like this:

.ts

 constructor() {
    this.fruitCtrl.valueChanges.subscribe(search =&gt; {
      this.filteredFruits = of(this.allFruits.filter(item =&gt;
        item.name.toLowerCase().includes(search)
      ));
    });
  }

.html

&lt;mat-form-field class=&quot;example-chip-list&quot;&gt;
  &lt;mat-chip-list #chipList&gt;
    &lt;mat-chip
      *ngFor=&quot;let fruit of fruits&quot;
      [selectable]=&quot;selectable&quot;
      [removable]=&quot;removable&quot;
      (removed)=&quot;remove(fruit)&quot;&gt;
      {{fruit}}
      &lt;mat-icon matChipRemove *ngIf=&quot;removable&quot;&gt;cancel&lt;/mat-icon&gt;
    &lt;/mat-chip&gt;
    &lt;input
      placeholder=&quot;New fruit...&quot;
      #fruitInput
      [formControl]=&quot;fruitCtrl&quot;
      [matAutocomplete]=&quot;auto&quot;
      [matChipInputFor]=&quot;chipList&quot;
      [matChipInputSeparatorKeyCodes]=&quot;separatorKeysCodes&quot;
      [matChipInputAddOnBlur]=&quot;addOnBlur&quot;
      (matChipInputTokenEnd)=&quot;add($event)&quot;&gt;
  &lt;/mat-chip-list&gt;
  &lt;mat-autocomplete #auto=&quot;matAutocomplete&quot; (optionSelected)=&quot;selected($event)&quot;&gt;
    &lt;mat-option *ngFor=&quot;let fruit of filteredFruits | async&quot; [value]=&quot;fruit&quot;&gt;
      {{fruit.name}}
    &lt;/mat-option&gt;
  &lt;/mat-autocomplete&gt;
&lt;/mat-form-field&gt;


&lt;pre&gt;{{fruits|json}}&lt;/pre&gt;

Working Demo

答案2

得分: 1

HTML更改

<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
  {{fruit.name}}
</mat-option>

TYPESCRIPT更改

private _filter(value: string): string[] {
  const filterValue = value['name'] ? value['name'].toLowerCase() : value.toLowerCase();

  return this.allFruits.filter((fruit) => new RegExp(value, 'gi').test(fruit['name']));
}
英文:

[Answer updated to fix second search list]

HTMl CHANGES

&lt;mat-option *ngFor=&quot;let fruit of filteredFruits | async&quot; [value]=&quot;fruit&quot;&gt;
  {{fruit.name}}
&lt;/mat-option&gt;

TYPESCRIPT CHANGES

private _filter(value: string): string[] {
  const filterValue = value[&#39;name&#39;] ?  value[&#39;name&#39;].toLowerCase() : value.toLowerCase();

  return this.allFruits.filter((fruit) =&gt; new RegExp(value, &#39;gi&#39;).test(fruit[&#39;name&#39;]));
}

huangapple
  • 本文由 发表于 2020年1月3日 19:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定