cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Parameters to Button Press Handler

0 Kudos

Hi everyone.

The following code works:


var oDialog = new sap.ui.commons.Dialog();

oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress(HandleButtonClick);

oDialog.addButton(oButton);

                

// Open Dialog

oDialog.open();

function HandleButtonClick()

{    oDialog.close();        }

But this doesn't. Basically I'm trying to pass reference of oDialog, so that I can close it at a later stage.


var oDialog = new sap.ui.commons.Dialog();

oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress(HandleButtonClick(oDialog));

oDialog.addButton(oButton);

                

// Open Dialog

oDialog.open();

function HandleButtonClick(oDialog)

{    oDialog.close();        }

Thanks in advance guys.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Try using the following code:


var oDialog = new sap.ui.commons.Dialog();

oButton = new sap.ui.commons.Button({

     text: "Close",

     press: HandleButtonClick({dialog: oDialog})

});

oDialog.addButton(oButton);

//Open Dialog

oDialog.open();

function HandleButtonClick(oEvent){

     var dialog = oEvent.getParameter("dialog");

     oDialog.close();

}

0 Kudos

Thanks for your response. I'm getting a "Object doesn't support property or method 'getParameter' " error. I have modified my code as below:


oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress(HandleButtonClick({dialog: oDialog}));

oDialog.addButton(oButton);

        

// Open Dialog

oDialog.open();

function HandleButtonClick(oEvent)

{    var Dialog = oEvent.getParameter("dialog");

    Dialog.close();

}

Thanks

Former Member
0 Kudos

Hi, yea I didn't test the code before I posted that... Sorry for that.

Take a look at the jsbin: https://jsbin.com/vinusub/edit?html,js,output

For if jsbin doesn't work for some reason:


var oDialog = new sap.ui.commons.Dialog(); 

oButton = new sap.ui.commons.Button(); 

oButton.setText("Close");

oButton.attachPress(function(oEvent){HandleButtonClick(oDialog);});

oDialog.addButton(oButton); 

//Open Dialog 

var open = new sap.ui.commons.Button({

  text:"Open dialog",

  press: function(){oDialog.open();}

});

function HandleButtonClick(oDialog){   

     oDialog.close(); 

}

0 Kudos

Thanks for your response. The syntax seems to get crazier by the sec! Anyhow, do you know the technical reasoning why specifying the object directly doesn't work, as you have mentioned in your previous post? Thank you for your time.

Former Member
0 Kudos

So I found another way of passing data along, I think this should be the preferred method:


var oDialog = new sap.ui.commons.Dialog();

function HandleButtonClick(oEvent, data)

{

    var Dialog = data.dialog;

    Dialog.close();

}

oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress({dialog: oDialog}, HandleButtonClick);

oDialog.addButton(oButton);

The jsbin link, it sends multiple data items to the function: JS Bin - Collaborative JavaScript Debugging

The documentation of attachPress shows that you can include custom Data using this method: JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.commons.Button

0 Kudos

Thanks. I will look into this.

Answers (0)