Using mat-option AND mat-radio-button in a mat-select or mat-selection-list

Viewed 4393

I'm trying to create a "multi-level" mat-select and I'd like to use both checkboxes and radio buttons.

An example of how it could be used to set properties for a car (assuming a radio can only be Digital or FM):

  • [v] Stero
  • [v] Radio
  • ---(o) Digital
  • ---( ) FM
  • [ ] Child seats
  • [ ] Rear camera

The radio buttons should only appear if the "parent" option is checked, in this case Radio.

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

I've created a solution but as I select a radio button I'll get this exception:

"Value must be an array in multiple-selection mode at getMatSelectNonArrayValueError (select.es5.js:116) at MatSelect.push.."

I assume this is because the mat-select looks for changes within its structure, where the radio buttons are placed. How can I structure the mat components to get the desired behavior?

1 Answers

I think you may have over-complicated this a little. Instead of using an if-else construct, just hide or show the checkboxes when there are sub-properties. Here's a simplified version that should work:

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

And in the .ts file:

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