Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button within a table with Material Design

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.

like image 841
Kira Avatar asked Dec 17 '22 23:12

Kira


2 Answers

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>
like image 50
Jan Feyen Avatar answered Dec 28 '22 09:12

Jan Feyen


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.

like image 45
Steven Avatar answered Dec 28 '22 09:12

Steven