程序解读-ZDEMO_ALVGRID

来源:互联网 发布:软件邀请赛作品 编辑:程序博客网 时间:2024/05/09 11:09

REPORT zdemo_alvgrid.
*&---------------------------------------------------------------------*
*& Report ZDEMO_ALVGRID *
*& *
*&---------------------------------------------------------------------*
*& *
*& Example of a simple ALV Grid Report *
*& ................................... *
*& *
*& The basic requirement for this demo is to display a number of *
*& fields from the EKKO table. *
*&---------------------------------------------------------------------*
*REPORT zdemo_alvgrid .
TABLES: ekko."定义数据对象ekko,其结构为ekko,等同于语句DATA ekko TYPE ekko.
*TYPE-POOLS: slis. "ALV Declarations.声明TYPE-POOLS(类型池),新版本不需要声明可以直接用
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,"定义数据类型
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
statu TYPE ekpo-statu,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
peinh TYPE ekpo-peinh,
END OF t_ekko.
DATA: it_ekko TYPE TABLE OF t_ekko,"定义标准表(内表),可以加 with unique key ,INITIAL SIZE,value is initial,read-only来修饰,如果用了value is initial,必须用INITIAL SIZE
wa_ekko like LINE OF it_ekko."定义数据对象,参考内表it_ekko的一行
*ALV data declarations
DATA: it_fieldcatalog TYPE TABLE OF slis_fieldcat_alv,"定义标准表,WITH HEADER LINE不建议使用
wa_fieldcatalog like line of it_fieldcatalog,"定义数据对象,参考内表it_fieldcatalog的一行
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.
************************************************************************
*Start-of-selection.
START-OF-SELECTION."事假块,一般主处理流程都写在这里
  PERFORM data_retrieval."调用子程序
  PERFORM build_fieldcatalog.
  PERFORM build_layout.
  PERFORM build_events.
  PERFORM build_print_params.
  PERFORM display_alv_report.
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.
* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you more control of the final product.
* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
* I.e. Field type may be required in-order for
* the 'TOTAL' function to work.
  wa_fieldcatalog-fieldname 'EBELN'."
  wa_fieldcatalog-seltext_m 'Purchase Order'."
  wa_fieldcatalog-col_pos 0."
  wa_fieldcatalog-outputlen 10.
  wa_fieldcatalog-emphasize 'X'.
  wa_fieldcatalog-key 'X'.
* fieldcatalog-do_sum = 'X'.
* fieldcatalog-no_zero = 'X'.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'EBELP'.
  wa_fieldcatalog-seltext_m 'PO Item'.
  wa_fieldcatalog-col_pos 1.
  APPEND wa_fieldcatalog TO it_fieldcatalog.

*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'STATU'.
  wa_fieldcatalog-seltext_m 'Status'.
  wa_fieldcatalog-col_pos 2.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'AEDAT'.
  wa_fieldcatalog-seltext_m 'Item change date'.
  wa_fieldcatalog-col_pos 3.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'MATNR'.
  wa_fieldcatalog-seltext_m 'Material Number'.
  wa_fieldcatalog-col_pos 4.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'MENGE'.
  wa_fieldcatalog-seltext_m 'PO quantity'.
  wa_fieldcatalog-col_pos 5.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'MEINS'.
  wa_fieldcatalog-seltext_m 'Order Unit'.
  wa_fieldcatalog-col_pos 6.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'NETPR'.
  wa_fieldcatalog-seltext_m 'Net Price'.
  wa_fieldcatalog-col_pos 7.
  wa_fieldcatalog-outputlen 15.
  wa_fieldcatalog-datatype 'CURR'.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
  wa_fieldcatalog-fieldname 'PEINH'.
  wa_fieldcatalog-seltext_m 'Price Unit'.
  wa_fieldcatalog-col_pos 8.
  APPEND wa_fieldcatalog TO it_fieldcatalog.
*  CLEAR fieldcatalog.
ENDFORM. " BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*& Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
* Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gd_layout-no_input 'X'.
  gd_layout-colwidth_optimize 'X'.
  gd_layout-totals_text 'Totals'(201).
