SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Web Dynpro - How to create dynamic Node in the Context (Dynamic Programming)

Web Dynpro - How to create dynamic Node in the Context (Dynamic Programming)

Web Dynpro - How to create dynamic Node in the Context (Dynamic Programming)

 

Web Dynpro Component “DemoDynamic” explains how to create a node dynamically based on a structure.

Below code creates a child node under the root note ‘ROOT1’ and fills the data dynamically.

Web Dynpro - How to create dynamic Node in the Context (Dynamic Programming)

 


method wdDoInit .

    wd_this->root1_node = wd_context->get_child_node( name = 'ROOT1' ).
    new_structure( ).

endmethod.
 method NEW_STRUCTURE . DATA: structure_name TYPE string, rootnodeinfo TYPE REF TO if_wd_context_node_info. DATA: rtti TYPE REF TO cl_abap_structdescr, rtti_dummy TYPE REF TO cl_abap_typedescr. DATA: param TYPE wdr_name_value, params TYPE wdr_name_value_list, l_rc TYPE i. wd_this->root1_node->get_attribute( EXPORTING name = 'STRUCTURE_NAME' IMPORTING value = structure_name ). TRANSLATE structure_name TO UPPER CASE. l_rc = wd_assist->is_structure_or_table( structure_name ). IF l_rc = 0. * report message * get message manager DATA: l_current_controller TYPE REF TO if_wd_controller, l_message_manager TYPE REF TO if_wd_message_manager, l_text type string. l_current_controller ?= wd_this->wd_get_api( ). l_message_manager = l_current_controller->get_message_manager( ). * report message l_text = wd_assist->get_text( cl_wd_demo_dyn_model=>no_struct ). l_message_manager->report_error_message( EXPORTING message_text = l_text ). ELSE. * create node info rootnodeinfo = wd_this->root1_node->get_node_info( ). wd_assist->create_nodeinfo_from_struct( parent_info = rootnodeinfo structure_name = structure_name is_multiple = abap_true ). wd_this->structure_changed = abap_true. * create and fill node DATA: stru_tab TYPE REF TO data. FIELD-SYMBOLS: TYPE table. DATA: root1_element TYPE REF TO if_wd_context_element. CREATE DATA stru_tab TYPE TABLE OF (structure_name). ASSIGN stru_tab->* TO . root1_element = wd_this->root1_node->get_element( ). IF l_rc = 2. * structure name is a table wd_assist->get_table_data( exporting tab_name = structure_name importing data = ). wd_this->structure_node = wd_assist->fill_node( parent_element = root1_element child_name = structure_name values = ). ELSE. * structure_name is not a table INSERT INITIAL LINE INTO TABLE . wd_this->structure_node = wd_assist->fill_node( parent_element = root1_element child_name = structure_name values = ). ENDIF. ENDIF. endmethod.
Create the Subnode with the name of the structure

method CREATE_NODEINFO_FROM_STRUCT .

  data: l_sdescr      type ref to cl_abap_structdescr,  "#EC NEEDED
        l_tdescr      type ref to cl_abap_typedescr.
*
  data: subnode_info   type ref to if_wd_context_node_info.

* check whether there is a subnode with this name
*  translate structure_name to upper case.                   "#EC NOTEXT

  if node_name is initial.
    node_name = structure_name.
  endif.

  try.
      subnode_info ?= parent_info->get_child_node( name = node_name ).
    catch CX_WD_CONTEXT.
      clear subnode_info.
  endtry.

  if subnode_info is initial.

* check the structure_name
    CL_ABAP_TYPEDESCR=>describe_by_name(
      exporting
        p_name = structure_name
      receiving
        P_DESCR_REF    = l_tdescr
      EXCEPTIONS
        TYPE_NOT_FOUND = 1
        others         = 2
        ).
    if sy-subrc <> 0.
      raise exception type CX_WDR_BAD_ARGS
        exporting parameter = 'STRUCTURE_NAME' argument = structure_name.
    endif.
    try.
        l_sdescr ?= l_tdescr.
      catch CX_SY_MOVE_CAST_ERROR.
        raise exception type CX_WDR_BAD_ARGS
          exporting parameter = 'STRUCTURE_NAME' argument = structure_name.
    endtry.

* create subnode_info
    subnode_info = parent_info->add_new_child_node(
        name                   = node_name
        static_element_type    = structure_name
        IS_MANDATORY           = is_mandatory
        IS_MANDATORY_SELECTION = abap_false
        is_multiple            = is_multiple ).

  endif.

  new_node = subnode_info.
endmethod.

Fill the Subnode with the data

method FILL_NODE .

  translate child_name to upper case.

  node ?= parent_element->get_child_node( name = child_name ).
  if node is not bound.
    return.
  endif.

  case value_kind.
    when 'TABLE'.
      try.
        node->bind_table( new_items = values ).
      catch Cx_Sy_Move_Cast_Error.
        node->bind_elements( new_items = values ).
      endtry.
    when 'STRUCTURE'.
      node->bind_structure( new_item = values ).
    when others.
      node->bind_elements( new_items = values ).
  endcase.
endmethod.

Output:

Web Dynpro - How to create dynamic Node in the Context (Dynamic Programming)

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in