You can easily achieve this with the help of *matCellDef. Instead of assigning it to element, you can assign it to row. Please find the complete code in below stackblitz:
URL - https://stackblitz.com/edit/angular-emnxjb
In our case we will use this property with button ng-container and will pass the entire row. And with this we will have data of mat-slide-toggle as well. I am alerting those values for demo.
I am just solving issue of how to pass value from one column to another, I hope you will handle rest.
Please let me know if any questions.
UPDATED CODE FOR WORKAROUND
In this approach i am keeping a track of mat slide toggle which are active in array called slideTrueArray
. When user clicks a button, it send the row index and based on the fact if that index if present in array, I am consoling the output.
import {SelectionModel} from '@angular/cdk/collections';
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';
export interface Playlist {
id: number;
name: string;
}
const ELEMENT_DATA: Playlist[] = [
{id: 1, name: 'first'},
{id: 2, name: 'second'},
{id: 3, name: 'third'},
];
/**
* @title Table with selection
*/
@Component({
selector: 'table-selection-example',
styleUrls: ['table-selection-example.css'],
templateUrl: 'table-selection-example.html',
})
export class TableSelectionExample {
canShowWaitingList: any;
displayedColumns: string[] = [ 'show waiting list', 'play'];
dataSource = new MatTableDataSource<Playlist>(ELEMENT_DATA);
slideTrueArray: number[] = [];
constructor() {
}
play(i: any) {
const isExist = this.slideTrueArray.indexOf(i);
const slideValue = isExist !== -1 ? true : false;
alert(slideValue);
}
toggleCurrentSlide(index:number) {
debugger;
const isSlideExist = this.slideTrueArray.indexOf(index);
if(isSlideExist !== -1) {
this.slideTrueArray.splice(isSlideExist, 1);
} else {
this.slideTrueArray.push(index);
}
}
}
table-selection-example.html
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="show waiting list">
<mat-header-cell *matHeaderCellDef> Show Waiting List</mat-header-cell>
<mat-cell *matCellDef="let element; let i = index;">
<mat-slide-toggle (change)="toggleCurrentSlide(i)"></mat-slide-toggle>
</mat-cell>
</ng-container>
<ng-container matColumnDef="play">
<mat-header-cell *matHeaderCellDef> Play</mat-header-cell>
<mat-cell *matCellDef="let element; let i = index;">
<button (click)="play(i)" mat-icon-button>
<mat-icon class="mat-18">play_arrow</mat-icon>
</button>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns; sticky:true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>