0

i'm trying to delete a row from a table. But i got an error that said TypeError: Cannot read property 'value' of undefined when i put the delete button at the end of a row. I followed this video link , but i just need to make the delete button functional without load row data to the form.

//service.ts
deleteProduct(key: string) {
  this.productList.remove(key);
}

//component.ts
onDelete(form: NgForm) {
  //fungsi deleteProduct()
  if (confirm('Are you sure to delete this record ?') == true) {
    this.ProductService.deleteProduct(form.value.$prdKey);
    //this.resetForm(form);
  }
}

onItemClick(prd: Product) {
  this.ProductService.selectedProduct = Object.assign({}, prd);
}
<!--the form-->
<form #productForm="ngForm" (ngSubmit)="onSubmit(productForm)">...</form>

<tr *ngFor="let product of productList">
  <td>{{product.prdName}}</td>
  <td><button type="button" class="btn btn-warning" (click)="onItemClick(product)" title="click to edit or delete" data-toggle="modal" data-target="#myModal">Update</button></td>
  <td><button type="button" class="btn btn-danger" *ngIf="ProductService.selectedProduct.$prdKey == null" (click)="onDelete(ProductService.selectedProduct.$prdKey)">Delete</button></td>
</tr>

The video tutorial version actually is *ngIf="ProductService.selectedProduct.$prdKey != null". I made the *ngIf="ProductService.selectedProduct.$prdKey == null" so the delete button will appear at the end of the row without clicking a row first. Can anyone help me solve this? Please let me know if more snippets are needed. Thank you in advance.

3 Answers 3

1

This is because onDelete() you are passing a key of string type but in onDelete() definition you are expecting it as NgForm type.

Chnage onDelete(form: NgForm) { to onDelete(key: string) {

Try this

onDelete(key: string) {
  //fungsi deleteProduct()
  if (confirm('Are you sure to delete this record ?') == true) {
    this.ProductService.deleteProduct(key);
    //this.resetForm(form);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

try this

//component.ts
onDelete(key: string) {
  //fungsi deleteProduct()
  if (confirm('Are you sure to delete this record ?') == true) {
    this.ProductService.deleteProduct(key);
  this.productList.remove(key);
    //this.resetForm(form);
  }
}

Comments

1

There are lost of issues :

<button type="button" class="btn btn-danger" 
        *ngIf="ProductService.selectedProduct.$prdKey == null" 
        (click)="onDelete(ProductService.selectedProduct.$prdKey)">
            Delete
</button>
  1. ProductService.selectedProduct.$prdKey == null means shows delete when $prdKey is null , so onDelete(ProductService.selectedProduct.$prdKey) this will always pass null to onDelete

  2. onDelete(form: NgForm) , you has set param as NgForm , but you are getting null as explained above

  3. form.value.$prdKey this line will throw error TypeError: Cannot read property 'value' of undefined


You should use : *ngIf="ProductService.selectedProduct.$prdKey != null" as it is a perfect logic , and remove all the products with $prdKey null.

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.