title: Removing selected rows from the table in angular 2 using angular material table
body: <p>I am trying to implement a simple table in angular 2 using angular material selection table .</p>
  
  <p>I used a button to remove the selected rows from the table.But on click of the button how can i delete the rows ?</p>
  
  <h2>Below shown is my html file for the table</h2>
  
  <pre><code>&lt;div class="example-container mat-elevation-z8"&gt;
    &lt;mat-table #table [dataSource]="dataSource"&gt;
  
      &lt;!-- Checkbox Column --&gt;
      &lt;ng-container matColumnDef="select"&gt;
        &lt;mat-header-cell  style="max-width: 100px;" *matHeaderCellDef&gt;
          &lt;mat-checkbox (change)="$event ? masterToggle() : null"
                        [checked]="selection.hasValue() &amp;&amp; isAllSelected()"
                        [indeterminate]="selection.hasValue() &amp;&amp; !isAllSelected()"&gt;
          &lt;/mat-checkbox&gt;
        &lt;/mat-header-cell&gt;
        &lt;mat-cell   style="max-width: 100px;" *matCellDef="let row"&gt;
          &lt;mat-checkbox (click)="$event.stopPropagation()"
                        (change)="$event ? selection.toggle(row) : null"
                        [checked]="selection.isSelected(row)"&gt;
          &lt;/mat-checkbox&gt;
        &lt;/mat-cell&gt;
      &lt;/ng-container&gt;
  
      &lt;!-- Number Column --&gt;
      &lt;ng-container matColumnDef="num"&gt;
        &lt;mat-header-cell style="max-width: 100px;" *matHeaderCellDef&gt; No. &lt;/mat-header-cell&gt;
        &lt;mat-cell style="max-width: 100px;" *matCellDef="let element"&gt; {{element.num}} &lt;/mat-cell&gt;
      &lt;/ng-container&gt;
  
      &lt;!-- Message Column --&gt;
      &lt;ng-container matColumnDef="name"&gt;
        &lt;mat-header-cell *matHeaderCellDef&gt; Message &lt;/mat-header-cell&gt;
        &lt;mat-cell *matCellDef="let element"&gt; {{element.name}} &lt;/mat-cell&gt;
      &lt;/ng-container&gt;
  
  
      &lt;mat-header-row *matHeaderRowDef="displayedColumns"&gt;&lt;/mat-header-row&gt;
      &lt;mat-row *matRowDef="let row; columns: displayedColumns;"
               (click)="selection.toggle(row)"&gt;
      &lt;/mat-row&gt;
    &lt;/mat-table&gt;
  
     &lt;mat-paginator #paginator
                   [pageSize]="10"
                   [pageSizeOptions]="[5, 10, 20]"&gt;
    &lt;/mat-paginator&gt;
  &lt;/div&gt;
  
  &lt;br/&gt;
  &lt;button style="background: #3B4990; color:white;" (click)="deleted($event)"&gt;Remove Selected Messages&lt;/button&gt;
  </code></pre>
  
  <h2>Below shown is my .ts file.</h2>
  
  <pre><code>import {Component, ViewChild} from '@angular/core';
  import {MatPaginator, MatTableDataSource} from '@angular/material';
  import {SelectionModel} from '@angular/cdk/collections';
  
  
  @Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
     styleUrls: ['./home.component.scss']
  })
  
  
  export class HomeComponent {
  
  displayedColumns = ['select', 'num', 'name'];
    dataSource = new MatTableDataSource&lt;Element&gt;(ELEMENT_DATA);
  
  
  
    @ViewChild(MatPaginator) paginator: MatPaginator;
  
    /**
     * Set the paginator after the view init since this component will
     * be able to query its view for the initialized paginator.
     */
    ngAfterViewInit() {
      this.dataSource.paginator = this.paginator;
    }
  
    selection = new SelectionModel&lt;Element&gt;(true, []);
  
    /** Whether the number of selected elements matches the total number of rows. */
    isAllSelected() {
      const numSelected = this.selection.selected.length;
      const numRows = this.dataSource.data.length;
      return numSelected === numRows;
    }
  
    /** Selects all rows if they are not all selected; otherwise clear selection. */
    masterToggle() {
      this.isAllSelected() ?
          this.selection.clear() :
          this.dataSource.data.forEach(row =&gt; this.selection.select(row));
    }
  
      deleted($event)
  { 
   delete(this.dataSource.data.forEach(row =&gt; this.selection.select(row));)
  }
  
  }
  
  export interface Element {
    name: string;
    num: number;
  }
  
  const ELEMENT_DATA: Element[] = [
    {num: 1, name: 'Message1'},
    {num: 2, name: 'Message2'},
    {num: 3, name: 'Message3'},
    {num: 3, name: 'Message4'},
    {num: 4, name: 'Message5'},
    {num: 5, name: 'Message6'},
  ];
  </code></pre>
  
  <p>can anybody please help me how can i remove the selected rows from the table using the button.</p>