html - Mat Select 多个设置选定值

标签 html angular typescript angular-material

我想设置列表中的某些项目在打开列表时被选中。

我的页面:

from this

目标页面:

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);
      }
    );
  }

我尝试使用 [(ngModel)] amd 和 [compareWith]="comparer",但不起作用

我尝试过这个 StackBlitz,但也不起作用 https://stackblitz.com/edit/angular6-mat-select-with-obj-default-value?file=app%2Fselect-multiple-example.html

Angular 版本12.2.3

最佳答案

关于 TS

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

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

在 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>

关于html - Mat Select 多个设置选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69930466/

相关文章:

reactjs - react + typescript : Defining EventHandler

typescript - Typescript 中的 Getter/Setter

html - 处理带有身份验证所需图像的 HTML PDF

javascript - localStorage 的 'storage' 事件中的未定义属性

javascript - 基于 if 语句动态启用/禁用按钮 Ionic

javascript - 无法在我的 angular-2 快速入门应用程序中加载 ng2-fullpage

twitter-bootstrap - ngClass 具有字符串插值来动态更改它

html - 如何将 <tr id> 转换为 <div id>?

javascript - 从 JavaScript 在 HTML 上打印文本

angular - 在数组 angular 4 中添加项目