How to delete particular row from angular material table which doesnt have filters?

JustCode picture JustCode · Feb 18, 2019 · Viewed 21.7k times · Source

my samplepage.component.html

<table mat-table [dataSource]="myDataArray" >
      <!-- <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns" >
                      <th mat-header-cell *matHeaderCellDef > {{column}} </th>
                      <td mat-cell *matCellDef="let element" > {{element[column]}} </td>
                    </ng-container> -->


      <ng-container matColumnDef="id">
        <th mat-header-cell *matHeaderCellDef> Id. </th>
        <td mat-cell *matCellDef="let element; let i = index;"> {{i+1}} </td>
      </ng-container>


      <ng-container matColumnDef="ticketnumb">
        <th mat-header-cell *matHeaderCellDef> Ticket Number </th>
        <td mat-cell *matCellDef="let element"> {{element.ticketnumb}} </td>
      </ng-container>


      <ng-container matColumnDef="actionsColumn">
          <th mat-header-cell *matHeaderCellDef> Action </th>
          <td mat-cell *matCellDef="let element;  let j = index;"> 
              <button mat-raised-button  color="warn" focusable="false" (click)="deleteTicket(j)">
                  <i class="fa fa-times mat-icon"></i> Remove
                </button>
          </td>
        </ng-container>





      <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

my samplepage.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  selector: 'app-batchticketvalidation',
  templateUrl: './batchticketvalidation.component.html',
  styleUrls: ['./batchticketvalidation.component.css']
})
export class BatchticketvalidationComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }
  displayedColumns = ['id', 'ticketnumb', 'actionsColumn'];
  dataSource: PeriodicElement[] = ELEMENT_DATA;
  private myDataArray: any;
  private tempTicketCount: number = 0;



  deleteTicket(rowid: number){

   if (rowid > -1) {
    this.myDataArray.splice(rowid, 1);
 }

  }

}


export interface PeriodicElement {
  id: number;
  ticketnumb: string;
  actionsColumn: any;
}


const ELEMENT_DATA: PeriodicElement[] = [
];

I am new to angular. I need help in removing perticular row from the table and once the row is removed the table should get refresh or it should show the existing data. I have only static rows. This is just a plain mockup html that i want to show to the client.

Each row has delete button and on click of delete button I am calling deleteTicket(rowid).

When deleteTicket method is triggered, the row is not removed from Ui, but when i console this.myDataArray, the data is removed from object.

I tried all possibilities.

Answer

Leandro Feitosa picture Leandro Feitosa · Apr 8, 2019

after splice() by index of item, you should update the datasource.

const index = this.dataSource.data.indexOf(item.id);
this.dataSource.data.splice(index, 1);
this.dataSource._updateChangeSubscription(); // <-- Refresh the datasource