1

I am using angular material 7.

I am using radio-button-group.

I have a table design which i have to use. So that is fixed.

When i try to use radio-buttons in td, i am not sure hot to give same group.

below is my html.

<tr>
      <td>
        <mat-radio-group >
          <mat-radio-button value="maternal"></mat-radio-button>
        </mat-radio-group>
      </td>
      <td>
        <mat-radio-group >
          <mat-radio-button value="paternal"></mat-radio-button>
        </mat-radio-group>
      </td>
    </tr>

Here is stackblitz of my issue https://stackblitz.com/edit/angular-tt2kep-woea34.

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53

3 Answers3

0

Use below code

    <table style="width:100%; text-align:left;">
  <thead>
    <th>Meternal</th>
    <th>Paternal</th>
  </thead>
  <tbody>
    <mat-radio-group >
    <tr>

      <td>

          <mat-radio-button value="maternal">Meternal</mat-radio-button>

      </td>
      <td>
          <mat-radio-button value="paternal">Peternal</mat-radio-button>

      </td>

    </tr>
    </mat-radio-group>
  </tbody>
</table>
Er Abdul Meman
  • 163
  • 1
  • 9
  • We can't break table like this. Try it in my stackblitz, it will break design completely. And what if i have some other controls in other td? – Ankur Akvaliya Apr 05 '19 at 12:18
  • I know brother but you are using different radio group so I think it's not possible to give two radio group to use a same value. I'' try to check another solution if possible – Er Abdul Meman Apr 05 '19 at 12:32
0

We have this cool ngProjectAs to use on ng-container, which won't break your css. So we can project mat-radio-group for the container, which we wrap around tr:

<ng-container ngProjectAs="mat-radio-group">
  <tr>
    <td>
      <mat-radio-button value="maternal"></mat-radio-button>
    </td>
    <td>
      <mat-radio-button value="paternal"></mat-radio-button>
    </td>
  </tr>
</ng-container>

Your StackBlitz

AT82
  • 71,416
  • 24
  • 140
  • 167
0

It is not mandatory that you use mat-radio-group. Instead, you can use the name input for mat-radio-button. The challenge then becomes how to access the 'group' value. For that you can manually manage the radio changes.

This example assumes some kind of data structure for your rows:

<table style="width:100%; text-align:left;">
  <thead>
    <th>Maternal</th>
    <th>Paternal</th>        
    <th>Value</th>
  </thead>
  <tbody>
    <ng-container *ngFor="let row of rows">
      <tr>
        <td>
            <mat-radio-button value="maternal" [name]="row" (change)="radioChange($event)" checked="true"></mat-radio-button>
        </td>
        <td>
            <mat-radio-button value="paternal" [name]="row" (change)="radioChange($event)"></mat-radio-button>
        </td>
        <td>
            {{ values[row].value }}
        </td>
      </tr>
    </ng-container>
  </tbody>
</table>

export class RadioOverviewExample {
  selectedValue:string="meternal";
  selectedValue1:string="meternal1";
  rows = ['one', 'two', 'three'];
  values = {};

  ngOnInit() {
    this.rows.forEach(row => {
      this.values[row] = { value: 'maternal' };
    })
  }

  radioChange(event) {
    this.values[event.source.name].value = event.value;
  }
}

https://stackblitz.com/edit/angular-tt2kep-h6pjiy?file=app%2Fradio-overview-example.html

G. Tranter
  • 16,766
  • 1
  • 48
  • 68