Houssem
Houssem

Reputation: 69

how to manipulate mat-select multiple from outside of the select?

I have a mat select which contains a list of contrats and evrey time i click on the contrat i add it to a list in order to display it under the mat-select .

 <mat-form-field style="width: 100%;">
    <mat-select placeholder="Contrats" formControlName="contrat" multiple>
      <mat-option *ngFor="let contrat of contrats$ | async" [value]="contrat.code" (click)="addContrat(contrat.code,contrat.label)">
        {{ contrat.label }}
      </mat-option>
    </mat-select>
  </mat-form-field>

and this is the function which allow me to add contrat

public list: any[] = [];
addContrat(code: string, label: string) {
this.list.push({ code, label });
}
removeContract(i: number) {
 this.list.splice(i, 1);
}

and this is the tempalte :

<mat-chip-list [multiple]="true">
    <mat-chip style="width:100%" *ngFor="let x of list; let i=index" >
        {{x.code}} -- {{x.label}}
        <mat-icon matChipRemove aria-label="" (click)="removeContract(i)">clear</mat-icon>
    </mat-chip>

  </mat-chip-list>

so i want when i click on remove contrat the mat select will be updated

Upvotes: 0

Views: 2287

Answers (1)

AliWieckowicz
AliWieckowicz

Reputation: 557

I think you may need to decorate your chip with the "removeable" property like so

<mat-chip *ngFor="let fruit of fruits" [selectable]="selectable"
             [removable]="removable" (removed)="remove(fruit)">
      {{fruit.name}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>

https://stackblitz.com/angular/ebkrnrqbnne?file=app%2Fchips-input-example.html https://material.angular.io/components/chips/overview#chip-input

Else, I think you could move your click event onto the chip rather than the icon if a simple solution is acceptable.

 <mat-chip style="width:100%" *ngFor="let x of list; let i=index" (click)="removeContract(i)">

Upvotes: 1

Related Questions