0

When I delete element my MAT-TABLE is not refresh

.ts file

import { MatTableDataSource, MatTable } from '@angular/material/table';
@ViewChild(MatTable) table: MatTable<any>;

const index = this.dataSource.data.findIndex(d => Number(d.id) == Number(id)); 
this.dataSource.data.splice(index, 1);
this.table.renderRows();

.html file

<table mat-table [dataSource]="dataSource" class="lessons-table mat-elevation-z8" matSort table>
1
  • Prefer immutable methods like filter and use triple-equals: this.dataSource.data = this.dataSource.data.filter(item => +item.id !== +id);. Doing that way, you don't need to use findIndex. Commented Mar 27, 2020 at 22:36

1 Answer 1

1

For the mat-table data to update, you need to immutably assign the new array to your dataSource.

Try this.

this.dataSource.data = this.dataSource.data.splice(index, 1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.