Salesforce Lightning Confirm Dialog Box

If you want to show a modal window, you can use the code below

Lightning Component:

<!--Attributes-->
<aura:attribute name="showConfirmDialogBox" type="boolean" default="false"/>

<!--Component Start-->
<div class="slds-m-around_xx-large">
    <lightning:button name="delete" variant="brand" label="Delete" onclick="{!c.handleConfirmDialog}"/>

    <aura:if isTrue="{!v.showConfirmDialogBox}">
        <!--Modal Box Start-->
        <div role="dialog" class="slds-modal slds-fade-in-open ">
            <div class="slds-modal__container">
                <!--Modal Box Header Start-->
                <header class="slds-modal__header">
                    <h1 class="slds-text-heading--medium">Redirection Confirmation</h1>
                </header>
                <!--Modal Box Header End-->

                <!--Modal Box Content Start-->
                <div class="slds-modal__content slds-p-around--medium">
                    <center><b>You will be redirected to another page, Click Ok to move or Cancel to stay on this page.</b></center>
                </div>
                <!--Modal Box Content End-->

                <!--Modal Box Button Start-->
                <footer class="slds-modal__footer">
                    <lightning:button name='Cancel' label='Cancel' onclick='{!c.handleConfirmDialogCancel}'/>
                    <lightning:button variant="brand" name='Ok' label='Ok' onclick='{!c.handleConfirmDialogOk}'/>
                </footer>
                <!--Modal Box Button End-->
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>            
    </aura:if>
</div>
<!--Component End-->

Lightning JS Controller:

({

handleConfirmDialog : function(component, event, helper) {
    component.set('v.showConfirmDialogBox', true);
},

handleConfirmDialogOk : function(component, event, helper) {
    console.log('Ok');
    component.set('v.showConfirmDialogBox', false);
},

handleConfirmDialogCancel : function(component, event, helper) {
    console.log('Cancel');
    component.set('v.showConfirmDialogBox', false);
},

})

Permanent link to this article: https://salesforcebuddy.com/2020/04/salesforce-lightning-confirm-dialog-box/

Leave a Reply

Your email address will not be published.