Search code examples
sapui5

How to trigger an event after a Dialog resized in SAPUI5?


I have a sap.m.Dialog.

I need to trigger an event handler after the dialog is resized.

How can I register this event handler while the Dialog in SAPUI5 does not have a resize event.


Solution

  • As per documentation sap.m.Dialog has a resizable property that allows sap.m.Dialog to have a resize handler.If you set that property to True you can use sap.ui.core.ReisizeHandler to handle that event.

    See the below example on jsbin as well.

    var oDialogResize = new sap.m.Dialog('resizableDialog', {
                title: "Resizable Dialog",
                resizable: true,
                content: [
                    new sap.m.Label("label1",{text: ""}),
                    new sap.m.ToolbarSpacer(),
                    new sap.m.Label("label2",{text: ""})
                ],
                endButton: new sap.m.Button('resizeDialogCloseButton', {
                    text: "Cancel", press: function () {
                        oDialogResize.close();
                    }
                })
            });
    
    
    
    var oButton = new sap.m.Button({
      text: 'Open',
      press: function() {
        oDialogResize.open();
      }
      
    }).placeAt('content');
    
    oButton.addEventDelegate({
         "onAfterRendering": function () {
              sap.ui.core.ResizeHandler.register(oDialogResize, function(oEvent){
                console.log("ResizeEvent");
                sap.ui.getCore().byId("label1").setText(" W:" + oEvent.size.width);
                 sap.ui.getCore().byId("label2").setText(" H: " + oEvent.size.height);
              });
         }
    }, this);
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>JS Bin</title>
        <script 
                src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" 
                id="sap-ui-bootstrap" 
                data-sap-ui-theme="sap_bluecrystal" 
                data-sap-ui-xx-bindingSyntax="complex" 
                data-sap-ui-libs="sap.m"></script>
      </head>
      <body class="sapUiBody">
        <div id='content'></div>
      </body>
    </html>