Skip to end of metadata
Go to start of metadata

Creating Ui elements like table dynamically in abap web dynpro application .
Step1. Create an abap web dynpro application in SE80 like  YH_DYNAMIC_UI_ELEMENTS with view Main and Window Main_window.

 Step 2.  Create a node SFLIGHT with Attributes as shown

 
Step 3. Create an event 'GET_FLIGHT'  in methods tab and save your Application .

Step 4.  Creating UI Elements dynamically is done in Modifyview Method. Following is the code to create a Input Field and Button Element on the screen

 

METHOD wddomodifyview .


 DATA lr_container  TYPE REF TO cl_wd_uielement_container.

  DATA lr_input      TYPE REF TO cl_wd_input_field.

  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.



* navigate from <CONTEXT> to <SFLIGHT> via lead selection

  lo_nd_sflight = wd_context->get_child_node( name = wd_this->wdctx_sflight ).



  IF first_time = abap_true.



    lr_container ?= view->get_element( 'ROOTUIELEMENTCONTAINER' ).



    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    TYPE REF TO cl_wd_matrix_head_data.



    CALL METHOD cl_wd_input_field=>new_input_field

      EXPORTING

        bind_value = 'SFLIGHT.CARRID'

        id         = 'INPUT1'

      RECEIVING

        control    = lr_input.

    lr_matrix = cl_wd_matrix_head_data=>new_matrix_head_data( lr_input ).

    lr_input->set_layout_data( lr_matrix ).


    CALL METHOD lr_container->add_child

      EXPORTING

        index     = 1

        the_child = lr_input.


    CALL METHOD cl_wd_button=>new_button

      EXPORTING

        enabled   = 'X'

        id        = 'B1'

        on_action = 'GET_FLIGHT'

        text      = 'Get Flight Data'

      RECEIVING

        control   = lr_button.


    lr_matrix = cl_wd_matrix_head_data=>new_matrix_head_data( lr_button ).

    lr_button->set_layout_data( lr_matrix ).

    CALL METHOD lr_container->add_child

      EXPORTING

        index     = 1

        the_child = lr_button.

  ELSE.

   lr_container ?= view->get_element( 'ROOTUIELEMENTCONTAINER' ).


    DATA lo_el_context TYPE ref to if_wd_context_element.

    DATA lv_flag       TYPE wd_this->element_context-flag.



*   get element via lead selection

    lo_el_context = wd_context->get_element( ).


*   get single attribute

    lo_el_context->get_attribute(

      EXPORTING

        name =  `FLAG`

      IMPORTING

        value = lv_flag ).

code to create table dynamically

 
    IF lv_flag EQ '1' .

    CALL METHOD cl_wd_dynamic_tool=>create_table_from_node

      EXPORTING

        ui_parent = lr_container

        table_id  = 'MY_TABLE'

        node      = lo_nd_sflight

      RECEIVING

        table     = lr_table.

    ENDIF.

  ENDIF.

ENDMETHOD.

Step 5. While creating button we assign ACTION to thebutton ..but the pre-requisite is that the action should already be there in created mode.

Step 6. To Poplate Data into the Table

METHOD onactionget_flight .

  DATA lo_nd_sflight TYPE REF TO if_wd_context_node.

  DATA lo_el_sflight TYPE REF TO if_wd_context_element.

  DATA ls_sflight TYPE wd_this->element_sflight.

  DATA lv_carrid  TYPE wd_this->element_sflight-carrid.

  DATA it_flight  TYPE STANDARD TABLE OF sbook.

  DATA wa_flight  LIKE LINE OF it_flight.



* navigate from <CONTEXT> to <SFLIGHT> via lead selection

  lo_nd_sflight = wd_context->get_child_node( name = wd_this->wdctx_sflight ).



* get single attribute

  lo_nd_sflight->get_attribute(

    EXPORTING

      name =  `CARRID`

    IMPORTING

      value = lv_carrid ).



*& Fetch Data from database table



  SELECT carrid                     " Airline

         connid                     " Connection Id

         fldate                     " Flight Date

   FROM sbook

   INTO CORRESPONDING

   FIELDS OF TABLE it_flight

   WHERE carrid EQ lv_carrid.



  IF sy-subrc EQ 0.

    lo_nd_sflight->bind_table( it_flight ).

  ENDIF.



    data  lo_el_context type ref to if_wd_context_element.

    DATA lv_flag TYPE wd_this->element_context-flag.



*   get element via lead selection

    lo_el_context = wd_context->get_element( ).



lv_flag = '1'.



*   set single attribute

    lo_el_context->set_attribute(

      name =  `FLAG`

      value = lv_flag ).



ENDMETHOD.

Step 7. Create an application and save & activate it .

Step 8.

Output :

 

 

  • No labels