3

I have a material Table with expandable rows (https://material.angular.io/components/table/examples) and I want to manage delete into the table itself, so I create a delete button with a function that trigger the delete event. The api works, the record is correctly deleted, but the table is not refreshed after delete, and I want to implement this behaviour. Here a stackblitz with fake api: https://stackblitz.com/edit/angular-comzph

I try, in my delete function to call ngOnInt and to navigate back to the same route, but nothing happens...

deleteCustomer(id) {
 this.api.deleteCustomer(id)
  .subscribe(res => {
    alert(`cliente rimosso`);
    // TODO fix reload list after delete
    // this.router.navigate['/clienti'];
    this.ngOnInit();
  }, (err) => {
    console.log(err);
  }
 );
}

I try to use this solution too, but does not works. I try using ngOnChange and ngOnDestroy, but does not works too...

Could someone help me?

ufollettu
  • 822
  • 3
  • 19
  • 45
  • Instead of completely refreshing or re-initializing the component, you could refresh the table dataset by calling your api to fetch the data again. – Gijs Post Jul 11 '18 at 08:10
  • I try to call `this.api.getCustomers()` inside the `deleteCustomer()` method too, but is not working – ufollettu Jul 11 '18 at 08:18
  • Maybe using pipe? `this.api.deleteCustomer(id).pipe(flatMap(_ => this.api.getCustomers)).subscribe(result => { // Assign to data })` – Gijs Post Jul 11 '18 at 08:25
  • no, do not works. maybe there's a problem with `datasource` property passed to mat-table html – ufollettu Jul 11 '18 at 09:00
  • I solve the problem using this solution: https://stackoverflow.com/questions/46746598/angular-material-how-to-refresh-a-data-source-mat-table, thanks for your help – ufollettu Jul 11 '18 at 09:11

3 Answers3

5

There is no need to call the server for the updated data and download all the data again. Just call this function (below) and delete the row (splice) in the dataSource. Pass in the record id from your delete function and whatever the column name is where your id's are and magic happens. I include the paginator.

My StackBlitz for Angular Material Table has a working example. Also see my UpdateDatatableService which I use for CREATE AND UPDATE.

// Remove the deleted row from the data table. 
// Need to remove from the downloaded data first.

  private deleteRowDataTable (recordId, idColumn, paginator, dataSource) {
    this.dsData = dataSource.data;
    const itemIndex = this.dsData.findIndex(obj => obj[idColumn] === recordId);
    dataSource.data.splice(itemIndex, 1);
    dataSource.paginator = paginator;
  }
Preston
  • 3,260
  • 4
  • 37
  • 51
  • The splice call returns the data without that row, if splicing the MatTableDataSource directly use - this.myDataSource = this.myDataSource.data.splice(index, 1); – Airo Jun 03 '22 at 18:18
3

The Angular documentation shows there is a method, renderRows() which is available on the <table mat-table> elements. I was having the same problems, implemented this method, and it works great. I also learnt that you can optionally provide a data stream instead of simple data. Below solves the rendering of rows with simple data.

In your HTML template, give the <table mat-table> element a property binding:

<table mat-table [dataSource]="data" #myTable> <!-- #table binding here -->
  <!-- table cells content etc... -->
</table>

In your component class, link to the template property with @ViewChild, and call the renderRows() method when the table data changes.

import { MatTable } from '@angular/material/table';

export class MyComponent {
    @ViewChild('myTable') myTable: MatTable<any>; /*not exatly sure what the type should be set too */

    editDataMethod(){
        /* data handling logic */
        this.myTable.renderRows();

}
MR-DS
  • 92
  • 3
0

I solved creating a new datasource for mat-table every time the customer list is refreshed. I need to provide ChangeDetectorRef to my constructor, then to write this code:

refreshCustomersList() {
 this.api.getCustomers()
  .subscribe(res => {
    console.log(res);
    this.clienti = res;
    this.dataSource = new ClientiDataSource(this.api);
    this.changeDetectorRefs.detectChanges();
  }, err => {
    console.log(err);
 });
}

And it works!

NB: This is a problem only if you are using mat-table datasource

ufollettu
  • 822
  • 3
  • 19
  • 45