Home > Back-end >  Mat Select multiple set selected value
Mat Select multiple set selected value

Time:11-12

I want to set some items in the list to be selected when I open the list.

my page:

from this

target page:

enter image description here

html

          <mat-form-field
            appearance="fill"
            color="accent"
            class="w-100 ps-lg-4 ps-0"
          >
            <mat-label>Applicativo Di Vendita</mat-label>
            <mat-select
              [formControl]="applicativoDiVenditaControl"
              multiple
              required
              (closed)="selectOnClose()"
            >
              <mat-select-trigger>
                {{
                  applicativoDiVenditaControl.value
                    ? applicativoDiVenditaControl.value[0]
                    : ""
                }}
                <span
                  *ngIf="applicativoDiVenditaControl.value?.length > 1"
                  class="additional-selection"
                >
                  ( {{ applicativoDiVenditaControl.value.length - 1 }}
                  {{
                    applicativoDiVenditaControl.value?.length === 2
                      ? "altro"
                      : "altri"
                  }})
                </span>
              </mat-select-trigger>
              <mat-option *ngIf="applicativoDiVenditaList.length <= 0" disabled
                >Nessun Elemento</mat-option
              >
              <mat-option
                *ngFor="let applicativoDiVendita of applicativoDiVenditaList"
                [value]="applicativoDiVendita.value"
                >{{ applicativoDiVendita.value }}</mat-option
              >
            </mat-select>
            <mat-error *ngIf="applicativoDiVenditaControl.invalid">{{
              getRequiredErrorMessage()
            }}</mat-error>
          </mat-form-field>

TS

applicativoDiVenditaControl: FormControl;
applicativoDiVenditaList: IdValueObj[] = [];

constructor(
    @Inject(MAT_DIALOG_DATA) public product: productClass,
    public dialogRef: MatDialogRef<CatalogoProdottiComponent>,
    private productCharService: ProductCharService,
  ) {
this.applicativoDiVenditaControl = new FormControl('', Validators.required);
this.getSalesApplication();
    }

getSalesApplication() {
    this.productCharService.getChar('sales').subscribe(
      (data) => {
        this.applicativoDiVenditaList = data;
      },
      (error) => {
        console.log(error);
      }
    );
  }

i tried with [(ngModel)] amd with [compareWith]="comparer", but dont works

i tried this StackBlitz, but dont work too https://stackblitz.com/edit/angular6-mat-select-with-obj-default-value?file=app/select-multiple-example.html

angular version 12.2.3

CodePudding user response:

On TS

export class YourForm{
  selectedValue: string[]=['0','1']

  yourValues= [
    {value: '0', viewValue: 'Value 0'},
    {value: '1', viewValue: 'Value 1'},
    {value: '2', viewValue: 'Value 2'}
  ];
}

On HTML

<mat-form-field>
    <mat-select placeholder="Your options" [(ngModel)]="selectedValue" name="values" multiple>
      <mat-option *ngFor="let v of yourValues" [value]="v.value">
        {{v.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  • Related