Skip to Content

Dynamic NODE creation using RTTC

Hi WD4A colleagues,

currently I'm trying to create a node dynamically to my view context. The node must be able to handle mutiple lines (itab). The creation of the NODE_INFO and the NODE itself seem to work.

But at the moment I'm populating my node with lo_node->bind_table, only the first item of the collection get's a proper element (CL_WDR_CONTEXT_ELEMENT). Anybody an idea what I'm doing wrong?

Thanks and best,

Sander

DATA lo_root_node_info TYPE REF TO cl_wdr_context_node_info.

DATA lo_node_info TYPE REF TO cl_wdr_context_node_info.

DATA lo_root_element TYPE REF TO cl_wdr_context_element.

DATA lo_context TYPE REF TO cl_wdr_controller.

DATA lo_node TYPE REF TO if_wd_context_node.

DATA lo_tabledescr TYPE REF TO cl_abap_tabledescr.

DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

DATA lr_data TYPE REF TO DATA.

DATA lv_node_name TYPE STRING.

FIELD-SYMBOLS <ft> TYPE ANY TABLE.

TYPES: BEGIN OF ty_s,

NUMBER TYPE i,

text TYPE STRING,

END OF ty_s.

TYPES: ty_t TYPE STANDARD TABLE OF ty_s WITH EMPTY KEY.

CREATE DATA lr_data TYPE ty_t.

ASSIGN lr_data->* TO <ft>.

* Needed for testing

DATA lt_table TYPE ty_t.

lt_table = VALUE #( ( NUMBER = 1 text = 'Sample text' ) ( NUMBER = 2 text = 'More text' ) ).

<ft> = lt_table.

lo_tabledescr ?= cl_abap_typedescr=>describe_by_data( <ft> ).

lo_structdescr ?= lo_tabledescr->get_table_line_type( ).

lo_root_node_info ?= wd_context->get_node_info( ).

lo_root_element ?= wd_context->get_lead_selection( ).

lv_node_name = 'DYNAMIC'.

lo_context ?= wd_context->get_context( ).

* remove old entry

TRY.

lo_root_node_info->if_wd_context_node_info~remove_child_node( lv_node_name ).

CATCH cx_wd_context.

ENDTRY.

lo_node_info = cl_wdr_context_node_info=>create(

NAME = lv_node_name

parent = lo_root_node_info

is_mandatory = abap_false "cardinality 0:

is_multiple = abap_true "cardinality :n

is_mandatory_selection = abap_false "selection 0:

is_multiple_selection = abap_false "selection :1

is_singleton = abap_true

is_initialize_lead_selection = abap_true

static_element_rtti = lo_structdescr ).

lo_root_node_info->if_wd_context_node_info~add_child_node(

child_info = lo_node_info

is_static = abap_false ).

lo_node = cl_wdr_context_node=>create(

node_name = lv_node_name

parent_element = lo_root_element

CONTEXT = lo_context

node_info = lo_node_info ).

* At this point, only the first line is populated with

* an element in the CL_WDR_CONTEXT_NODE->COLLECTION part of the

* lo_node instance

lo_node->bind_table( <ft> ).

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    avatar image
    Former Member
    Jan 16, 2015 at 03:13 PM

    You are over complicating things, the standard RTTI method handles everything automatically, even the case where the node exists already. See below.

    data lr_strucdescr type ref to cl_abap_structdescr.

    data lr_node_info type ref to if_wd_context_node_info.

    * initialize

    clear re_node.

    lr_node_info = wd_context->get_node_info( ).

    check lr_node_info is bound.

    * create context node by the name of <STRUCT NAME>

    lr_strucdescr ?= cl_abap_typedescr=>describe_by_name( iv_struct_name ).

    * when already existing do not create

    try.

    lr_node_info = lr_node_info->add_new_child_node(

    name = iv_struct_name

    static_element_rtti = lr_strucdescr

    is_static = abap_false

    is_mandatory = abap_false

    is_mandatory_selection = abap_false

    is_multiple = abap_true

    is_multiple_selection = abap_true

    is_singleton = abap_false ).

    catch cx_wd_context. "#EC NO_HANDLER

    endtry.

    re_node = wd_context->get_child_node( iv_struct_name ).

    then you simply bind the table to the node.

    Add comment
    10|10000 characters needed characters exceeded