3
votes

I'm actually trying to insert a radio button group within a table component of material design and angular 6.

So in the examples of Material design official website there is a table with checkboxes but what I want is to do the same with radio button for each element of the table.

I tried to do it with a *ngFor directive but i can't access the ELEMENT_DATA array even if it's a global const.

import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';
import {SelectionModel} from '@angular/cdk/collections';

@Component({
  selector: 'table-selection-example',
  styleUrls: ['table-selection-example.css'],
  templateUrl: 'table-selection-example.html',
})
export class TableSelectionExample {
  displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.dataSource.data.forEach(row => this.selection.select(row));
  }
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
    <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Checkbox Column -->
  <ng-container matColumnDef="select">
    <th mat-header-cell *matHeaderCellDef>
      <mat-checkbox (change)="$event ? masterToggle() : null"
                    [checked]="selection.hasValue() && isAllSelected()"
                    [indeterminate]="selection.hasValue() && !isAllSelected()">
      </mat-checkbox>
    </th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()"
                    (change)="$event ? selection.toggle(row) : null"
                    [checked]="selection.isSelected(row)">
      </mat-checkbox>
    </td>
  </ng-container>

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"
      (click)="selection.toggle(row)">
  </tr>
</table>

So with this code i want to remove the checkboxes and replace it with radio buttons.

what i tried is to put :

<mat-radio-group>
   <mat-radio-button *ngIf="let i of ELEMENT_DATA" value = i></mat-radio-button>
</mat-radio-button>

and like I said before I can't access my Array ELEMENT_DATA. I also tried to change it with dataSource.length, but nothing do.

thanks for helping.

7

7 Answers

6
votes

you can add the mat-radio-group to mat-cell and use ngModel:

<ng-container matColumnDef="radio">
  <mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell>
  <mat-cell *matCellDef="let row">
    <mat-radio-group [(ngModel)]="selectedPerson">
      <mat-radio-button [value]="row"></mat-radio-button>
    </mat-radio-group>
  </mat-cell>
</ng-container>
2
votes

This is probably too late, but thought I would chime in. I just wanted to implement this same behavior, and I found that using the SelectionModel from the CKD basically allowed me to visually replicate this behavior, even if not in a better way.

https://material.angular.io/components/table/overview#selection

It supports multi or single selection modes and you can easily put the click logic on the mat-row, along with any kind of visual styling (or even a radio button) to inform the user of the state of the current selection.

1
votes

you can just add inside of the td of your table and it would work. but I got no idea how to know which radio button is selected.

 <table mat-table [dataSource]="dataSource" matSortDisableClear>

                                <ng-container matColumnDef="select">
                  <th mat-header-cell *matHeaderCellDef>
                    <mat-radio-button disabled>
                    </mat-radio-button>
                  </th>
                  <td mat-cell *matCellDef="let row">
                    <mat-radio-button (click)="$event.stopPropagation()"
                                      (change)="$event ? selection3.toggle(row) : null"
                                      [checked]="selection3.isSelected(row)"
                                      ></mat-radio-button>
                  </td>
                </ng-container>
                <tr mat-header-row *matHeaderRowDef="datadisplayedColumns"></tr>

                <tr mat-row *matRowDef="let row; columns: datadisplayedColumns;"></tr>
 </table>

typescript

  selection3 = new SelectionModel<Instruments>(false, []);

if you work in the row section you can always use let row as I did is also handy when you want to put a routerLink on your entire row

1
votes

Every thing you've done is correct. But, why are you trying to do *ngIf = "let i of Element_Data":

    <mat-radio-group>
        <mat-radio-button *ngIf="let i of ELEMENT_DATA" value = i></mat-radio-button>
    </mat-radio-button>.

Are you trying to do *ngFor? You can't do ' let i of Element_Data ' in *ngIf

0
votes

ELEMENT_DATA needs to be part of the class.

Try this:

import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';
import {SelectionModel} from '@angular/cdk/collections';

@Component({
  selector: 'table-selection-example',
  styleUrls: ['table-selection-example.css'],
  templateUrl: 'table-selection-example.html',
})
export class TableSelectionExample {
  displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.dataSource.data.forEach(row => this.selection.select(row));
  }

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}
0
votes

ELEMENT_DATA should be a property of the core component not a constant. Otherwise you cannot access it in html part.

0
votes

Try replicating the code below, worked for me

                <th mat-header-cell *matHeaderCellDef> No. </th>
                <td mat-cell *matCellDef="let row">    
                        <mat-radio-button [value]="row" (click)="radiobuttonClick(row)" color="primary"></mat-radio-button>
                </td>
            </mat-radio-group>