Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

632
Visualizações
How to refresh mat-table data source after deleting item through mat-dialog

I would like to refresh the items showed in a mat-table after deleting one item, without accessing again to server. It is working fine if I am deleting the item directly from delete button in a row, but it is not working when using a mat-dialog.

Here is my code doing directly (this is working):

HTML:

<button mat-button (click)="delete(element)"></button>

TS:

    public delete(element) {
    this.myService.delete(element.ID).then(() => {
      const index = this.dataSource.data.findIndex(q => q.ID == element.ID);
      this.dataSource.data.splice(index, 1);
    })
  }

And here my code doing thorough mat-dialog, where it is not refresing the table:

HTML:

<button mat-button (click)="openDialog(element)"></button>

TS:

public openDialog(element) {
    const dialogRef = this.dialog.open(DeleteItemDialogComponent, {
      panelClass: ['backOfficeDialog'],
      data: {itemType: 'Document'}
    });
    dialogRef.afterClosed().subscribe(response => {
      if (response.event == 'Delete') {
        this.delete(element);
      }
    });
  }

  public delete(element) {
    this.myService.delete(element.ID).then(() => {
      const index = this.dataSource.data.findIndex(q => q.ID == element.ID);
      this.dataSource.data.splice(index, 1);
    })
  }

MatDialogItem:

    export class DeleteItemDialogComponent implements OnInit {

  constructor(
    public dialogRef: MatDialogRef<DeleteItemDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) { }

  ngOnInit() {
  }

  public delete() {
    this.dialogRef.close({event: 'Delete'});
  }

  closeDialog() {
    this.dialogRef.close({event: 'Cancel'});
  }
}

Do you know why in the second case it is not refreshing the table, what I am doing wrong, and how to solve it?

Thank you!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Just call ngOnInit(). But if you have subscribers, they must be unsubcribed into ngOnDestroy, then call first ngOnDestroy and then ngOnInit. This will work fine and update data without refresh page.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda