ConfirmDialogConfirmDialog is backed by a service utilizing Observables to display confirmation windows easily that can be shared by multiple actions on the same component.

ConfirmDialog - 图1

Documentation

Import

  1. import {ConfirmDialogModule} from 'primeng/confirmdialog';
  2. import {ConfirmationService} from 'primeng/api';
  3.  

Getting Started

ConfirmDialog is defined using p-confirmDialog tag and an instance of ConfirmationService is required to display it by calling confirm method.

  1. <p-confirmDialog header="Confirmation" icon="pi pi-exclamation-triangle"></p-confirmDialog>
  2. <button type="text" (click)="confirm()" pButton icon="pi pi-check" label="Confirm"></button>
  3.  
  1. export class ConfirmDialogDemo {
  2. constructor(private confirmationService: ConfirmationService) {}
  3. confirm() {
  4. this.confirmationService.confirm({
  5. message: 'Are you sure that you want to perform this action?',
  6. accept: () => {
  7. //Actual logic to perform a confirmation
  8. }
  9. });
  10. }
  11. }
  12.  

Confirm method takes a confirmation instance used to customize the dialog UI along with accept and reject actions.

NameTypeDefaultDescription
messagestringnullMessage of the confirmation.
keystringnullOptional key to match the key of the confirm dialog, necessary to use when component tree has multiple confirm dialogs.
iconstringnullIcon to display next to the message.
headerstringnullHeader text of the dialog.
acceptFunctionnullCallback to execute when action is confirmed.
rejectFunctionnullCallback to execute when action is rejected.
acceptLabelstringnullLabel of the accept button.
rejectLabelstringnullLabel of the reject button.
acceptVisiblebooleantrueVisibility of the accept button.
rejectVisiblebooleantrueVisibility of the reject button.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
blockScrollbooleantrueWhether background scroll should be blocked when dialog is visible.

Customization

Properties of the dialog are defined in two ways, message, icon and header properties can either be defined using confirm method or declaratively on p-confirmDialog ng-template. If these values are unlikely to change then declarative approach would be useful, still properties defined in a ng-template can be overriden with confirm method call.

In addition, buttons at footer section can be customized by passing your own UI, important note to make confirmation work with a custom UI is defining a local ng-template variable for the dialog and assign accept()-reject() methods to your own buttons.

  1. <p-confirmDialog #cd header="Confirmation" icon="pi pi-exclamation-triangle">
  2. <p-footer>
  3. <button type="button" pButton icon="pi pi-times" label="No" (click)="cd.reject()"></button>
  4. <button type="button" pButton icon="pi pi-check" label="Yes" (click)="cd.accept()"></button>
  5. </p-footer>
  6. </p-confirmDialog>
  7.  

Animation Configuration

Transition of the ConfirmDialog open and hide animations can be customized using the transitionOptions property with a default value as 400ms cubic-bezier(0.25, 0.8, 0.25, 1), example below disables the animation altogether.

  1. <p-confirmDialog [transitionOptions]="'0ms'">
  2. </p-confirmDialog>
  3.  

Properties

NameTypeDefaultDescription
headerstringnullTitle text of the dialog.
messagestringnullMessage of the confirmation.
keystringnullOptional key to match the key of confirm object, necessary to use when component tree has multiple confirm dialogs.
iconstringnullIcon to display next to message.
acceptLabelstringYesLabel of the accept button.
acceptIconstringpi pi-checkIcon of the accept button.
acceptVisiblebooleantrueVisibility of the accept button.
rejectLabelstringNoLabel of the reject button.
rejectIconstringpi pi-timesIcon of the reject button.
rejectVisiblebooleantrueVisibility of the reject button.
width (deprecated, use style property)NumberautoWidth of the dialog.
height (deprecated, use style property)NumberautoHeight of the dialog.
closeOnEscapebooleantrueSpecifices if pressing escape key should hide the dialog.
rtlbooleanfalseWhen enabled dialog is displayed in RTL direction.
closablebooleantrueAdds a close icon to the header to hide the dialog.
appendToanynullTarget element to attach the dialog, valid values are "body" or a local ng-template variable of another element.
acceptButtonStyleClassstringnullStyle class of the accept button.
rejectButtonStyleClassstringnullStyle class of the reject button.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
transitionOptionsstring400ms cubic-bezier(0.25, 0.8, 0.25, 1)Transition options of the animation.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-dialogContainer element
ui-confirmdialogContainer element
ui-dialog-titlebarContainer of header.
ui-dialog-titleHeader element.
ui-dialog-titlebar-iconIcon container inside header.
ui-dialog-titlebar-closeClose icon element.
ui-dialog-contentContent element.

Dependencies

ConfirmationService

Source

View on GitHub

  1. <p-confirmDialog [style]="{width: '50vw'}"></p-confirmDialog>
  2. <button type="button" (click)="confirm1()" pButton icon="pi pi-check" label="Confirm"></button>
  3. <button type="button" (click)="confirm2()" pButton icon="pi pi-times" label="Delete"></button>
  4. <p-messages [value]="msgs"></p-messages>
  5.  
  1. @Component({
  2. templateUrl: 'showcase/demo/confirmdialog/confirmdialogdemo.html',
  3. providers: [ConfirmationService]
  4. })
  5. export class ConfirmDialogDemo {
  6. msgs: Message[] = [];
  7. constructor(private confirmationService: ConfirmationService) {}
  8. confirm1() {
  9. this.confirmationService.confirm({
  10. message: 'Are you sure that you want to proceed?',
  11. header: 'Confirmation',
  12. icon: 'pi pi-exclamation-triangle',
  13. accept: () => {
  14. this.msgs = [{severity:'info', summary:'Confirmed', detail:'You have accepted'}];
  15. },
  16. reject: () => {
  17. this.msgs = [{severity:'info', summary:'Rejected', detail:'You have rejected'}];
  18. }
  19. });
  20. }
  21. confirm2() {
  22. this.confirmationService.confirm({
  23. message: 'Do you want to delete this record?',
  24. header: 'Delete Confirmation',
  25. icon: 'pi pi-info-circle',
  26. accept: () => {
  27. this.msgs = [{severity:'info', summary:'Confirmed', detail:'Record deleted'}];
  28. },
  29. reject: () => {
  30. this.msgs = [{severity:'info', summary:'Rejected', detail:'You have rejected'}];
  31. }
  32. });
  33. }
  34. }
  35.