Dynamic internal table is an extension to the internal table concept. It is used when the number of fields is NOT known at the design time or until the compile time. So in this tutorial, I will discuss the basic methods in implementing a dynamic internal table.
First add the static structure. When I say static structure, this is the structure that is declared explicitly in your ABAP program. In my sample codes below, I have two fields for my static structure. (see TYPES: t_output in the sample code below).
Next, count how many no. of columns to be added dynamically. You can compute this from the selection screen from where it will be referenced. So in my sample code below, I have one parameter. Based from this parameter, user will enter how many columns they want to add. So be sure, you must define this point since this is critical where the number of columns will be determined. Remember that the number of column is the number of loop.
Then, combine the dynamic columns together with the static fields. However, before you can combine you must do the loop adding the dynamic column with their fieldname and datatype. (see subroutine f_add_dyn_field in the sample code below)
Finally, create the dynamic table by instantiating it with reference to cl_abap_structdescr . The dynamic table then will be passed to the FM REUSE_ALV_GRID_DISPLAY for display. Remember also in creating the fieldcatalog, you must do a loop to add the dynamic column. Otherwise, it will not be displayed. For reference, see the routine f_build_fieldcatalog.
Complete Sample Code:
Output:
Five columns are added.
thanks a lot again for reading... keep on visiting this blog... enjoy... :)
First add the static structure. When I say static structure, this is the structure that is declared explicitly in your ABAP program. In my sample codes below, I have two fields for my static structure. (see TYPES: t_output in the sample code below).
Next, count how many no. of columns to be added dynamically. You can compute this from the selection screen from where it will be referenced. So in my sample code below, I have one parameter. Based from this parameter, user will enter how many columns they want to add. So be sure, you must define this point since this is critical where the number of columns will be determined. Remember that the number of column is the number of loop.
Then, combine the dynamic columns together with the static fields. However, before you can combine you must do the loop adding the dynamic column with their fieldname and datatype. (see subroutine f_add_dyn_field in the sample code below)
Finally, create the dynamic table by instantiating it with reference to cl_abap_structdescr . The dynamic table then will be passed to the FM REUSE_ALV_GRID_DISPLAY for display. Remember also in creating the fieldcatalog, you must do a loop to add the dynamic column. Otherwise, it will not be displayed. For reference, see the routine f_build_fieldcatalog.
Complete Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | REPORT y_dynamic_table. TYPE-POOLS : abap,slis. TYPES: BEGIN OF t_output, bukrs TYPE bukrs, dmbtr TYPE dmbtr, END OF t_output. DATA: wa_data_ref TYPE REF TO data, wa_struct_type TYPE REF TO cl_abap_structdescr, wa_elem_type TYPE REF TO cl_abap_elemdescr, wa_comp_fld TYPE cl_abap_structdescr=>component, i_comp_tab TYPE cl_abap_structdescr=>component_table, i_comp_tot_tab TYPE cl_abap_structdescr=>component_table, g_total_field TYPE i, g_tabix(2) TYPE c. DATA: i_fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE, g_layout TYPE slis_layout_alv. FIELD-SYMBOLS: <fs_it_output> TYPE STANDARD TABLE, <fs_wa_output> TYPE ANY. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. PARAMETERS: p_nocol(2) TYPE c DEFAULT 5. SELECTION-SCREEN END OF BLOCK b1. START-OF-SELECTION. PERFORM f_add_static_structure. PERFORM f_add_dyn_field. PERFORM f_create_dyn_itab. PERFORM f_display_alv_report. *&--------------------------------------------------------------* *& Form F_CREATE_DYN_ITAB *&--------------------------------------------------------------* FORM f_add_static_structure. wa_struct_type ?= cl_abap_typedescr=>describe_by_name( 'T_OUTPUT' ). i_comp_tab = wa_struct_type->get_components( ). APPEND LINES OF i_comp_tab TO i_comp_tot_tab. ENDFORM. " F_CREATE_DYN_ITAB *&--------------------------------------------------------------* *& Form F_ADD_DYN_FIELD *&--------------------------------------------------------------* FORM f_add_dyn_field . g_tabix = 0. DO p_nocol TIMES. g_tabix = g_tabix + 1. wa_elem_type ?= cl_abap_elemdescr=>describe_by_name( 'DMBTR'). CONCATENATE 'COL_' g_tabix INTO wa_comp_fld-name. wa_comp_fld-type = cl_abap_elemdescr=>get_p( p_length = wa_elem_type->length p_decimals = wa_elem_type->decimals ). APPEND wa_comp_fld TO i_comp_tot_tab. ENDDO. ENDFORM. " F_ADD_DYN_FIELD *&--------------------------------------------------------------* *& Form F_CREATE_TAB *&--------------------------------------------------------------* FORM f_create_dyn_itab. wa_struct_type = cl_abap_structdescr=>create( i_comp_tot_tab ). CREATE DATA wa_data_ref TYPE HANDLE wa_struct_type. ASSIGN wa_data_ref->* TO <fs_wa_output>. CREATE DATA wa_data_ref LIKE STANDARD TABLE OF <fs_wa_output>. ASSIGN wa_data_ref->* TO <fs_it_output>. ENDFORM. " F_CREATE_TAB *&--------------------------------------------------------------* *& Form f_display_alv_report *&--------------------------------------------------------------* FORM f_display_alv_report . PERFORM f_create_layout USING g_layout. PERFORM f_build_fieldcatalog. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'PF_STATUS' is_layout = g_layout it_fieldcat = i_fieldcatalog[] TABLES t_outtab = <fs_it_output> EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. " F_DISPLAY_ALV_REPORT *&--------------------------------------------------------------* *& Form f_create_layout *&--------------------------------------------------------------* FORM f_create_layout USING p_gd_layout TYPE slis_layout_alv. p_gd_layout-zebra = 'X'. p_gd_layout-colwidth_optimize = space. ENDFORM. " f_create_layout *&--------------------------------------------------------------* *& Form pf_status *&--------------------------------------------------------------* FORM pf_status USING ut_extab TYPE slis_t_extab. SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'. ENDFORM. "pfstatus *&--------------------------------------------------------------* *& Form f_build_fieldcatalog *&--------------------------------------------------------------* FORM f_build_fieldcatalog. REFRESH i_fieldcatalog. i_fieldcatalog-fieldname = 'BUKRS'. i_fieldcatalog-seltext_l = 'Company Code'. i_fieldcatalog-outputlen = 20. APPEND i_fieldcatalog TO i_fieldcatalog. CLEAR i_fieldcatalog. i_fieldcatalog-fieldname = 'DMBTR'. i_fieldcatalog-seltext_l = 'Amount'. i_fieldcatalog-outputlen = 10. APPEND i_fieldcatalog TO i_fieldcatalog. CLEAR i_fieldcatalog. g_tabix = 0. DO p_nocol TIMES. g_tabix = g_tabix + 1. wa_elem_type ?= cl_abap_elemdescr=>describe_by_name( 'DMBTR'). CONCATENATE 'COL_' g_tabix INTO wa_comp_fld-name. i_fieldcatalog-fieldname = wa_comp_fld-name. i_fieldcatalog-seltext_m = wa_comp_fld-name. i_fieldcatalog-outputlen = 13. APPEND i_fieldcatalog TO i_fieldcatalog. ENDDO. ENDFORM. "f_build_fieldcatalog |
Output:
Five columns are added.
thanks a lot again for reading... keep on visiting this blog... enjoy... :)
No comments:
Post a Comment