如何根据条件从mat-select中禁用Option

问题描述 投票:0回答:1

我选择了两个mat-select,在第一个中我选择了Customer Individual或Organizational Customer的类型。如果用户选择了Ind Customer,我会显示另一个mat-select。但是,问题在于第二个mat-select我想要禁用某些输入字段的下拉选项。我怎样才能做到这一点?

HTML代码选择客户类型

   <mat-form-field>
   <mat-label>Select Customer Type</mat-label>
   <mat-select (onSelectionChange)="getCustType($event)">
   <mat-option *ngFor="let obj of custType" (click)="getCustType(obj)" 
   [value]="obj" > {{ obj.viewValue }}</mat-option>
   </mat-select>
   </mat-form-field>

打字稿代码:

custType: any[] = [{ value: 'indCust', viewValue: 'IndividualCustomer' }, { value: 'orgCust', viewValue: 'Organizational Customer' }];

第二个下拉HTML代码:

<mat-form-field class="col-sm-3">
    <mat-label>Select Option to Edit</mat-label>
    <mat-select (onSelectionChange)="getoptiontoedit($event)" >
      <mat-option *ngFor="let obj of optiontoeditlist" (click)="getoptiontoedit(obj)" [value]="obj"> {{ obj.viewValue }}</mat-option>
    </mat-select>
  </mat-form-field>

第二次下拉的打字稿代码:

  optiontoeditlist: any[] = [
{ value: 'address', viewValue: 'Address' },
{ value: 'agentRelationship', viewValue: 'Agent Relationship' },
{ value: 'agreementRelationship', viewValue: 'Agreement Relationship' },
{ value: 'organizationCustomer', viewValue: 'Organization Customer' },
{ value: 'complaint', viewValue: 'Complaint' },
{ value: 'contact', viewValue: 'Contact' },
{ value: 'identification', viewValue: 'Identification' },
{ value: 'individualCustomer', viewValue: 'Individual Customer'}
];

如果用户在第一个下拉列表中选择“组织客户”,我想要从第二个下拉列表中禁用/隐藏individualCustomer选项,同样如果用户在第一个下拉列表中选择“个人客户”,我希望从第二个下拉列表中禁用/隐藏OrganazationalCustomer。

html typescript angular7
1个回答
0
投票

您可以为您的第二个选项列表实现管道,如下所示

@Pipe({ name: 'optfilter' })
export class OptionsFilterPipe implements PipeTransform {
   transform(alloptions: any[],firstSelectOpt: any<>) {
     return alloptions.filter(option => option.value.indexOf(firstSelectOpt.value)==-1); // or whatever your comparator condition is this just for indication
   }
 }

那你可以像使用它一样

<mat-option *ngFor="let obj of optiontoeditlist | optFilter: 'selectedFirstOption'"
© www.soinside.com 2019 - 2024. All rights reserved.