【问题标题】:Using mat-option AND mat-radio-button in a mat-select or mat-selection-list在 mat-select 或 mat-selection-list 中使用 mat-option AND mat-radio-button
【发布时间】:2019-05-30 23:55:03
【问题描述】:

我正在尝试创建一个“多级”垫选择,我想同时使用复选框和单选按钮。

如何使用它来设置汽车属性的示例(假设收音机只能是数字或 FM):

  • [v] 立体
  • [v] 收音机
  • ---(o) 数字
  • ---( ) 调频
  • [ ] 儿童座椅
  • [ ] 后置摄像头

如果在这种情况下检查“父”选项,则只能出现单选按钮。

<mat-form-field>
  <mat-select [(value)]="viewValue" #multipleSelect (openedChange)="onMultipleChange($event, multipleSelect.selected)" multiple>
    <ng-container *ngFor="let property of carProperties">

      <!-- If the property does not have any subProperties, display the property. Else display the nested options (subProperties) -->
      <mat-option *ngIf="!property.subProperties; else nestedOption" [value]="property.value">
        {{property.value}}
      </mat-option>
      <ng-template #nestedOption>
        <mat-checkbox #parentOption>
          {{property.value}}
        </mat-checkbox>
        <ng-container *ngIf="parentOption.checked">
          <ng-template #radioOptions>
            <mat-radio-group (change)="radioChange($event)"> <!-- Not sure what the ngModel should be here -->
              <mat-radio-button *ngFor="let subProperty of property.subProperties" [value]="subProperty.value">
                {{subProperty.value}}
              </mat-radio-button>
            </mat-radio-group>
          </ng-template>
        </ng-container>
      </ng-template>
    </ng-container>
  </mat-select>
</mat-form-field>

我已经创建了一个解决方案,但是当我选择一个单选按钮时,我会得到这个异常:

"值必须是多选模式下的数组 在 getMatSelectNonArrayValueError (select.es5.js:116) 在 MatSelect.push..”

我认为这是因为 mat-select 在其结构中查找更改,即放置单选按钮的位置。如何构建 mat 组件以获得所需的行为?

【问题讨论】:

    标签: angular typescript angular-material


    【解决方案1】:

    我认为您可能有点过于复杂了。不要使用 if-else 构造,只需在有子属性时隐藏或显示复选框。这是一个应该可以工作的简化版本:

    <mat-form-field>
        <mat-select [(value)]="viewValue" #multipleSelect multiple>
            <ng-container *ngFor="let property of carProperties">
                <mat-option [value]="property.value">
                    {{ property.value }}
                </mat-option>
                <div *ngIf="property.subProperties && valueSelected(property.value)">
                    <mat-radio-group>
                        <mat-radio-button *ngFor="let subProperty of property.subProperties"
                            [value]="subProperty.value"
                            style="display: block; padding: 12px 12px 12px 32px;">
                            {{ subProperty.value }}
                        </mat-radio-button>
                    </mat-radio-group>
                </div>
            </ng-container>
        </mat-select>
    </mat-form-field>
    

    在 .ts 文件中:

    viewValue: string[] = [ ];
    carProperties = [
        { value: 'Stereo' },
        { value: 'Radio',
          subProperties: [
            { value: 'Digital' },
            { value: 'FM' }
          ]
        }, { value: 'Child seats' },
        { value: 'Rear camera' }
    ];
    
    valueSelected(value: string): boolean {
        return this.viewValue.indexOf(value) !== -1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-07
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 2020-12-01
      • 2023-03-21
      相关资源
      最近更新 更多