(原创)创建非纵深结构的动态内表
有时我们最终要输出的内表字段并不是固定的,可能随着情况的变化而变化,此时我们就需要用到动态内表。一般来讲,创建动态内表的方式有两种。一种是调用cl_alv_table_create 的create_dynamic_table 方法,这种创建只能适用于最终创建的动态内表是非纵深结构(参考的扁平结构内未嵌套扁平结构)的情况,还有一种是调用类CL_ABAP_STRUCTDESCR和类CL_ABAP_TABLEDESCR的相关方法,可以创建纵深结构的动态内表。
纵深结构示例(扁平结构LVC_S_SCOL内嵌套扁平结构LVC_S_COLO):
这次先给出一个创建非纵深结构动态内表的DEMO示例:
输出目标:根据选择屏幕中填入的列名输出ALV,列名之间用逗号分隔。
源代码:
*&---------------------------------------------------------------------* *& Report Z15540_DYN01 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT z15540_dyn01. DATA:gs_layo TYPE lvc_s_layo, gs_fcat TYPE lvc_s_fcat, gt_fcat TYPE lvc_t_fcat. DATA:dyn_table TYPE REF TO data, dyn_line TYPE REF TO data. DATA gt_col TYPE TABLE OF string. FIELD-SYMBOLS:<fs_table> TYPE STANDARD TABLE. PARAMETERS p_col TYPE string OBLIGATORY."填入动态创建的列名,以逗号分隔 START-OF-SELECTION. PERFORM frm_layo. PERFORM frm_fcat. PERFORM frm_create_dyn."创建动态内表&&填充数据 PERFORM frm_alv_dispaly. *&---------------------------------------------------------------------* *& Form FRM_LAYO *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_layo . CLEAR gs_layo. gs_layo-zebra = 'X'. gs_layo-cwidth_opt = 'X'. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_FCAT *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_fcat . SPLIT p_col AT ',' INTO TABLE gt_col. LOOP AT gt_col ASSIGNING FIELD-SYMBOL(<fs_col>). gs_fcat-fieldname = <fs_col>. gs_fcat-coltext = <fs_col> && '描述' . gs_fcat-ref_table = 'MAKT'. gs_fcat-ref_field = 'MAKTX'. APPEND gs_fcat TO gt_fcat. CLEAR gs_fcat. ENDLOOP. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CREATE_DYN *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_create_dyn . DATA: lv_col TYPE i, lv_index(2). "创建动态内表 CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING * i_style_table = it_fieldcatalog = gt_fcat * i_length_in_byte = IMPORTING ep_table = dyn_table * e_style_fname = * EXCEPTIONS * generate_subpool_dir_full = 1 * others = 2 . ASSIGN dyn_table->* TO <fs_table>. CREATE DATA dyn_line LIKE LINE OF <fs_table>. ASSIGN dyn_line->* TO FIELD-SYMBOL(<fs_line>). "填充数据 DESCRIBE TABLE gt_col LINES lv_col. DO lv_col TIMES. lv_index = sy-index. ASSIGN COMPONENT sy-index OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_field>). <fs_field> = '填充值' && lv_index . ENDDO. APPEND <fs_line> TO <fs_table>. DO lv_col TIMES. lv_index = sy-index. ASSIGN COMPONENT sy-index OF STRUCTURE <fs_line> TO <fs_field>. <fs_field> = '填充值' && lv_index . ENDDO. APPEND <fs_line> TO <fs_table>. DO lv_col TIMES. lv_index = sy-index. ASSIGN COMPONENT sy-index OF STRUCTURE <fs_line> TO <fs_field>. <fs_field> = '填充值' && lv_index . ENDDO. APPEND <fs_line> TO <fs_table>. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_ALV_DISPALY *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_alv_dispaly . "显示ALV CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING * I_CALLBACK_PROGRAM = SY-REPID is_layout_lvc = gs_layo it_fieldcat_lvc = gt_fcat TABLES t_outtab = <fs_table> EXCEPTIONS program_error = 1 OTHERS = 2. *if SY-SUBRC <> 0. * message id SY-MSGID type SY-MSGTY number SY-MSGNO * with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. *endif. ENDFORM.