Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My samplepage.component.ts

import { Component } from '@angular/core';

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

  constructor() { }

  displayedColumns = ['id', 'ticketnumb', 'actionsColumn'];
  dataSource: PeriodicElement[] = ELEMENT_DATA;
  private myDataArray: any;

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

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

const ELEMENT_DATA: PeriodicElement[] = [
];

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>

I am new to angular. I need help in removing particular 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.

like image 637
JustCode Avatar asked Feb 18 '19 10:02

JustCode


People also ask

How do you delete a row in a table in angular?

Using the ng-repeat directive, we bind data to our HTML table. Now on the delete button, we added directive ng-click, and with deleteRow() function, the selected row gets deleted.

What is MatTableDataSource?

MatTableDataSource. Data source that accepts a client-side data array and includes native support of filtering, sorting (using MatSort), and pagination (using MatPaginator). Allows for sort customization by overriding sortingDataAccessor, which defines how data properties are accessed.


1 Answers

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
like image 79
Leandro Feitosa Avatar answered Oct 02 '22 15:10

Leandro Feitosa