How to delete the items from confirmation popup when we click on button

developer1

For the below angular code I have some iteration of data,and now I have to delete the items when we clcik on the delete button it will show the confirmation popup to delete or not if we clcik on yes the particular item has to be removed from the iteration.

.cmponent.ts

public saveHealthyHabits() {
    let isCategoryExist = false;
    let categoryDetails = {
      category: this.clinicalNoteForm.controls.category.value,
      habitDetails: this.healthyHabits.value,
      showDetails: true,
    };
    
    if (this.selectedCategoryDetails) {
      this.selectedCategoryDetails.forEach((selectedCategory) => {
        if (selectedCategory.category === categoryDetails.category) {
          isCategoryExist = true;
          selectedCategory.habitDetails = selectedCategory.habitDetails.concat(
            categoryDetails.habitDetails
          );
        }
      });
    }
   //some code
  }
public deletedata(categoryDetail){
    this.selectedCategoryDetails.forEach((selectedCategory) => {
      //have to add the code here 
})
    
  }

.component.html

 <ng-container *ngFor="let categoryDetail of selectedCategoryDetails">
      <div>
        <div>
          <b>{{ categoryDetail.category }}</b>
        </div>
         </div>

      <div *ngIf="categoryDetail.showDetails">
      <ul>
          <li class="habit-list"
            *ngFor="let habits of categoryDetail.habitDetails" >
        
            <div class="target-details">
              <b>{{ clinicalNoteLabels.target }}: </b
              ><span class="habit-list__value">{{ habits.target }}</span>
            </div>
          </li>
        </ul>
        <div class="habit-footer">
       <span class="m-l-10"  
          [popoverOnHover]="false"
          type="button"
          [popover]="customHabitPopovers"><i class="fa fa-trash-o" ></i> Delete</span>
        </div>
        <div class="clinical-note__popoverdelete">

        <popover-content #customHabitPopovers [closeOnClickOutside]="true">
          <h5>Do you want to delete this habit?</h5>
          <button
          class="btn-primary clinical-note__save"  (click)="deletedata(categoryDetail);customHabitPopovers.hide()">yes </button>
       
        </popover-content></div>
      </div>
    </ng-container>

.component.spec.ts

describe("healthy habits", () => {
    beforeEach(() => {
      component.selectedCategoryDetails = [
        {
          category: "Drink More Water",
          habitDetails: [
            { trigger: "wake up", target: "drink a glass of water" },
          ],
          showDetails: false,
        },
        {
          category: "Drink More Water",
          habitDetails: [
            { trigger: "wake up", target: "drink a glass of water" },
          ],
          showDetails: true,
        },
      ];
    });
    it("should remove habitsAnd triggers", () => {
      component.deletedata(1);
      expect(component.selectedCategoryDetails.length).toBe(1);
    });
});

In the above code after adding the items If I want to delete some particular item when we clcik on item it will show the confirmation popup with some text and buttons yes and no,

So when we click on yes button from the popup it has to remove the particular item. I am new to angular can anyone help me on this

Octavian Mărculescu

You could just filter out the element that needs to be removed:

public deletedata(categoryDetail) {
  this.selectedCategoryDetails = 
    this.selectedCategoryDetails.filter(cd => cd !== categoryDetail);
}

The test that you included passes an index to the delete method, instead of the actual object. You should modify the test to pass the object instead:

it("should remove habitsAnd triggers", () => {
  component.deletedata(component.selectedCategoryDetails[1]);
  expect(component.selectedCategoryDetails.length).toBe(1);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to close the popup modal when we click on save or cancel button

how to disable ng-click event in confirmation Popup Button

How to create a popup on click delete button with ReactJS?

Delete Confirmation when click on link

Popup when click button

How to create a confirmation delete popup in React?

How to hide the delete button in the main body and show it in the next add popup when click on add button using Angular2

( if steatmetn), even if the value is null when I click the button it delete the items

How to stop popup from opening when I click on l.divicon custom html containing a button

How to remove the selected data from saved data when we click on button in a selected one

how can i display items when we click imageView

How to change a image when when we click a button?

Datepicker: How to popup datepicker when click on button and store value in variable

How can we delete table items from redis?

How to delete the exact item on the list when I click the delete button?

How to trigger the modal popup from click a button in ELixir Phoenix

Xamarin IOS: How to display a popup control from button click

How to click on confirmation button using Selenium with Python?

Laravel : How to add a confirmation on delete button?

how we can convert image and text in pdf when button click?

How to close the selected div when we click on the button

Does not delete document from firebase when click on button (onclick method)

How can I set alert confirmation window when delete button pressed

confirmation dialog on delete button

how to validate recyclerView items when click on button in android

show popup menu when click on button

How we can pop element from one table and push the same element in other table when i click the submit button in Javascript?

How to show popup on top of recyclerview item when we click on respective item

How to load more items from a map function on click of a button