ALV中动态内表+行转化为列

ALV中动态内表+行转化为列--继续膜拜老白

转换为:
REPORT z_barry_test_fieldsymols .
TYPE-POOLS: slis.
TABLES: mseg,mkpf.
DATA: gd_fieldcat TYPE slis_t_fieldcat_alv.
DATA: sla TYPE slis_layout_alv ,
      ivariant LIKE disvariant,
      i_repid LIKE sy-repid ,
      i_excluding TYPE slis_t_extab.
TYPES: BEGIN OF ty_sum ,
        werks LIKE mseg-werks ,
        matnr LIKE mseg-matnr ,
        lgort LIKE mseg-lgort ,
        bwart LIKE mseg-bwart ,
        menge LIKE mseg-menge ,
      END OF ty_sum.
DATA : itab TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
DATA : hs_sum TYPE HASHED TABLE OF ty_sum
       WITH UNIQUE KEY werks matnr lgort bwart WITH HEADER LINE .
DATA : itab_sum TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
DATA: BEGIN OF itab_out OCCURS 0,
        werks LIKE mseg-werks ,
        matnr LIKE mseg-matnr ,
        lgort LIKE mseg-lgort ,
        menge01 TYPE i        ,
        menge02 TYPE i        ,
        menge03 TYPE i        ,
        menge04 TYPE i        ,
        menge05 TYPE i        ,
        menge06 TYPE i        ,
        menge07 TYPE i        ,
        menge08 TYPE i        ,
        menge09 TYPE i        ,
        menge10 TYPE i        ,
        menge11 TYPE i        ,
        menge12 TYPE i        ,
        menge13 TYPE i        ,
        menge14 TYPE i        ,
        menge15 TYPE i        ,
        menge16 TYPE i        ,
        menge17 TYPE i        ,
        menge18 TYPE i        ,
        menge19 TYPE i        ,
        menge20 TYPE i        ,
        menge21 TYPE i        ,
        menge22 TYPE i        ,
        menge23 TYPE i        ,
        menge24 TYPE i        ,
        menge25 TYPE i        ,
        menge26 TYPE i        ,
        line_sum TYPE i       ,
      END OF itab_out.
TYPES: BEGIN OF ty_bwart ,
        bwart LIKE mseg-bwart ,
      END OF ty_bwart.
DATA : hs_bwart TYPE HASHED TABLE OF ty_bwart
       WITH UNIQUE KEY bwart WITH HEADER LINE .
DATA : itab_bwart TYPE STANDARD TABLE OF ty_bwart WITH HEADER LINE .
FIELD-SYMBOLS: <f_fs1> ,
               <f_fs2>.
DATA: max_count TYPE i .
SELECT-OPTIONS: s_mblnr FOR mseg-mblnr ,
                s_budat FOR mkpf-budat .
PARAMETERS:     p_sum AS CHECKBOX .
START-OF-SELECTION.
PERFORM getdata.
PERFORM fixdata.
IF p_sum = 'X'.
    PERFORM outdata.
ELSE.
    PERFORM outdata2.
ENDIF.
*&---------------------------------------------------------------------*
*&      Form GETDATA
*&---------------------------------------------------------------------*
FORM getdata.
SELECT werks matnr lgort bwart menge INTO TABLE itab
FROM mkpf INNER JOIN mseg ON mkpf~mblnr = mseg~mblnr AND
                               mkpf~mjahr = mseg~mjahr
WHERE mkpf~mblnr IN s_mblnr AND
        mkpf~budat IN s_budat .
ENDFORM.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form FIXDATA
*&---------------------------------------------------------------------*
FORM fixdata.
DATA: index LIKE sy-tabix .
LOOP AT itab.
    hs_sum = itab.
    COLLECT hs_sum.
    hs_bwart = itab-bwart .
    COLLECT hs_bwart.
ENDLOOP.
SORT hs_bwart.
itab_bwart[] = hs_bwart[].
itab_sum[] = hs_sum[].
LOOP AT itab_sum.
    itab_out-werks = itab_sum-werks .
    itab_out-matnr = itab_sum-matnr .
    itab_out-lgort = itab_sum-lgort .
    READ TABLE itab_bwart WITH KEY bwart = itab_sum-bwart .
    index = sy-tabix + 3.
    ASSIGN COMPONENT index OF STRUCTURE itab_out TO <f_fs1>.
    <f_fs1> = itab_sum-menge.
    itab_out-line_sum = itab_sum-menge.
    COLLECT itab_out.
    CLEAR itab_out.
