Open In App

Angular PrimeNG ConfirmDialog Customization

Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is a framework used with angular to create components with great styling this framework is very easy to use and is used to make responsive websites. In this article, we will know how to use the ConfirmDialog Customization component in Angular PrimeNG. 

The ConfirmDialog Component is used to make a dialog box containing confirm button to confirm the element.

Angular PrimeNG ConfirmDialog Customization Properties:

  • message: It is the message of the confirmation. It is of string data type, the default value is null.
  • key: It is the Optional key to match the key of the confirm dialog. It is of string data type, the default value is null.
  • icon: It is the Icon to display next to the message. It is of string data type, the default value is null.
  • header: It is the Header text of the dialog. It is of string data type, the default value is null.
  • accept: It is the Callback to execute when action is confirmed.
  • reject: It is the Callback to execute when an action is rejected
  • acceptLabel: It is the Label of the accept button. It is of string data type, the default value is null.
  • rejectLabel: It is the Label of the reject button. It is of string data type, the default value is null.
  • acceptIcon: It is the Icon of the accept button. It is of string data type, the default value is null.
  • rejectIcon: It is the Icon of the reject button. It is of string data type, the default value is null.
  • acceptButtonStyleClass: It is used to set the Style class of the accept button. It is of string data type, the default value is null.
  • rejectButtonStyleClass: It is used to set the Style class of the reject button. It is of string data type, the default value is null.
  • acceptVisible: It is used to set the Visibility of the accept button. It is of boolean datatype & the default value is false.
  • rejectVisible: It is used to set the Visibility of the reject button. It is of boolean datatype & the default value is false.
  • style: It is the Inline style of the component. It is of object data type, the default value is null.
  • styleClass: It is the Style class of the component. It is of string data type, the default value is null.
  • maskStyleClass: It is the Style class of the mask. It is of string data type, the default value is null.
  • blockScroll: It is used to specify Whether the background scroll should be blocked when the dialog is visible. It is of boolean datatype & the default value is false.
  • closeOnEscape: It Specifies if pressing the escape key should hide the dialog. It is of boolean datatype & the default value is false.
  • dismissableMask: It Specifics if clicking the modal background should hide the dialog. It is of boolean datatype & the default value is false.
  • defaultFocus: It is the Element to receive the focus when the dialog gets visible.

Creating Angular Application And Installing Module:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following.

 

Steps to run the application: Run the below command to see the output

npm run

Example 1: Below is the example code that illustrates the use of Angular PrimeNG ConfirmDialog Customization.

app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ConfirmDialog Customisation.</h5>
 
<p-confirmDialog #cd [style]="{width: '60vw'}">
    <ng-template pTemplate="header">
        <h3>GeeksforGeeks</h3>
    </ng-template>
 
    <ng-template pTemplate="footer">
        <button type="button" pButton
            icon="pi pi-times" label="reject me!"
            (click)="cd.reject()">
        </button>
       
        <button type="button" pButton
            icon="pi pi-check" label="confirm me!"
            (click)="cd.accept()">
        </button>
    </ng-template>
</p-confirmDialog>
 
<p-button (click)="gfg()"
     icon="pi pi-code"
     label="Custom Confirmation">
</p-button>
 
<p-messages [value]="msgs"></p-messages>


app.component.ts:

Javascript




import { Component } from '@angular/core';
import {ConfirmationService} from 'primeng/api';
import {Message} from 'primeng/api';
import { PrimeNGConfig } from 'primeng/api';
 
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [ConfirmationService]
})
 
export class AppComponent {  
    msgs: Message[] = [];
 
    constructor(private confirmationService: ConfirmationService,
        private primengConfig: PrimeNGConfig
    ){}
 
    gfg() {
        this.confirmationService.confirm({
            message: 'Hey Geek, Are you sure?',
            icon: 'pi pi-exclamation-triangle',
            accept: () => {
                this.msgs = [{
                    severity:'success',
                    summary:'Accepted!',
                    detail:'Geeks has accepted.'
                }];
            },
            reject: () => {
                this.msgs = [{
                    severity:'error',
                    summary:'Refused',
                    detail:'Geeks has Refused.'
                }];
            }
        });
    }
}


app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {RouterModule} from '@angular/router';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
 
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
        MessagesModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
 
export class AppModule { }


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG ConfirmDialog Customization using the position.

app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ConfirmDialog Customisation.</h5>
 
<p-button
    (click)="decidePos('left')"
    label="Custom Left">
</p-button>
  
<p-button
    (click)="decidePos('right')"
    label="Custom Right">
</p-button>
 
<p-confirmDialog #gfg1
    [style]="{width: '60vw'}" key='dialogPos'
    [position]="pos">
    <ng-template pTemplate="header">
        <h3>GeeksforGeeks!</h3>
    </ng-template>
 
    <ng-template pTemplate="footer">
        <button type="button" pButton
            icon="pi pi-times" label="reject me!"
            (click)="gfg1.reject()">
        </button>
        <button type="button" pButton
            icon="pi pi-check" label="confirm me!"
            (click)="gfg1.accept()">
        </button>
    </ng-template>
</p-confirmDialog>
 
<p-messages [value]="ToastMessages"></p-messages>


app.component.ts:

Javascript




import { Component } from '@angular/core';
import { ConfirmationService } from 'primeng/api';
import { Message } from 'primeng/api';
 
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [],
    providers: [ConfirmationService],
})
 
export class AppComponent {
    ToastMessages: Message[];
    pos: string;
 
    constructor(
        private confirmationService: ConfirmationService
    ) {}
 
    decidePos(pos: string) {
        this.pos = pos;
 
        this.confirmationService.confirm({
        message: 'Do you want to buy this course ?',
        header: 'GeeksforGeeks',
        icon: 'pi pi-info-circle',
        accept: () => {
            this.ToastMessages = [
            {
                severity: 'success',
                summary: 'Confirmed!',
                detail: 'You have bought this course.',
            },
            ];
        },
         
        reject: () => {
            this.ToastMessages = [
            {
                severity: 'error',
                summary: 'Failure',
                detail: 'OOPs! Try buying course next time.',
            },
            ];
        },
            key: 'dialogPos',
        });
    }
}


app.module.ts:

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {RouterModule} from '@angular/router';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent }   from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessagesModule } from 'primeng/messages';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
 
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ConfirmDialogModule,
        ButtonModule,
        MessagesModule,
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
 
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/confirmdialog



Last Updated : 20 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads