5

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?

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

3 Answers 3

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;
  }
Sign up to request clarification or add additional context in comments.

1 Comment

The splice call returns the data without that row, if splicing the MatTableDataSource directly use - this.myDataSource = this.myDataSource.data.splice(index, 1);
4

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();

}

Comments

1

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

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.