Dynamically changing Text Area to display all input texts
Often when we need to print a Web Dynpro View, we can just use the browser’s built-in printing method (Ctrl+P). Issue will arise if the contents in our Web Dynpro View are not fully displayed in the printed page. For example, an Text Edit that required us to scroll through to view all the contents.
To solve this problem, we may want to change the Text Edit dynamically to show all the text without scrolling. And yes, we will need to use Dynamic Programming to achieve this.
Before attempt to change the Text Edit size dynamically, we need to figure out how to derive the required size of the Text Edit such that all the text inside will be shown in Printing Mode.
So for example, our original content is like below screenshot:
Notice that there is a scroll bar and we won’t be able to see all of the text when we print the page. Now let say we click on Resize button and we want the page to look like this.
With this, we are able to print the page with all content displayed.
Look at Text Edit’s properties, it seems that the unit of the cols is by “Number of Letter” and the unit of rows is literally “Number of Rows”. From this we can derive how to calculate the rows needed to display text without scrolling:
Rows Needed = Length(Value in Text Edit) / Number of Columns + 1
We add 1 to make sure that there is no scrollable contents. And with this, our problem is solved.
Now we come to the coding part.
Dynamically changing UI elements in Web Dynpro can be done in the hook method WDDOMODIFYVIEW (before the View get rendered). So let say when we click on the Resize button, an Action will be triggered, in the Action, we set a flag states that we want to resize the UI. This chain of events will eventually reach WDDOMODIFYVIEW. And here is our code in WDDOMODIFYVIEW:
METHOD wddomodifyview .
DATA: lr_container TYPE REF TO cl_wd_uielement_container,
lt_ui_children TYPE cl_wd_uielement=>tt_uielement,
lr_text_edit TYPE REF TO cl_wd_text_edit,
lv_value TYPE string,
lv_rows TYPE i,
lv_val_len TYPE i,
lv_cols TYPE i.
TYPE REF TO cl_wd_uielement.
IF first_time EQ abap_false AND wd_this->print_mode = abap_true.
“Get all UI elements in the root container
lr_container ?= view->get_element( ‘ROOTUIELEMENTCONTAINER’ ).
lt_ui_children = lr_container->get_children( ).
“Filter the ones that need to be tweaked in size
IF lt_ui_children IS NOT INITIAL.
LOOP AT lt_ui_children ASSIGNING
CLEAR: lv_rows, lv_cols, lv_value, lv_val_len.
“Down cast UI element to UI Text_Edit, skip the loop if it’s not
lv_value = lr_text_edit->get_value( ).
lv_val_len = STRLEN( lv_value ).
lv_cols = lr_text_edit->get_cols( ).
lv_rows = lv_val_len / lv_cols.
lv_rows = lv_rows + 1.
IF lv_rows > 5.
lr_text_edit->set_rows( lv_rows ).
wd_this->print_mode = abap_false.
The above code is just for demonstration in blog purposes, few thoughts for it:
– The code is generic enough to cover for all Text Edits in our Web Dynpro view
– We can also extend the logic to cover for resizing other UI elements
– We may want to set a default rows value and bind the rows properties of our Text Edit to it
– We should also cater for case when user want to return to original view
– There’s no “INSTANCE OF” in ABAP ????
That’s end of the blog. Please comment and share if you know of other efficient method to print the Web Dynpro view.
Thanks much for reading. Cheers!