typescriptangular-materialangular6mat-autocompletemd-chip

How to save selected object using mat-chip and autocomplete in angular material 2


I am using Angular 6 with Angular Material. I am trying to save a selected object or list of selected object from mat-chip and autocomplete. I am able to send string value to fruits[] array but can not able to send selected object in to fruits[] array. Please help me to find a solution. Thanks.

My Demo Project Link: demo code on stackblitz


Solution

  • You can try this solution.

    I have created a demo on Stackblitz .

    component.html

    <mat-form-field class="example-chip-list">
        <mat-chip-list #chipList>
            <mat-chip *ngFor="let fruit of fruits;let indx=index;" [selectable]="selectable" [removable]="removable" (removed)="remove(fruit,indx)">
                {{fruit.name}}
                <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>
    

    component.ts

    import { COMMA, ENTER } from '@angular/cdk/keycodes';
    import { Component, ElementRef, ViewChild } from '@angular/core';
    import { FormControl } from '@angular/forms';
    import { MatAutocompleteSelectedEvent, MatChipInputEvent } from '@angular/material';
    import { Observable } from 'rxjs';
    import { map, startWith } from 'rxjs/operators';
    
    /**
     * @title Basic chips
     */
    @Component({
      selector: 'chips-overview-example',
      templateUrl: 'chips-overview-example.html',
      styleUrls: ['chips-overview-example.css'],
    })
    export class ChipsOverviewExample {
      visible = true;
      selectable = true;
      removable = true;
      addOnBlur = false;
      separatorKeysCodes: number[] = [ENTER, COMMA];
      fruitCtrl = new FormControl();
      filteredFruits: Observable<string[]>;
      fruits: any = [];
      allFruits: any = [
        {
          id: 1,
          name: 'Apple'
        },
        {
          id: 2,
          name: 'Orange'
        },
        {
          id: 3,
          name: 'Banana'
        },
        {
          id: 4,
          name: 'Malta'
        }
      ];
    
      @ViewChild('fruitInput') fruitInput: ElementRef;
    
      constructor() {
        this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
          startWith(null),
          map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
      }
    
      add(event: MatChipInputEvent): void {
        const input = event.input;
        const value = event.value;
        // Add our fruit
        if ((value || '').trim()) {
          this.fruits.push({
              id:Math.random(),
              name:value.trim()
          });
        }
    
        // Reset the input value
        if (input) {
          input.value = '';
        }
    
        this.fruitCtrl.setValue(null);
      }
    
      remove(fruit, indx): void {
        this.fruits.splice(indx, 1);
      }
    
      selected(event: MatAutocompleteSelectedEvent): void {
        this.fruits.push(event.option.value);
        this.fruitInput.nativeElement.value = '';
        this.fruitCtrl.setValue(null);
      }
    
      private _filter(value: any): string[] {
        return this.allFruits.filter(fruit => fruit.id === value.id);
      }
    }