Quantcast
Viewing all articles
Browse latest Browse all 789

Working with Dialog Controls in SAP UI5

This blog tells you about types of dialog controls and how to use in SAP UI5

The Dialog control is used to display some information temporarily in a size-limited window in front of the regular application screen, without affecting/removing or modifying the currently displayed content in the main/parent window. Dialog control includes thoughts known from other technologies where traditionally we use to call the windows that appears have names such as dialog box, popup window, alert box, or message box.
It is modal that means that some user action is required before returning to the parent window is possible. To achieve this, you can create an instance of the dialog control using its constructor Dialog() (i.e., new sap.ui.commons.Dialog()). Next, you set the properties for the title and the content, make settings for the size and define other property values.
The MessageBox utility class implements some of the functionality based on the Dialog control. MessageBox defines a template that can be easily configured with the method parameters. Each method of the MessageBox class addresses a different use case: alert(), confirm(), and show().
Syntax for alert() :-
sap.ui.commons.MessageBox.alert("Your Message",'fnCallback',"title");     //fnCallback & title are optional ; but fnCallback is used to handle the event after the user responds
Example:-
sap.ui.commons.MessageBox.alert("You are refused to display the message",'',"Notification");
Output:-
Syntax for confirm() :-
sap.ui.commons.MessageBox.confirm("Your message",fnCallback, "title");  //fnCallback & title are optional ; but fnCallback is used to handle the event after the user responds
Example:-
new sap.ui.commons.Button({text:"Show Message", press : dispAlert}).placeAt("content1");  // Displays a button & when is press the button, this will call the function dispAlert()
function dispAlert()        {
           sap.ui.commons.MessageBox.confirm("Are you want to display message?",rCallAlertBack, "Confirmation");
       returnfalse;
        }
function rCallAlertBack(rValue)       {
if(rValue)    //If User presses 'Ok'
            {
            sap.ui.commons.MessageBox.alert("Welcome to SAPUI5.\n You're now viewing a Simple alert message.",'',"Congrats!");
        returnfalse;
            }
else  // If the user presses 'Cancel'
           {
            sap.ui.commons.MessageBox.alert("You are refussed to display the message",'',"Notification");
            }
    }
Output:-
Once the user presses the button "Show Message" ; below Confirmation window will be popup
When the user presses "Ok" ; rCallAlertBack() will execute the corresponding lines of code and displays as shown below
Syntax for show() :-
sap.ui.commons.MessageBox.show(sMessage,oIcon,sTitle,vActions,fnCallback,oDefaultAction)
where,
sMessage :Message to be displayed    
oIcon        :sap.ui.commons.MessageBox.Icon.WARNING or ERROR or INFORMATION OR CRITICAL OR SUCCESS OR QUESTION
sTitle        :title of the message box
vActions  : sap.ui.commons.MessageBox.Action.YES or sap.ui.commons.MessageBox.Action.NO.(You can also use OK or CANCEL instead of YES or NO)
fnCallback: tohandle the event after the user responds
Example:-
Here is your complete code, Copy & paste in notepad -> save as "*.html (* - anyname) and execute/run in your browser.

<!DOCTYPEHTML>

<html>

<head>

<title>Welcome | SAPUI5 Demo ::Dialog controls</title>

     <scriptsrc="resources/sap-ui-core.js"id="sap-ui-bootstrap"

           data-sap-ui-libs="sap.ui.commons"

           data-sap-ui-theme="sap_goldreflection">

           // Available Themes: sap_goldreflection, sap_platinum and By Default: High Contrast Black

     </script>

     <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->

     <script>

     function dispAlert()   { //Display simple Alert Message

           sap.ui.commons.MessageBox.confirm("Are you want to display message?",rCallAlertBack, "Confirmation");

       returnfalse;

       }


// to call the above function, we create a simple button

     new sap.ui.commons.Button({text:"Show Message", press : dispAlert}).placeAt("content1");

  

// to get back the user selection from the above function

                   

     function rCallAlertBack(rValue)            {

       if(rValue)    //If User presses 'Ok'

                {

             sap.ui.commons.MessageBox.alert("Welcome to SAPUI5.\n You're now viewing a Simple alert message.",'',"Congrats!");

         returnfalse;

                                      }

       else  // If the user presses 'Cancel'

                {

             sap.ui.commons.MessageBox.alert("You are refussed to display the message",'',"Notification");

                }

         }


    jQuery.sap.require("sap.ui.commons.MessageBox");  // this is required since there is no direct access to the box's icons like MessageBox.Icon.WARNING

            

     function fnCallbackMessageBox1(sResult1) {           

               if(sResult1=='OK')           {

                sap.ui.commons.MessageBox.show("You are not authorized to delete this content.\n Please check your access",

                                                                                sap.ui.commons.MessageBox.Icon.WARNING,

                                                                            "Warning",

                                                                                [sap.ui.commons.MessageBox.Action.OK,sap.ui.commons.MessageBox.Action.CANCEL],

                                                                              '', sap.ui.commons.MessageBox.Action.OK);                     

                }

               else                {

                sap.ui.commons.MessageBox.alert("You are refused to display the message",'',"Notification");

                }

           returntrue;

            }


// a callback that will be called when the message box is closed again

            

     function fnCallbackMessageBox(sResult) {

              if(sResult=='OK')                    {

                    sap.ui.commons.MessageBox.show("You are not authorized to delete this content.\nCheck Access and try again",

                                                                                sap.ui.commons.MessageBox.Icon.INFORMATION,

                                                                            "Notification", [sap.ui.commons.MessageBox.Action.OK,sap.ui.commons.MessageBox.Action.CANCEL],

                                                                                 fnCallbackMessageBox1,sap.ui.commons.MessageBox.Action.OK);           

                    }

              else                        {

                    sap.ui.commons.MessageBox.show("Data deleted successfully!\n", sap.ui.commons.MessageBox.Icon.SUCCESS,"Sucess",

                                                                               [sap.ui.commons.MessageBox.Action.OK], '', [sap.ui.commons.MessageBox.Action.OK]);

                    }

          returntrue;

           }

    

   function openMessageBox() {

   /* sap.ui.commons.MessageBox.Icon.WARNING or ERROR or INFORMATION OR CRITICAL OR SUCCESS OR QUESTION

       For the buttons to be displayed, you can use values such as sap.ui.commons.MessageBox.Action.YES or sap.ui.commons.MessageBox.Action.NO.  */


     // open a fully configured message box

                   sap.ui.commons.MessageBox.show("You are not authorized to delete this content.\nCheck Access and try again",

                                                                                          sap.ui.commons.MessageBox.Icon.ERROR,"Error",

                                                                               [sap.ui.commons.MessageBox.Action.OK,sap.ui.commons.MessageBox.Action.RETRY],

                                                                               fnCallbackMessageBox,

                                                                               [sap.ui.commons.MessageBox.Action.OK,sap.ui.commons.MessageBox.Action.RETRY]);

              }

// to call the above function, we create a simple button

              new sap.ui.commons.Button({text:"Delete Content", press : openMessageBox}).placeAt("Content2");

</script>

</head>

<bodyclass="sapUiBody">

<divid="content1"></div>

<br/>

<divid="Content2"></div>

  <br/>

</body>

</html>

 

Sample Outputs:-
Once you press "Delete Content" button,
All the below message box are coded to display.
Error Dialog box:-
Success Dialog box:-
Notification Dialog box:-
Warning Dialog box:-

Viewing all articles
Browse latest Browse all 789

Trending Articles