* gd_layout-totals_only = 'X'.
* gd_layout-f2code = 'DISP'. "Sets fcode for when double
* "click(press f2)
* gd_layout-zebra = 'X'.
* gd_layout-group_change_edit = 'X'.
* gd_layout-header_text = 'helllllo'.
ENDFORM. " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* Display report using ALV grid
*----------------------------------------------------------------------*
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             = it_fieldcatalog
*     it_special_groups       = gd_tabgroup
      it_events               gt_events
      is_print                gd_prntparams
      i_save                  'X'
*     is_variant              = z_template
    TABLES
      t_outtab                it_ekko
    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 DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
  UP TO 10 ROWS
  FROM ekpo
  INTO TABLE it_ekko.
ENDFORM. " DATA_RETRIEVAL
*-------------------------------------------------------------------*
* 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(10TYPE c.
* Title
  wa_header-typ 'H'.
  wa_header-info 'EKKO 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(4INTO wa_header-info. "todays date
  APPEND wa_header TO t_header.
  CLEAR: wa_header.
* Total No. of Records Selected
  DESCRIBE TABLE it_ekko 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.                    "top-of-page
*------------------------------------------------------------------*
* FORM USER_COMMAND *
*------------------------------------------------------------------*
* --> R_UCOMM *
* --> RS_SELFIELD *
*------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
* Check function code
  CASE r_ucomm.
    WHEN '&IC1'.
* Check field clicked on within ALVgrid report
      IF rs_selfield-fieldname 'EBELN'.
* Read data table, using index of row user clicked on
        READ TABLE it_ekko INTO wa_ekko INDEX rs_selfield-tabindex.
* Set parameter ID for transaction screen field
        SET PARAMETER ID 'BES' FIELD wa_ekko-ebeln.
* Sxecute transaction ME23N, and skip initial data entry screen
        CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
      ENDIF.
  ENDCASE.
ENDFORM.                    "user_command
*&---------------------------------------------------------------------*
*& Form BUILD_EVENTS
*&---------------------------------------------------------------------*
* Build events table
*----------------------------------------------------------------------*
FORM build_events.
  DATA: ls_event TYPE slis_alv_event.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type 0
    IMPORTING
      et_events   gt_events[].
  READ TABLE gt_events WITH KEY name slis_ev_end_of_page
  INTO ls_event.
  IF sy-subrc 0.
    MOVE 'END_OF_PAGE' TO ls_event-form.
    APPEND ls_event TO gt_events.
  ENDIF.
  READ TABLE gt_events WITH KEY name slis_ev_end_of_list
  INTO ls_event.
  IF sy-subrc 0.
    MOVE 'END_OF_LIST' TO ls_event-form.
    APPEND ls_event TO gt_events.
  ENDIF.
ENDFORM. " BUILD_EVENTS
*&---------------------------------------------------------------------*
*& Form BUILD_PRINT_PARAMS
*&---------------------------------------------------------------------*
* Setup print parameters
*----------------------------------------------------------------------*
FORM build_print_params.
  gd_prntparams-reserve_lines '3'. "Lines reserved for footer
  gd_prntparams-no_coverpage 'X'.
ENDFORM. " BUILD_PRINT_PARAMS
*&---------------------------------------------------------------------*
*& Form END_OF_PAGE
*&---------------------------------------------------------------------*
FORM end_of_page.
  DATA: listwidth TYPE i,
  ld_pagepos(10TYPE c,
  ld_page(10TYPE c.
  WRITE: sy-uline(50).
  SKIP.
  WRITE:/40 'Page:', sy-pagno .
ENDFORM.                    "END_OF_PAGE
*&---------------------------------------------------------------------*
*& Form END_OF_LIST
*&---------------------------------------------------------------------*
FORM end_of_list.
  DATA: listwidth TYPE i,
        ld_pagepos(10TYPE c,
  ld_page(10TYPE c.
  SKIP.
  WRITE:/40 'Page:', sy-pagno .
ENDFORM .                    "END_OF_LIST