ENDLOOP.
ENDFORM.                    " FIXDATA
*&---------------------------------------------------------------------*
*&      Form outdata
*&---------------------------------------------------------------------*
FORM outdata.
PERFORM fieldcat_init .
sla-colwidth_optimize = 'X'.
sla-zebra             = 'X'.
i_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = i_repid
            it_fieldcat             = gd_fieldcat[]
            i_save                  = 'A'
            is_variant              = ivariant
            is_layout               = sla
            it_excluding            = i_excluding
            i_callback_user_command = 'USER_COMMAND'
       TABLES
            t_outtab                = itab_sum
       EXCEPTIONS
            program_error           = 1
            OTHERS                  = 2.
ENDFORM.                    " outdata
*---------------------------------------------------------------------*
*       FORM fieldcat_init                                            *
*---------------------------------------------------------------------*
FORM fieldcat_init .
PERFORM frm_catlg_set USING: 'WERKS'     'WERKS' 'X' ,
                               'MATNR'     'MATNR' 'X' ,
                               'LGORT'     'LGORT' 'X' ,
                               'BWART'     'BWART' 'X' ,
                               'MENGE'     'MENGE' '' .
ENDFORM.                    "fieldcat_init
*---------------------------------------------------------------------*
*       FORM frm_catlg_set                                            *
*---------------------------------------------------------------------*
FORM frm_catlg_set USING p_field
                         p_text
                         p_key.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
ls_fieldcat-fieldname     = p_field.
ls_fieldcat-seltext_l     = p_text.
ls_fieldcat-key           = p_key.
IF p_field = 'LINE_SUM'.
    ls_fieldcat-emphasize = 'C700'.
ENDIF.
APPEND ls_fieldcat TO gd_fieldcat .
CLEAR ls_fieldcat .
ENDFORM.                    "frm_catlg_set
*&--------------------------------------------------------------------*
*&      Form user_command
*&--------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
                    rs_selfield TYPE slis_selfield.
READ TABLE itab INDEX rs_selfield-tabindex.
CASE r_ucomm.
    WHEN '&IC1'. "双击
    WHEN 'EXIT'.
      LEAVE PROGRAM.
ENDCASE.
rs_selfield-refresh = 'X'. "自动刷新
ENDFORM. "user_com
*---------------------------------------------------------------------*
*       FORM outdata2                                                 *
*---------------------------------------------------------------------*
FORM outdata2.
PERFORM fieldcat_init2 .
sla-colwidth_optimize = 'X'.
sla-zebra             = 'X'.
i_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = i_repid
            it_fieldcat             = gd_fieldcat[]
            i_save                  = 'A'
            is_variant              = ivariant
            is_layout               = sla
            it_excluding            = i_excluding
            i_callback_user_command = 'USER_COMMAND'
       TABLES
            t_outtab                = itab_out
       EXCEPTIONS
            program_error           = 1
            OTHERS                  = 2.
ENDFORM.                    " outdata
*---------------------------------------------------------------------*
*       FORM fieldcat_init2                                           *
*---------------------------------------------------------------------*
FORM fieldcat_init2 .
DATA: c(2) TYPE n ,
        txt(20) TYPE c .
PERFORM frm_catlg_set USING: 'WERKS'   'WERKS' 'X' ,
                               'MATNR'   'MATNR' 'X' ,
                               'LGORT'   'LGORT' 'X' .
LOOP AT hs_bwart .
    c = c + 1.
    CONCATENATE 'MENGE' c INTO txt.
    PERFORM frm_catlg_set USING: txt hs_bwart-bwart '' .
ENDLOOP.
PERFORM frm_catlg_set USING: 'LINE_SUM' 'SUM' '' .
ENDFORM.                    "fieldcat_init
使用指针动态生成内表的方法:
REPORT z_barry_test_fieldsymols .
TYPE-POOLS: slis.
TABLES: mseg,mkpf.
DATA: gd_fieldcat TYPE slis_t_fieldcat_alv.
DATA: sla TYPE slis_layout_alv ,
      ivariant LIKE disvariant,
      i_repid LIKE sy-repid ,
      i_excluding TYPE slis_t_extab.
TYPES: BEGIN OF ty_sum ,
        werks LIKE mseg-werks ,
        matnr LIKE mseg-matnr ,
        lgort LIKE mseg-lgort ,
        bwart LIKE mseg-bwart ,
        menge LIKE mseg-menge ,
      END OF ty_sum.
