Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primeng confirmation service multiple calls

I am using PrimeNg with Angular 6 to generate a confirmation box on deleting an item from a form, and saving all changes made to the form. When

delete() {
  this._confirmationService.confirm({
     message: 'Delete Item?',
     key: 'delete',
     accept: () => {
       // code to delete row
     }
  });
}

submit() {
  this._confirmationService.confirm({
     message: 'Save Changes',
     key: 'submit',
     accept: () => {
       // code to save changes
     }
  });
}

html

<button pButton (click)="delete()"></button>
<button pButton (click)="submit()"></button>

<p-confirmDialog key="delete"></p-confirmDialog>
<p-confirmDialog key="submit"></p-confirmDialog>

When not using a key, both buttons call the submit confirm function. While using keys, the submit button calls the submit confirmation but gets stuck in a loop when accepted, and the delete function calls the submit confirmation then, if rejected, calls the delete confirmation.

What do I need to do so only the confirmation service specific to the function is called?

like image 842
bjwhip Avatar asked Jan 21 '26 10:01

bjwhip


2 Answers

I had a similar issue when rejecting the confirmation dialog, I had to click like 10 times to exit the dialog. It turns out that I was simply missing the confirmationService.close() on the reject event:

confirmDelete(index: number): void {

    this.confirmationService.confirm({
      message: 'Are you sure you want to delete the parameter?',
      accept: () => {
        this.deleteParameter(index);
      },
      reject: () => {
        this.confirmationService.close();
      }
    });
}
like image 196
Henrique Delgado Avatar answered Jan 23 '26 01:01

Henrique Delgado


Try this code :

HTML:

<button type="button" (click)="delete()" pButton icon="pi pi-check" label="Delete"> 
</button>

<button type="button" (click)="submit()" pButton icon="pi pi-times" label="Submit"> 
</button>

<p-confirmDialog ></p-confirmDialog>

TS:

submit() {
    this.confirmationService.confirm({
        message: 'Are you sure that you want to proceed?',
        header: 'Confirmation',
        icon: 'pi pi-exclamation-triangle',
        accept: () => {
            //message here
        },
        reject: () => {
            //message here
        }
    });
}

delete() {
    this.confirmationService.confirm({
        message: 'Do you want to delete this record?',
        header: 'Delete Confirmation',
        icon: 'pi pi-info-circle',
        accept: () => {
            //message here
        },
        reject: () => {
            //message here
        }
    });
}
like image 34
Ömür Alçin Avatar answered Jan 23 '26 00:01

Ömür Alçin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!