典型的ALV-GRID report

本文介绍了一个使用ABAP编程语言创建的ALV-GRID报表示例。该报表展示了如何从BKPF和BSEG表中选择数据,并通过ALV-GRID展示凭证抬头及行项目详情。此外,还实现了双击凭证号时调用FB03事务码查看完整凭证的功能。

好久没有写ABAP代码了。今天想复习一下最常用的ALV-GRID报表,于是随便写了一个报表。
第一层显示凭证抬头,第二层显示行项目,第三层直接调用FB03显示凭证。关键是复习一下ALV的一些语法。

 

*&---------------------------------------------------------------------*
*& Report  ZTEST2009                                                                      *
*&                                                                                                       *
*&---------------------------------------------------------------------*
*&                                                                                                       *
*&                                                                                                       *
*&---------------------------------------------------------------------*

REPORT  ZTEST2009                               .

TABLES: bkpf, bseg.

TYPE-POOLS: slis.

DATA: gt_bkpf LIKE TABLE OF bkpf WITH HEADER LINE,
      gt_bseg LIKE TABLE OF bseg WITH HEADER LINE,
      gt_detail LIKE TABLE OF bseg WITH HEADER LINE.

*ALV data declarations
data: fieldcatalog  TYPE slis_t_fieldcat_alv with header line,
      gd_tab_group  TYPE slis_t_sp_group_alv,
      gd_layout     TYPE slis_layout_alv,
      gd_repid      LIKE sy-repid,
      gt_events     TYPE slis_t_event,
      gd_prntparams TYPE slis_print_alv.

data: subfieldcatalog  TYPE slis_t_fieldcat_alv with header line.

PARAMETERS: p_bukrs LIKE bkpf-bukrs,
            p_gjahr LIKE bkpf-gjahr,
            p_blart LIKE bkpf-blart.

SELECT-OPTIONS: s_budat FOR bkpf-budat.

START-OF-SELECTION.
  PERFORM data_retrieval.
  PERFORM build_fieldcatalog.
  PERFORM build_layout.
  PERFORM display_alv_report.
*&---------------------------------------------------------------------*
*&      Form  data_retrieval
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM data_retrieval .
SELECT * FROM bkpf INTO TABLE gt_bkpf WHERE bukrs EQ p_bukrs
                                        AND gjahr EQ p_gjahr
                                        AND blart EQ p_blart
                                        AND budat IN s_budat.

SELECT * FROM bseg INTO TABLE gt_bseg FOR ALL ENTRIES IN gt_bkpf
                                      WHERE bukrs EQ gt_bkpf-bukrs
                                        AND belnr EQ gt_bkpf-belnr
                                        AND gjahr EQ gt_bkpf-gjahr.
ENDFORM.                    " data_retrieval
*&---------------------------------------------------------------------*
*&      Form  build_fieldcatalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
  fieldcatalog-fieldname   = 'BUKRS'.
  fieldcatalog-seltext_m   = '公司代码'.
  fieldcatalog-col_pos     = 0.
  fieldcatalog-outputlen   = 4.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
*  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'BELNR'.
  fieldcatalog-seltext_m   = '会计凭证号码'.
  fieldcatalog-col_pos     = 1.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'GJAHR'.
  fieldcatalog-seltext_m   = '会计年度'.
  fieldcatalog-col_pos     = 2.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'BLART'.
  fieldcatalog-seltext_m   = '凭证类型'.
  fieldcatalog-col_pos     = 3.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.
ENDFORM.                    " build_fieldcatalog
*&---------------------------------------------------------------------*
*&      Form  build_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_layout .
  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-zebra             = 'X'.
  gd_layout-no_vline          = 'X'.
ENDFORM.                    " build_layout
*&---------------------------------------------------------------------*
*&      Form  display_alv_report
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv_report .
  gd_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program      = gd_repid
            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
            i_callback_user_command = 'USER_COMMAND'
*            i_grid_title           = outtext
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
*            it_special_groups       = gd_tabgroup
            it_events               = gt_events
            is_print                = gd_prntparams
            i_save                  = 'X'
*            is_variant              = z_template
       tables
            t_outtab                = gt_bkpf
       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.                    " display_alv_report
*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM user_command USING r_ucomm     LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield.
clear: gt_detail.
refresh: gt_detail.