DATA : itab TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
DATA : hs_sum TYPE HASHED TABLE OF ty_sum
       WITH UNIQUE KEY werks matnr lgort bwart WITH HEADER LINE .
DATA : itab_sum TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
TYPES: BEGIN OF ty_bwart ,
        bwart LIKE mseg-bwart ,
      END OF ty_bwart.
DATA : hs_bwart TYPE HASHED TABLE OF ty_bwart
       WITH UNIQUE KEY bwart WITH HEADER LINE .
DATA : itab_bwart TYPE STANDARD TABLE OF ty_bwart WITH HEADER LINE .
FIELD-SYMBOLS: <f_fs1> ,
               <f_out> TYPE STANDARD TABLE ,
               <f_out_wa> .
DATA: max_count TYPE i ,
      dy_out    TYPE REF TO data ,
      dy_out_wa TYPE REF TO data,
      xfc       TYPE lvc_s_fcat,
      ifc       TYPE lvc_t_fcat.
DATA: c(2) TYPE n ,
      txt(20) TYPE c .

SELECT-OPTIONS: s_mblnr FOR mseg-mblnr ,
                s_budat FOR mkpf-budat .
PARAMETERS:     p_sum AS CHECKBOX .
START-OF-SELECTION.
PERFORM getdata.
PERFORM fixdata.
IF p_sum = 'X'.
    PERFORM outdata.
ELSE.
    PERFORM outdata2.
ENDIF.
*&---------------------------------------------------------------------*
*&      Form GETDATA
*&---------------------------------------------------------------------*
FORM getdata.
SELECT werks matnr lgort bwart menge INTO TABLE itab
FROM mkpf INNER JOIN mseg ON mkpf~mblnr = mseg~mblnr AND
                               mkpf~mjahr = mseg~mjahr
WHERE mkpf~mblnr IN s_mblnr AND
        mkpf~budat IN s_budat .
ENDFORM.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form FIXDATA
*&---------------------------------------------------------------------*
FORM fixdata.
DATA: index LIKE sy-tabix .
LOOP AT itab.
    hs_sum = itab.
    COLLECT hs_sum.
    hs_bwart = itab-bwart .
    COLLECT hs_bwart.
ENDLOOP.
SORT hs_bwart.
itab_bwart[] = hs_bwart[].
itab_sum[] = hs_sum[].
PERFORM creat_itab_out.
LOOP AT itab_sum.
    ASSIGN COMPONENT 'WERKS' OF STRUCTURE <f_out_wa> TO <f_fs1>.
    <f_fs1> = itab_sum-werks.
    ASSIGN COMPONENT 'MATNR' OF STRUCTURE <f_out_wa> TO <f_fs1>.
    <f_fs1> = itab_sum-matnr.
    ASSIGN COMPONENT 'LGORT' OF STRUCTURE <f_out_wa> TO <f_fs1>.
    <f_fs1> = itab_sum-lgort.
    READ TABLE itab_bwart WITH KEY bwart = itab_sum-bwart .
    index = sy-tabix + 3.
    ASSIGN COMPONENT index OF STRUCTURE <f_out_wa> TO <f_fs1>.
    <f_fs1> = itab_sum-menge.
    ASSIGN COMPONENT 'LINE_SUM' OF STRUCTURE <f_out_wa> TO <f_fs1>.
    <f_fs1> = itab_sum-menge.
    COLLECT <f_out_wa> INTO <f_out> .
    CLEAR <f_out_wa>.
ENDLOOP.
ENDFORM.                    " FIXDATA
*&---------------------------------------------------------------------*
*&      Form outdata
*&---------------------------------------------------------------------*
FORM outdata.
PERFORM fieldcat_init .
sla-colwidth_optimize = 'X'.
sla-zebra             = 'X'.
i_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = i_repid
            it_fieldcat             = gd_fieldcat[]
            i_save                  = 'A'
            is_variant              = ivariant
            is_layout               = sla
            it_excluding            = i_excluding
*            i_callback_user_command = 'USER_COMMAND'
       TABLES
            t_outtab                = itab_sum
       EXCEPTIONS
            program_error           = 1
            OTHERS                  = 2.
