Tuesday 22 September 2015

Dynamic creation of UI element and Context attribute in Web Dynpro ABAP-3

--------------------------------------------------------------------------------------------------------------
Post Details- This post shows how to create a context attribute and node dynamically.
After that creating UI elements like Label, Input Field, Button and Table dynamically.


Create a web dynpro component.


 Layout set is flow layout.


 To create a context attribute and a context node, change the WDDOINIT method.

Put the below code to create an attribute and Node dynamically.






 -------------------------------------------------------------------------------------------------------------
METHOD wddoinit .
  DATA :
         lr_node_info  TYPE REF TO if_wd_context_node_info,
         lr_node_info1 TYPE REF TO if_wd_context_node_info,
         ls_attr_info  TYPE        wdr_context_attribute_info.
  CALL METHOD wd_context->get_node_info
    RECEIVING
      node_info = lr_node_info.
  ls_attr_info-name = 'CARRID'.
  ls_attr_info-type_name = 'S_CARR_ID'.
* CREATE A CONTEXT ATTRIBUTE
  CALL METHOD lr_node_info->add_attribute
    EXPORTING
      attribute_info = ls_attr_info.
* Creating a context node with cardinality 'N'
  CALL METHOD cl_wd_dynamic_tool=>create_nodeinfo_from_struct
    EXPORTING
      parent_info    = lr_node_info
      structure_name = 'SFLIGHT'
      is_multiple    = abap_true
*     is_mandatory   = ABAP_FALSE
      node_name      = 'SFLIGHT'
    RECEIVING
      new_node       = lr_node_info1.
ENDMETHOD.

---------------------------------------------------------------------------------------------------------------
To Build UI element Dynamically change the WDDOMODIFYVIEW method.

-----------------------------------------------------------------------------------------------------------------------
METHOD wddomodifyview .
  DATA :
         lr_root_element TYPE REF TO if_wd_view_element,
         lr_container    TYPE REF TO cl_wd_uielement_container,
         lr_label        TYPE REF TO cl_wd_label,
         lr_input        TYPE REF TO cl_wd_input_field,
         lr_button       TYPE REF TO cl_wd_button,
         lr_table        TYPE REF TO cl_wd_table,
         lr_flow_data    TYPE REF TO cl_wd_flow_data,
         lr_node1        TYPE REF TO if_wd_context_node.

  IF first_time = abap_true.

    CALL METHOD view->get_element
      EXPORTING
        id      = 'ROOTUIELEMENTCONTAINER'
      RECEIVING
        element = lr_root_element.

    lr_container ?= lr_root_element.
* create a label
    CALL METHOD cl_wd_label=>new_label
      EXPORTING
        id        = 'L_FLIGHT_CODE'
        label_for = 'FLIGHT_CODE'
      RECEIVING
        control   = lr_label.

    CALL METHOD cl_wd_flow_data=>new_flow_data
      EXPORTING
        element = lr_label
      RECEIVING
        control = lr_flow_data.

    CALL METHOD lr_label->set_layout_data
      EXPORTING
        the_layout_data = lr_flow_data.

    CALL METHOD lr_container->add_child
      EXPORTING
        " index     = 1
        the_child = lr_label.
* Create an input field
    CALL METHOD cl_wd_input_field=>new_input_field
      EXPORTING
        bind_value = 'CARRID'
        id         = 'FLIGHT_CODE'
      RECEIVING
        control    = lr_input.

    CALL METHOD cl_wd_flow_data=>new_flow_data
      EXPORTING
        element = lr_input
      RECEIVING
        control = lr_flow_data.

    CALL METHOD lr_input->set_layout_data
      EXPORTING
        the_layout_data = lr_flow_data.

    CALL METHOD lr_container->add_child
      EXPORTING
        "   index     = 2
        the_child = lr_input.
* Creating a button
    CALL METHOD cl_wd_button=>new_button
      EXPORTING
        enabled   = 'X'
        id        = 'BTN'
        on_action = 'DISP_RECORD'
        text      = 'Display'
      RECEIVING
        control   = lr_button.

    CALL METHOD cl_wd_flow_data=>new_flow_data
      EXPORTING
        element = lr_button
      RECEIVING
        control = lr_flow_data.

    CALL METHOD lr_button->set_layout_data
      EXPORTING
        the_layout_data = lr_flow_data.

    CALL METHOD lr_container->add_child
      EXPORTING
*       index     = 3
        the_child = lr_button.
* Creating a table
    CALL METHOD wd_context->get_child_node
      EXPORTING
        name       = 'SFLIGHT'
      RECEIVING
        child_node = lr_node1.

    CALL METHOD cl_wd_dynamic_tool=>create_table_from_node
      EXPORTING
        ui_parent = lr_container
        table_id  = 'TAB1'
        node      = lr_node1
      RECEIVING
        table     = lr_table.

  ENDIF.
ENDMETHOD.

-----------------------------------------------------------------------------------------------------------------------

For the button the action is given as DISP_RECORD' . So create an action and change the event handler method.


 Put the code to read the attribute value and bind the table.


 --------------------------------------------------------------------------------------------------------------------
METHOD onactiondisp_record .
  DATA : 
        lv_carrid  TYPE          s_carr_id,
         lt_sflight TYPE TABLE OF sflight,
         lr_node1   TYPE REF TO   if_wd_context_node.
  CALL METHOD wd_context->get_attribute
    EXPORTING
      name  = 'CARRID'
    IMPORTING
      value = lv_carrid.
  SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE lt_sflight
                      WHERE carrid = lv_carrid.
  CALL METHOD wd_context->get_child_node
    EXPORTING
      name       = 'SFLIGHT'
    RECEIVING
      child_node = lr_node1.

  CALL METHOD lr_node1->bind_table
    EXPORTING
      new_items = lt_sflight.

ENDMETHOD.


---------------------------------------------------------------------------------------------------------------------

Create an application and test.





--------------------------------------------------------------------------------------------------------------

No comments:

Comments system

Disqus Shortname