cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically changing a UI caption.text

Former Member
0 Kudos

Hi!

I have a Caption in a View's Layout and i'd like to change its text properties when a user click on a button.

Now i have created the OnAction Event for the button, but i don't know how to point to that Caption!

How can i navigate through ROOTUIELEMENTCONTAINER to this Caption ID ?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

uday_gubbala2
Active Contributor
0 Kudos

Hi Fabio,

The best way for you would be to bind your Captions "text" property to an context attribute & specify your original value over there. By using this approach you can then simply code within your buttons action handler to modify the text by modifying the context attribute using a simple set_attribute( ). This would be the suggested & easy manner.

However am just explaining for your reference as to how you can achieve the same using the dynamic class methods. First obtain & save the reference of your caption into a view level attribute of type ref to CL_WD_CAPTION.

So withing your WDDOMODIFYVIEW you pass your Captions id & obtain its reference:

wd_this->lr_caption = view->get_element( id = 'CAP1' ).

So now within your buttons action handler you can change the text by saying as:

wd_this->lr_caption->set_text( value = 'My new text!' ).

However just please keep it in mind that we are not supposed to save & use the reference of ui elements like in the 2nd approach. We are supposed to modify our layout only within WDDOMODIFYVIEW & SAP wouldn't support any problems that we might run into while following this latter approach.

Regards,

Uday

Former Member
0 Kudos

Hi and thanks to all for your kind answers.

Uday Gubbala, get_element returns an IF_WD_VIEW_ELEMENT type not a CL_WD_CAPTION, but IF_WD_ELEMENT has not SET_TEXT.(instead this is a CL_WD_CAPTION method)

Now i'll try with set_attribute, but i like to test your solution too.

uday_gubbala2
Active Contributor
0 Kudos

Hi Fabio,

Sorry for missing out on this. I usually copy & paste out real working code from my sandbox so that no one faces any problem with the code snippets. This time the server was down and so I had typed it out from my mind. You now know as to why its wrong!

Well you just need to make use of casting and it would work fine. Just correct the earlier code in WDDOMODIFYVIEW & say as:

method WDDOMODIFYVIEW .
  check first_time = abap_true.
    wd_this->lr_caption ?= view->get_element( id = 'CAP1' ).
endmethod.

Below is my buttons action handler method:

method ONACTIONCHANGE_CAPTION .
  wd_this->lr_caption->set_text( value = 'My new text!' ).
endmethod.

So now on click of the button my captions text would change to "My new text!".

My Caption's id is CAP1 & I have defined an attribute lr_caption as type ref to cl_wd_caption in my views Attributes tab... Dont worry this code snippet would work!

Regards,

Uday

Answers (4)

Answers (4)

Former Member
0 Kudos

Context-->Attribute + set_attribute

former_member40425
Contributor
0 Kudos

Hi,

Create a context attribute of type string and bind it with the text property of caption.

And onaction of button set that attribute using code wizard(cntrl+F7). i.e Pass your text at run time.

I hope it helps.

Regards,

Rohit

Former Member
0 Kudos

Hi,

If you want to set a property of the UI element during the dynamic creation, pass the values when you create the element. To enhance the above example with a text on the button to be created (the text has been created before in the Online Text Repository):

 

data:    MY_TEXT       type string.     
 MY_TEXT      =  CL_WD_UTILITIES=>GET_OTR_TEXT_BY_ALIAS( 'MY_TEXT_ALIAS' ).
 LR_BUTTON    =  CL_WD_BUTTON=>NEW_BUTTON( text = MY_TEXT ).

Refer the link also:

http://help.sap.com/saphelp_nw04s/helpdata/EN/77/3545415ea6f523e10000000a155106/frameset.htm

Former Member
0 Kudos

Bind the properties of the Caption which you want to change to context attributes of appropriate type. For binding any property just click on the box next to the property and a pop up will open where you can choose the context attribute. Now in the event handler method just set the context attributes to the values you want and the properties of Caption are set to the value of the context attribute.

Edited by: Pooja Patodia on May 14, 2009 12:01 PM