ENDFORM.                    " outdata
*---------------------------------------------------------------------*
*       FORM fieldcat_init                                            *
*---------------------------------------------------------------------*
FORM fieldcat_init .
PERFORM frm_catlg_set USING: 'WERKS'     'WERKS' 'X' ,
                               'MATNR'     'MATNR' 'X' ,
                               'LGORT'     'LGORT' 'X' ,
                               'BWART'     'BWART' '' ,
                               'MENGE'     'MENGE' '' .
ENDFORM.                    "fieldcat_init
*---------------------------------------------------------------------*
*       FORM frm_catlg_set                                            *
*---------------------------------------------------------------------*
FORM frm_catlg_set USING p_field
                         p_text
                         p_key.
DATA: ls_fieldcat TYPE slis_fieldcat_alv.
ls_fieldcat-fieldname     = p_field.
ls_fieldcat-seltext_l     = p_text.
ls_fieldcat-key           = p_key.
IF p_field = 'LINE_SUM'.
    ls_fieldcat-emphasize = 'C700'.
ENDIF.
APPEND ls_fieldcat TO gd_fieldcat .
CLEAR ls_fieldcat .
ENDFORM.                    "frm_catlg_set
*&--------------------------------------------------------------------*
*&      Form user_command
*&--------------------------------------------------------------------*
*FORM user_command USING r_ucomm LIKE sy-ucomm
*                    rs_selfield TYPE slis_selfield.
* READ TABLE itab INDEX rs_selfield-tabindex.
* CASE r_ucomm.
*    WHEN '&IC1'. "双击
*
*    WHEN 'EXIT'.
*      LEAVE PROGRAM.
* ENDCASE.
** rs_selfield-refresh = 'X'. "自动刷新
*ENDFORM. "user_com
*---------------------------------------------------------------------*
*       FORM outdata2                                                 *
*---------------------------------------------------------------------*
FORM outdata2.
PERFORM fieldcat_init2 .
sla-colwidth_optimize = 'X'.
sla-zebra             = 'X'.
i_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            i_callback_program      = i_repid
            it_fieldcat             = gd_fieldcat[]
            i_save                  = 'A'
            is_variant              = ivariant
            is_layout               = sla
            it_excluding            = i_excluding
       TABLES
            t_outtab                = <f_out>
       EXCEPTIONS
            program_error           = 1
            OTHERS                  = 2.
ENDFORM.                    " outdata
*---------------------------------------------------------------------*
*       FORM fieldcat_init2                                           *
*---------------------------------------------------------------------*
FORM fieldcat_init2 .
CLEAR c .
PERFORM frm_catlg_set USING: 'WERKS'   'WERKS' 'X' ,
                               'MATNR'   'MATNR' 'X' ,
                               'LGORT'   'LGORT' 'X' .
LOOP AT hs_bwart .
    c = c + 1.
    CONCATENATE 'MENGE' c INTO txt.
    PERFORM frm_catlg_set USING: txt hs_bwart-bwart '' .
ENDLOOP.
PERFORM frm_catlg_set USING: 'LINE_SUM' 'SUM' '' .
ENDFORM.                    "fieldcat_init
*---------------------------------------------------------------------*
*       FORM creat_itab_out                                           *
*---------------------------------------------------------------------*
FORM creat_itab_out.
CLEAR c.
PERFORM build_itab_stru USING: 'WERKS' 'MSEG' 'WERKS' ,
                                 'MATNR' 'MSEG' 'MATNR' ,
                                 'LGORT' 'MSEG' 'LGORT' .
LOOP AT hs_bwart.
    c = c + 1.
    CONCATENATE 'MENGE' c INTO txt.
    PERFORM build_itab_stru USING: txt 'MSEG' 'MENGE' .
ENDLOOP.
PERFORM build_itab_stru USING: 'LINE_SUM' 'MSEG' 'MENGE' .
CALL METHOD cl_alv_table_create=>create_dynamic_table
               EXPORTING
                  it_fieldcatalog = ifc
               IMPORTING
                  ep_table        = dy_out.
ASSIGN dy_out->* TO <f_out>.
CREATE DATA dy_out_wa LIKE LINE OF <f_out>.
ASSIGN dy_out_wa->* TO <f_out_wa>.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM build_itab_stru                                          *
*---------------------------------------------------------------------*
FORM build_itab_stru USING fdname ref_t ref_f.
xfc-fieldname = fdname .
xfc-ref_table = ref_t.
xfc-ref_field = ref_f.
APPEND xfc TO ifc.
ENDFORM.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值