运行效果:
代码:
************************************************************************ * 程 序 名:xxx * 程序描述:动态内表 * 事务代码:xxx ************************************************************************ * 修改日志 ************************************************************************ * 日期 版本 修改人 描述 * -------- ---- ------------ ------------------------------------------- * 20221111 1.0 Amell 创建程序 * ************************************************************************ REPORT zpprtest. *********************************************************************** * Tables Definitions ************************************************************************ TABLES: mara. ************************************************************************ * Data Definitions ************************************************************************ TYPES: BEGIN OF ty_period, "期间表 seqno TYPE n LENGTH 2, "期间数 sdate TYPE sy-datum, "期间开始日期 edate TYPE sy-datum, "期间结束日期 ptext TYPE string, "期间描述(日、周、月) END OF ty_period, BEGIN OF ty_period_type, type TYPE string, "期间类型 STOCK库存 WIP在制 name TYPE string, "期间类型名称 END OF ty_period_type, BEGIN OF ty_data, matnr TYPE mara-matnr, "物料 meins TYPE mara-meins, "单位 END OF ty_data, BEGIN OF ty_qty, matnr TYPE mara-matnr, "物料 seqno TYPE n LENGTH 2, "期间数 menge TYPE mard-labst, "数量 type TYPE string, "类型 STOCK库存 WIP在制 meins TYPE mara-meins, "单位 END OF ty_qty. DATA: gt_period TYPE TABLE OF ty_period, gt_period_type TYPE TABLE OF ty_period_type, gt_data TYPE TABLE OF ty_data, gt_qty TYPE TABLE OF ty_qty. DATA: gs_layout TYPE lvc_s_layo, gt_fieldcat TYPE lvc_t_fcat. FIELD-SYMBOLS:<gt_data> TYPE STANDARD TABLE. DATA: gt_table TYPE REF TO data. ************************************************************************ * Includes Module ************************************************************************ ************************************************************************ * Selection Screen ************************************************************************ SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001. PARAMETERS: p_ptype TYPE zpd_type DEFAULT 'M' OBLIGATORY, p_pnum(2) TYPE n DEFAULT 3 OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. ************************************************************************ * Initialization ************************************************************************ INITIALIZATION. ************************************************************************ * At Selection Screen ************************************************************************ AT SELECTION-SCREEN. ************************************************************************ * At Selection Screen Output ************************************************************************ AT SELECTION-SCREEN OUTPUT. ************************************************************************ * Report Format ************************************************************************ TOP-OF-PAGE. END-OF-PAGE. ************************************************************************ * Main Process ************************************************************************ START-OF-SELECTION. "设定期间 PERFORM frm_set_period. "获取数据 PERFORM frm_get_data. "显示数据 PERFORM frm_display_data. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form FRM_SET_PERIOD *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_set_period . DATA: ls_period TYPE ty_period, ls_period_type TYPE ty_period_type. DATA: lv_sdate TYPE sy-datum, lv_edate TYPE sy-datum, lv_week TYPE scal-week, lv_seqno TYPE n LENGTH 2. "期间类型 ls_period_type-type = 'STOCK'. ls_period_type-name = '库存'. APPEND ls_period_type TO gt_period_type. CLEAR ls_period_type. ls_period_type-type = 'WIP'. ls_period_type-name = '在制'. APPEND ls_period_type TO gt_period_type. CLEAR ls_period_type. lv_sdate = sy-datum. "期间 DO p_pnum TIMES. ADD 1 TO lv_seqno. CASE p_ptype. WHEN 'D'. ls_period-seqno = lv_seqno. ls_period-sdate = ls_period-edate = lv_sdate. CONCATENATE 'D' lv_sdate INTO ls_period-ptext SEPARATED BY space. WHEN 'W'. "获取当日周数 PERFORM frm_get_week_by_date USING lv_sdate CHANGING lv_week. "获取当前周数第1天 PERFORM frm_get_first_day_of_week USING lv_week CHANGING lv_sdate. ls_period-seqno = lv_seqno. ls_period-sdate = lv_sdate. ls_period-edate = lv_sdate + 6. CONCATENATE 'W' lv_week INTO ls_period-ptext SEPARATED BY space. WHEN 'M'. "获取月份最后一天 PERFORM frm_get_lastdate_of_month USING lv_sdate CHANGING lv_edate. ls_period-seqno = lv_seqno. ls_period-sdate = lv_sdate+0(6) && '01'. ls_period-edate = lv_edate. CONCATENATE 'M' lv_sdate+0(6) INTO ls_period-ptext SEPARATED BY space. ENDCASE. APPEND ls_period TO gt_period. CLEAR ls_period. "后一天、下周第1天、下个月第1天 lv_sdate = lv_edate + 1. ENDDO. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_GET_WEEK_BY_DATE *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> LV_SDATE *& <-- LV_WEEK *&---------------------------------------------------------------------* FORM frm_get_week_by_date USING iv_date CHANGING ev_week. CALL FUNCTION 'DATE_GET_WEEK' EXPORTING date = iv_date IMPORTING week = ev_week EXCEPTIONS date_invalid = 1 OTHERS = 2. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_GET_FIRST_DAY_OF_WEEK *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> LV_WEEK *& <-- LV_SDATE *&---------------------------------------------------------------------* FORM frm_get_first_day_of_week USING iv_week CHANGING ev_date. CALL FUNCTION 'WEEK_GET_FIRST_DAY' EXPORTING week = iv_week IMPORTING date = ev_date EXCEPTIONS week_invalid = 1 OTHERS = 2. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_GET_LASTDATE_OF_MONTH *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> LV_SDATE *& <-- LV_TDATE *&---------------------------------------------------------------------* FORM frm_get_lastdate_of_month USING iv_date CHANGING ev_date. CALL FUNCTION 'LAST_DAY_OF_MONTHS' EXPORTING day_in = iv_date IMPORTING last_day_of_month = ev_date EXCEPTIONS day_in_no_date = 1 OTHERS = 2. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_GET_DATA *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_get_data . DATA: ls_data TYPE ty_data, ls_qty TYPE ty_qty. ls_data-matnr = '物料A'. ls_data-meins = 'PCS'. APPEND ls_data TO gt_data. CLEAR ls_data. ls_data-matnr = '物料B'. ls_data-meins = 'PCS'. APPEND ls_data TO gt_data. CLEAR ls_data. ls_qty-matnr = '物料A'. ls_qty-seqno = '01'. ls_qty-menge = 10. ls_qty-type = 'STOCK'. ls_qty-meins = 'PCS'. APPEND ls_qty TO gt_qty. CLEAR ls_qty. ls_qty-matnr = '物料A'. ls_qty-seqno = '01'. ls_qty-menge = 30. ls_qty-type = 'WIP'. ls_qty-meins = 'PCS'. APPEND ls_qty TO gt_qty. CLEAR ls_qty. ls_qty-matnr = '物料B'. ls_qty-seqno = '02'. ls_qty-menge = 10. ls_qty-type = 'STOCK'. ls_qty-meins = 'PCS'. APPEND ls_qty TO gt_qty. CLEAR ls_qty. ls_qty-matnr = '物料B'. ls_qty-seqno = '02'. ls_qty-menge = 30. ls_qty-type = 'WIP'. ls_qty-meins = 'PCS'. APPEND ls_qty TO gt_qty. CLEAR ls_qty. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_DISPLAY_DATA *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_display_data . "栏位最适宽度 gs_layout-cwidth_opt = 'X'. "ALV条纹 gs_layout-zebra = 'X'. "ALV栏位 PERFORM frm_create_field. "创建动态表 PERFORM frm_create_dynamic_table. "处理数据 PERFORM frm_process_alv_data. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = gs_layout it_fieldcat_lvc = gt_fieldcat i_save = 'A' TABLES t_outtab = <gt_data> EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CREATE_FIELD *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_create_field . DATA: ls_fieldcat TYPE lvc_s_fcat, ls_period TYPE ty_period, ls_period_type TYPE ty_period_type. DATA: lv_count TYPE i, lv_field TYPE lvc_fname, lv_fieldname TYPE lvc_fname. DEFINE fieldcat. ADD 1 TO lv_count. ls_fieldcat-col_pos = lv_count. ls_fieldcat-fieldname = &1. ls_fieldcat-ref_field = &2. ls_fieldcat-ref_table = &3. ls_fieldcat-qfieldname = &4. ls_fieldcat-scrtext_s = ls_fieldcat-scrtext_m = ls_fieldcat-scrtext_l = &5. ls_fieldcat-colddictxt = &6. APPEND ls_fieldcat TO gt_fieldcat. CLEAR ls_fieldcat. END-OF-DEFINITION. fieldcat 'MATNR' 'MATNR' 'MARA' '' '' ''. LOOP AT gt_period INTO ls_period. LOOP AT gt_period_type INTO ls_period_type. "栏位 lv_field = ls_period_type-type && ls_period-seqno. "栏位名 lv_fieldname = ls_period-ptext && ls_period_type-name. fieldcat lv_field 'LABST' 'MARD' 'MEINS' lv_fieldname 'L'. ENDLOOP. ENDLOOP. fieldcat 'MEINS' 'MEINS' 'MARA' '' '' ''. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_CREATE_DYNAMIC_TABLE *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_create_dynamic_table . CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = gt_fieldcat IMPORTING ep_table = gt_table. IF gt_table IS BOUND. ASSIGN gt_table->* TO <gt_data>. ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_PROCESS_ALV_DATA *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM frm_process_alv_data . FIELD-SYMBOLS: <line> TYPE any, <field> TYPE any. DATA: ls_data TYPE ty_data, ls_qty TYPE ty_qty, ls_period TYPE ty_period, ls_period_type TYPE ty_period_type. DATA: line TYPE REF TO data, lv_field TYPE lvc_fname. ASSIGN gt_table->* TO <gt_data>. CREATE DATA line LIKE LINE OF <gt_data>. ASSIGN line->* TO <line>. SORT: gt_data BY matnr, gt_qty BY matnr seqno type. LOOP AT gt_data INTO ls_data. ASSIGN COMPONENT 'MATNR' OF STRUCTURE <line> TO <field>. <field> = ls_data-matnr. ASSIGN COMPONENT 'MEINS' OF STRUCTURE <line> TO <field>. <field> = ls_data-meins. LOOP AT gt_period INTO ls_period. LOOP AT gt_period_type INTO ls_period_type. "栏位 lv_field = ls_period_type-type && ls_period-seqno. ASSIGN COMPONENT lv_field OF STRUCTURE <line> TO <field>. "栏位值 READ TABLE gt_qty INTO ls_qty WITH KEY matnr = ls_data-matnr seqno = ls_period-seqno type = ls_period_type-type BINARY SEARCH. IF sy-subrc = 0. <field> = ls_qty-menge. ENDIF. ENDLOOP. ENDLOOP. APPEND <line> TO <gt_data>. CLEAR <line>. ENDLOOP. ENDFORM.
标签:gt,内表,ty,type,period,ls,ALV,TYPE,报表 From: https://www.cnblogs.com/StephenAmell/p/16879770.html