Michael
Michael

Reputation: 297

How to pop selected row from material data table?

I'm using material data table for my Angular project. I use a dynamic table, where data gets added and deleted. How do I pop a given row from the data table? Of course pop doesn't work. I also used splice(row, 1), which also didn't work (maybe I used it wrong).

Type of 'row': Row is an object (Array consisting of 4 numbers).

Log of the row:

{sender: "7", recipient: "1", amount: "1", fee: "1"}

When clicking on the checkbox (each row has a checkbox) in the data table I want to delete the row. The checkbox determines, which row is getting deleted:

<mat-checkbox (click)="$event.stopPropagation(); putTXInBlock(row);"
                      (change)="$event ? selection.toggle(row) : null"
                      [checked]="selection.isSelected(row)">
</mat-checkbox>

Where the row should get deleted:

  putTXInBlock(row) {
    this.temp = this.dataSource.data.slice();
    this.temp.pop(); // pop input parameter row
    this.dataSource.data = this.temp;
    this.ref.detectChanges();
    this._TS.emitTransaction(row);
  }

Upvotes: 0

Views: 607

Answers (1)

Aragorn
Aragorn

Reputation: 5289

You can use Array.prototype.splice to remove an element from an array at an index.

for(var i = 0; i < this.temp.length; i++){
      if(JSON.stringify(row) === JSON.stringify(this.temp[i]) ){
        this.temp.splice(i,1);
        break;
      }
  }

To your question, you can't pop() because pop() can only remove the last element in the array.

Upvotes: 2

Related Questions