Web Dynpro – Dynamically adding Screen Elements

Home / SAP / ABAP Coding / Web Dynpro – Dynamically adding Screen Elements

Now, one of the big things I needed for Rapier is to have the screens be completely configurable.  In BSP, this accomplished mostly in the page fragments by doing some looping.  This concept doesn’t exist in ABAP Web Dynpro, so I needed to learn how to do it in Web Dynpro.  Here’s what I learned for dynamically adding screen elements to web dynpro view…

First and foremost, if you want to make dynamic elements in a view, you need to have an attribute to go along with it.  As I use Web Dynpro more and more, this is becoming clear to me why.  The easiest way to get a value onto a screen is using a bound attribute.  So you have 2 choices.  You can create every possible attribute in your view, and use some logic to call the correct one, or you can dynamically create the attributes you need.  While either option “could” work for me, I chose to do this dynamically.  Based on future testing/performance, I may change in the future, but regardless, a fun trick J  Once you have the attribute, you can create the element.  In my case, I needed 2 elements.  A label and something else (input field, longtext, etc…).  The process is same for either one.

Some of the groundwork for this.  I’m using matrixlayout to control the transparent container all of these elements are being put in. I’m skipping dropdowns for this post.  I’ll talk more about those in a separate post.  I’ve done my best to leave the data declarations in here, so you have something that is pretty close to working right here 🙂

It’s funny looking back, it’s not much code for as long as it took me to figure it out.  Ha ha ha.

DATA : rootnode_info TYPE REF TO if_wd_context_node_info,
dyn_node_info TYPE REF TO if_wd_context_node_info,
dyn_node TYPE REF TO if_wd_context_node,
dyn_attributes type WDR_CONTEXT_ATTR_INFO_MAP,
dyn_children TYPE WDR_CONTEXT_CHILD_INFO_MAP.
DATA : dyn_attr_info TYPE wdr_context_attribute_info.
DATA lr_container TYPE REF TO cl_wd_uielement_container.
DATA lr_input TYPE REF TO cl_wd_input_field.
DATA lr_check TYPE REF TO cl_wd_checkbox.
DATA lr_longtext TYPE REF TO cl_wd_text_edit.
DATA lr_drop TYPE REF TO cl_wd_dropdown_by_idx.
DATA lr_label TYPE REF TO cl_wd_label.
DATA lr_invisible TYPE REF TO cl_wd_invisible_element.
DATA lr_table TYPE REF TO cl_wd_table.
DATA lo_nd_sflight TYPE REF TO if_wd_context_node.
DATA lr_button TYPE REF TO cl_wd_button.
DATA lr_grid_data TYPE REF TO cl_wd_grid_data.
DATA lr_flow_data TYPE REF TO cl_wd_flow_data.
DATA lr_matrix_hd TYPE REF TO cl_wd_matrix_head_data.
DATA lr_matrix TYPE REF TO cl_wd_matrix_data.
DATA LV_BIND TYPE STRING.
DATA LV_bind_row TYPE STRING.
DATA lv_state(2) TYPE n.
DATA lv_pwd TYPE wdy_boolean.
DATA lv_label TYPE string.
DATA lo_el_context TYPE REF TO if_wd_context_element.
DATA ls_context TYPE wd_this->Element_context.

lo_el_context = wd_context->get_element( ).

*This will create a attribute at run time…
dyn_attr_info-name = field.
rootnode_info = wd_context->get_node_info( ).
dyn_attributes = rootnode_info->get_attributes( ).

* Check to make sure the attribute doesn’t already exist
READ TABLE dyn_attributes with key name = field TRANSPORTING NO FIELDS.
CHECK sy-subrc <> 0.

CALL METHOD rootnode_info->add_attribute( EXPORTING attribute_info = dyn_attr_info ).

*For Dynamically creating Input Field
*Note : Before that change the Layout of ROOTUIELEMENTCONTAINER to MATRIX LAYOUT
lr_container ?= l_view->get_element( CONTAINER ).
lv_bind = field.

case type.
when ‘TEXT’ OR ‘TIME’ or ‘DATE’.
CALL METHOD cl_wd_input_field=>new_input_field
EXPORTING
bind_value = lv_bind
id         = lv_bind
state      = lv_state
on_enter   = ‘ON_ENTER’
RECEIVING
control    = lr_input.
when ‘CHECK’.
CALL METHOD cl_wd_checkbox=>new_checkbox
EXPORTING
bind_checked = lv_bind
id         = lv_bind
state      = lv_state
RECEIVING
control    = lr_check.
when ‘LONGTEXT’.
CALL METHOD cl_wd_text_edit=>new_text_edit
EXPORTING
bind_value = lv_bind
bind_rows  = lv_bind_row
id         = lv_bind
state      = lv_state
RECEIVING
control    = lr_longtext.
ENDCASE.
CALL METHOD cl_wd_label=>new_label
EXPORTING
label_for = lv_bind
text      = lv_label
RECEIVING
control   = lr_label.

*** once the elements are created, they must assigned to the screen as either
*** a matrix head data or matrix data (head starts a new line)
lr_matrix_hd = cl_wd_matrix_head_data=>new_matrix_head_data( lr_label ).
lr_matrix_hd->set_v_align( ’01’ ).
lr_label->set_layout_data( lr_matrix_hd ).
CALL METHOD lr_container->add_child( lr_label ).
case wa_configrma-type.
when ‘TEXT’ OR ‘TIME’ or ‘DATE’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_input ).
lr_input->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_input ).
when ‘CHECK’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_check ).
lr_check->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_check ).
when ‘LONGTEXT’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_longtext ).
lr_longtext->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_longtext ).
ENDCASE.

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

Leave a Reply

Your email address will not be published. Required fields are marked *