cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Parameters to Button Press Handler

0 Kudos

Hi everyone. The following works:


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

oDialog.setTitle(sName);

               

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 work when I pass the reference of oDialog


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

oDialog.setTitle(sName);

               

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();        }

Basically, to write generic functions.

Accepted Solutions (1)

Accepted Solutions (1)

former_member183518
Active Participant
0 Kudos

You can pass the dialog instance as a next parameter after the event listener. In the event listener, you can access the dialog instance referring to *this*.


oButton.attachPress(HandleButtonClick , oDialog );

function HandleButtonClick(){ 

     this.close();       // this -> oDialog

}

0 Kudos

Thanks for your help. Just for the sake of curiosity, what would the syntax be if we were to pass two variables to the function? Or are we supposed to pass an array object?


oButton.attachPress(HandleButtonClick , oDialog1, oDialog2 ); 

function HandleButtonClick()

{   

    this.close();       // this -> oDialog  // What would the syntax be???

Thanks again guys.

former_member183518
Active Participant
0 Kudos

attachPress event doesn't allow multiple parameters to be passed. But You can wrap all your parameters into a single Object and pass it.


oButton.attachPress(HandleButtonClick , {param1:oDialog1, param2:oDialog2});


function HandleButtonClick(){

   console.log(this.param1) // would refer to the oDialog1

}

SAPUI5 SDK - Demo Kit


Answers (1)

Answers (1)

karthikarjun
Active Contributor
0 Kudos

Hi Jibran,

Wil this help...? JS Bin - Collaborative JavaScript Debugging

attachPress - is an event - you can invoke method here.

But If you want to pass argument, you should make a function call in attachPress event


Thanks,

Karthik A