Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7185564
  • 博文数量: 655
  • 博客积分: 10264
  • 博客等级: 上将
  • 技术积分: 8278
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-04 17:47
个人简介

ABAP顾问

文章分类

全部博文(655)

文章存档

2017年(2)

2014年(8)

2013年(3)

2012年(2)

2011年(18)

2010年(102)

2009年(137)

2008年(274)

2007年(134)

分类:

2007-10-31 09:57:18

转换为:
 
 
 
 
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: ,
               .
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 .
    = 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: ,
               TYPE STANDARD TABLE ,
               .
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 TO .
    = itab_sum-werks.
    ASSIGN COMPONENT 'MATNR' OF STRUCTURE TO .
    = itab_sum-matnr.
    ASSIGN COMPONENT 'LGORT' OF STRUCTURE TO .
    = itab_sum-lgort.
 
    READ TABLE itab_bwart WITH KEY bwart = itab_sum-bwart .
    index = sy-tabix + 3.
    ASSIGN COMPONENT index OF STRUCTURE TO .
    = itab_sum-menge.
 
    ASSIGN COMPONENT 'LINE_SUM' OF STRUCTURE TO .
    = itab_sum-menge.
 
    COLLECT INTO   .
    CLEAR .
  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                =
       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 .
  CREATE DATA dy_out_wa LIKE LINE OF .
  ASSIGN dy_out_wa->* TO .
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.
阅读(5903) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-04-30 22:19:46

高手!由衷的佩服! 给我们留点追赶的空间吧。。。。