Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Introduction

This document will help to create Component Usage dynamically and control the position of the used Components.

Details

We Create 2 Components.

  1. zaddr_cmp_main
  2. zaddr_cmp_sub

zaddr_cmp_main will have component usage at runtime and use zaddr_cmp_sub as component. We will have a Z table to store headings and Position number. Lets us name the table as zheadings.

First we will create zaddr_cmp_sub.

Create Context node TBL with Cardinality 1..N and attribute name email and phone.

Create Context node CAPTION with cardinality 1..1 ,attribute cap and make it as interface node for table caption from Z table

Create Interface Method SET_HEADING and create Importing parameter VALUE

Add following code in set_heading method

    DATA lo_nd_cap TYPE REF TO if_wd_context_node.
 
DATA lo_el_cap TYPE REF TO if_wd_context_element.
 
DATA ls_cap TYPE wd_this->element_caption.

  lo_nd_cap = wd_context->get_child_node( name = wd_this->wdctx_caption ).
  lo_el_cap = lo_nd_cap->get_element( ).
  lo_el_cap->set_attribute( name = 
`CAP` value = value ).

Create Table UI element and bind context

Code for Add Row

DATA lo_nd_tabl TYPE REF TO if_wd_context_node.

DATA lo_el_tabl type REF TO if_wd_context_element.

DATA is_tabl TYPE wd_this->element_tbl.

lo_nd_tabl = wd_context->get_child_node(
'TBL' ).
lo_el_tabl = lo_nd_tabl->bind_element( new_item = is_tabl
                                    set_initial_elements = abap_false ).

Now we create Z table and maintain fields and entries as below

Fields

POSNO field will help to control the position of the component

Entries

Now we create zaddr_cmp_main

We create ViewContainerUIElement dynamically to hold component zaddr_cmp_sub’s view based on Z table entries

Get all the entries in the Z table

SELECT * FROM zheadings INTO TABLE lt_tbl.

Get Component and create component usage group.

Loop thru the table entries and create viewcontaineruielement based on the position no set the layout data. Then add component usage to usage group. Depends on the requirement add the following code in init method or define the action and add the code

To pass the heading from Z table get the interface controller and call the method set_heading

select * from zheadings INTO TABLE lt_tbl.

  lo_cmp = wd_comp_controller->wd_get_api( ).
  lo_usg_grp = lo_cmp->create_cmp_usage_group( name =
'USG_GRP'
                                               used_component =
'ZADDR_CMP_SUB' ).

  lo_view_cntrl = wd_this->wd_get_api( ).

 
CLEAR l_flag.
 
SORT lt_tbl ASCENDING BY posno.
 
LOOP AT lt_tbl INTO ls_tbl.
    lo_vcu = cl_wd_view_container_uielement=>new_view_container_uielement( ).
   
IF l_flag IS INITIAL.
      lo_mhead_data = cl_wd_matrix_head_data=>new_matrix_head_data( lo_vcu ).
      l_flag = abap_true.
   
ELSE.
      lo_mdata = cl_wd_matrix_data=>new_matrix_data( lo_vcu ).
      l_flag = abap_false.
   
ENDIF.
    wd_comp_controller->tcoref->add_child( lo_vcu ).
   
CLEAR: l_pos,l_usg.
    l_pos = lo_vcu->if_wd_view_element~get_id( ).
   
CONCATENATE 'TBL_USAGE' l_pos INTO l_usg.
   
CONCATENATE 'MAIN' '/' l_pos INTO l_pos.
   
lo_cmp_usg = lo_usg_grp->add_component_usage( l_usg ).
    lo_cmp_usg->create_component(
'ZADDR_CMP_SUB' ).
    lo_view_cntrl->prepare_dynamic_navigation(
                source_window_name          =
'WD_MAIN'
                source_vusage_name          =
'MAIN_USAGE_0'
                source_plug_name            =
'OUT'
                target_component_name       =
'ZADDR_CMP_SUB'
                target_component_usage      = l_usg
                target_view_name            =
'WD_MAIN'
                target_plug_name            =
'DEFAULT'
                target_embedding_position   = l_pos ).
   
wd_this->fire_out_plg( ).

    lo_cif ?= lo_cmp_usg->get_interface_controller( ).
    lo_cif->set_heading(
EXPORTING value = ls_tbl-ztext ).

 
ENDLOOP.

Output will be like below

If we want to add another contact details maintainingthe Z table will be enough. By changing the position number we can control the position of the component.

Related Links

http://help.sap.com/saphelp_nw04s/helpdata/en/bc/191a427ff6db2ce10000000a1550b0/content.htm

2 Comments
Labels in this area