Monday, August 25, 2014

ABAP Tip - Create internal table dynamically.

DATA:  lf_ditab     TYPE REF TO data.

DATA:   ls_fcat  TYPE lvc_s_fcat,
        lt_fcat  
TYPE lvc_t_fcat.

DATA:  gv_grid TYPE REF TO cl_gui_alv_grid,
       gr_container 
TYPE REF TO cl_gui_custom_container.

FIELD-SYMBOLS:  <fs_dtab> TYPE  ANY TABLE,
                <fs_dwa>   
TYPE ANY.

START-
OF-SELECTION.

  
PERFORM create_field_catalog.

  
PERFORM create_dynamic_table.

  
PERFORM fill_dynamic_table.

  
PERFORM call_screen.

FORM create_field_catalog .
  ls_fcat-fieldname = 
'CARRID'.
  ls_fcat-ref_field = 
'CARRID'.
  ls_fcat-ref_table = 
'SFLIGHT'.
  
APPEND ls_fcat TO lt_fcat.
  
CLEAR ls_fcat.

  ls_fcat-fieldname = 
'CONNID'.
  ls_fcat-ref_field = 
'CONNID'.
  ls_fcat-ref_table = 
'SFLIGHT'.
  
APPEND ls_fcat TO lt_fcat.
  
CLEAR ls_fcat.
ENDFORM.                    " CREATE_FIELD_CATALOG

FORM create_dynamic_table .
** Create dynamic table
  
CALL METHOD cl_alv_table_create=>create_dynamic_table
    
EXPORTING
      it_fieldcatalog           = lt_fcat
    
IMPORTING
      ep_table                  = lf_ditab.
   
  
ASSIGN lf_ditab->* TO <fs_dtab>.
ENDFORM.                               " CREATE_DYNAMIC_TABLE


FORM fill_dynamic_table.
  
SELECT carrid connid
    
FROM sflight  INTO TABLE <fs_dtab>
   
UP TO 10 ROWS.
ENDFORM.                               " FILL_DYNAMIC_TABLE

FORM call_screen .
  
CALL SCREEN 100.
ENDFORM.                               " CALL_SCREEN

MODULE status_0100 OUTPUT.

  
SET PF-STATUS 'MENUBAR'.

* Object for container
  
CREATE OBJECT gr_container
    
EXPORTING
      container_name              = 
'CUSTOM'
    
EXCEPTIONS
      cntl_error                  = 
1
      cntl_system_error           = 
2
      create_error                = 
3
      lifetime_error              = 
4
      lifetime_dynpro_dynpro_link = 
5
      
OTHERS                      = 6.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  
ENDIF.

* Object for Alv grid
  
CREATE OBJECT gv_grid
    
EXPORTING
      i_parent          = gr_container
    
EXCEPTIONS
      error_cntl_create = 
1
      error_cntl_init   = 
2
      error_cntl_link   = 
3
      error_dp_create   = 
4
      
OTHERS            = 5.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  
ENDIF.

* Calling method for displaying the data
  
CALL METHOD gv_grid->set_table_for_first_display
    
CHANGING
      it_outtab                     = <fs_dtab>
      it_fieldcatalog               = lt_fcat
.

ENDMODULE.                             " STATUS_0100  OUTPUT

MODULE user_command_0100 INPUT.
  
CASE sy-ucomm.
    
WHEN 'BACK' OR 'EXIT'.
      
LEAVE TO  SCREEN 0.
    
WHEN 'CANCEL'.
      
LEAVE PROGRAM.
  
ENDCASE.                             " CASE SY-UCOMM

ENDMODULE.                             " USER_COMMAND_0100  INPUT




No comments:

Post a Comment