Home > Net >  How can I alert the success message after I deleted all zero quantity on table using Angular?
How can I alert the success message after I deleted all zero quantity on table using Angular?

Time:05-02

When I click the delete button it will delete all the products with zero quantity. I add an alert message to notify the user if the table has zero quantity or not. The success message will alert after deleting all the products with zero quantity and the error message alert if the table has no zero quantity. I'm using the sweetalert2. The problem is when I deleted all products that have zero quantity the error message showed instead of the success message. I want to show the success message after I deleted products and the error message only shows if the table has no zero quantity. How can I alert the success after I deleted all the products with zero quantity?

//Delete all the row with zero quantity

public onDeleteProduct(): void {
    this.productServive.deleteProduct().subscribe(
      (response: void) => {
        console.log(response);
        this.messageShow();
        this.getAllProduct();
        this.onCloseUpdateHandled();
      },
      (error: HttpErrorResponse) => {
        this.errorMessage(error.message);
      }
    );
  }

// product quantity is zero or not

public messageShow(): void {
    for(const product of this.products) {
      if(product.quantity === 0) {
        this.successMessage("deleted");
      } else {
        this.errorMessage("No more out-of-stock");
      }
    }
  }

//html

<button type="button"  (click)="onDeleteProduct()">Delete</button>

//example table enter image description here

CodePudding user response:

You can only print the errorMessage when you verified that no product has a quantity of 0. so it would be:

public messageShow(): void {
    for(const product of this.products) {
      if(product.quantity === 0) {
        this.successMessage("deleted");
        return;
      }
    }
    this.errorMessage("No more out-of-stock");
}

This method goes through all the products in the list. If it finds one with 0 quantity, it shows the success message and finishes the method (this is what the void return is used for). If it went through all products and has not find any with 0 quantity is shows the error message

  • Related