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

176
Visualizações
Angular Table Delete Operation

In my app, I have a list where the user can add to and delete elements from it. My problem is, when I click an element (it can be in the middle, at the end etc.), it deletes the first element of the list. And when I refresh the page, I can see the previously 'deleted' elements. Like I haven't deleted anything. Here is my code. What's wrong with it and how should I fix it?

HTML:

<button mat-icon-button>
  <mat-icon (click)="deleteWorkItem(row)">block</mat-icon>
</button>

TS:

deleteWorkItem(row: IProduct, index: number) {

  let supplierProduct: ISupplierProduct = {
    Supplier: {
      SupplierId: this.SupplierId
    },
    Product: {
      ProductId: row.ProductId
    }
  };

  this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, {
    disableClose: false
  });
  this.confirmDialogRef.componentInstance.confirmMessage = 'Ürünü silmek istiyor musunuz?';
  this.confirmDialogRef.afterClosed().subscribe(result => {
    if (result) {
      this._service.update('Supplier/DeleteSupplierProduct', supplierProduct).subscribe(response => {
        this._customNotificationService.Show('Ürün silindi', 'Tamam', 2);
      });
      let tempData = this.dataSource.data.slice(0);
      tempData.splice(index, 1);
      this.dataSource = new MatTableDataSource(tempData);
      this.EditIndex = undefined;

      this._products = this.dataSource.data;
      this.ProductChange.emit(this._products);
    }
  });


}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't seem to pass index into deleteWorkItem method.

You need to declare a template variable within *ngFor as follows:

<div *ngFor="let row of data; let i = index">
  ...
  <button mat-icon-button>
    <mat-icon (click)="deleteWorkItem(row, i)">block</mat-icon>
  </button>
</div>
about 4 years ago · Juan Pablo Isaza 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