CASE r_ucomm.                   "Condition on command executed
    WHEN '&IC1'.                  "Double Click
      IF rs_selfield-fieldname = 'BELNR'.
        READ TABLE gt_bkpf INDEX rs_selfield-tabindex.
        LOOP AT gt_bseg WHERE belnr EQ gt_bkpf-belnr.
          APPEND gt_bseg TO gt_detail.
        ENDLOOP.
        IF subfieldcatalog[] is initial.
          PERFORM build_subfieldcatalog.
        ENDIF.
        PERFORM display_sub_alv_report TABLES gt_detail.
      ENDIF.
ENDCASE.

ENDFORM.                    " USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  build_subfieldcatalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_subfieldcatalog .
  subfieldcatalog-fieldname   = 'BUKRS'.
  subfieldcatalog-seltext_m   = '公司代码'.
  subfieldcatalog-col_pos     = 0.
  subfieldcatalog-outputlen   = 4.
  subfieldcatalog-emphasize   = 'X'.
  subfieldcatalog-key         = 'X'.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'BELNR'.
  subfieldcatalog-seltext_m   = '会计凭证号码'.
  subfieldcatalog-col_pos     = 1.
  subfieldcatalog-no_zero     = 'X'.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'GJAHR'.
  subfieldcatalog-seltext_m   = '会计年度'.
  subfieldcatalog-col_pos     = 2.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'BLART'.
  subfieldcatalog-seltext_m   = '凭证类型'.
  subfieldcatalog-col_pos     = 3.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'BUZEI'.
  subfieldcatalog-seltext_m   = '凭证行项目'.
  subfieldcatalog-col_pos     = 4.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'AUGDT'.
  subfieldcatalog-seltext_m   = '清帐日期'.
  subfieldcatalog-col_pos     = 5.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

  subfieldcatalog-fieldname   = 'DMBTR'.
  subfieldcatalog-seltext_m   = '本位币金额'.
  subfieldcatalog-col_pos     = 6.
*  subfieldcatalog-do_sum      = 'X'.
  append subfieldcatalog to subfieldcatalog.
  clear  subfieldcatalog.

ENDFORM.                    " build_subfieldcatalog
*&---------------------------------------------------------------------*
*&      Form  display_sub_alv_report
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_BSEG  text
*----------------------------------------------------------------------*
FORM display_sub_alv_report  TABLES p_lt_bseg STRUCTURE bseg.

  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program      = gd_repid
*            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
            i_callback_user_command = 'SUB_USER_COMMAND'
*            i_grid_title           = outtext
            is_layout               = gd_layout
            it_fieldcat             = subfieldcatalog[]
*            it_special_groups       = gd_tabgroup
            it_events               = gt_events
            is_print                = gd_prntparams
            i_save                  = 'X'
*            is_variant              = z_template
       tables
            t_outtab                = p_lt_bseg
       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.                    " display_sub_alv_report
*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
Form top-of-page.
*ALV Header declarations
data: t_header type slis_t_listheader,
      wa_header type slis_listheader,
      t_line like wa_header-info,
      ld_lines type i,
      ld_linesc(10) type c.

* Title
  wa_header-typ  = 'H'.
  wa_header-info = 'BKPF Table Report'.
  append wa_header to t_header.
  clear wa_header.

* Date
  wa_header-typ  = 'S'.
  wa_header-key = 'Date: '.
  CONCATENATE  sy-datum+6(2) '.'
               sy-datum+4(2) '.'
               sy-datum(4) INTO wa_header-info.   "todays date
  append wa_header to t_header.
  clear: wa_header.

* Total No. of Records Selected
  describe table gt_bkpf lines ld_lines.
  ld_linesc = ld_lines.
  concatenate 'Total No. of Records Selected: ' ld_linesc
                    into t_line separated by space.
  wa_header-typ  = 'A'.
  wa_header-info = t_line.
  append wa_header to t_header.
  clear: wa_header, t_line.

  call function 'REUSE_ALV_COMMENTARY_WRITE'
       exporting
            it_list_commentary = t_header.
*            i_logo             = 'Z_LOGO'.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sub_user_command  USING r_ucomm     LIKE sy-ucomm
                             rs_selfield TYPE slis_selfield.
  CASE r_ucomm.
    WHEN '&IC1'.
      IF RS_SELFIELD-FIELDNAME = 'BELNR'.
        SET PARAMETER ID 'BUK' FIELD p_bukrs.
        READ TABLE gt_detail INDEX rs_selfield-tabindex.
        SET PARAMETER ID 'GJR' FIELD gt_detail-gjahr.
        SET PARAMETER ID 'BLN' FIELD gt_detail-belnr.
        CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
      ENDIF.
  ENDCASE.

ENDFORM.                    " SUB_USER_COMMAND

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值