SAP REPORT程序 简易ALV模板

2023-10-13 18:27:16 浏览数 (2)

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report ZTEST01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST01.


*&---------------------------------------------------------------------*
*          声明表和类型池
*&---------------------------------------------------------------------*
TYPE-POOLS: slis,icon.
TABLES: aufk.

*&---------------------------------------------------------------------*
*          定义选择屏幕
*&---------------------------------------------------------------------*

SELECTION-SCREEN:BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS: lv_aufnr TYPE aufk-aufnr.
SELECTION-SCREEN:END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERS: r_zs RADIOBUTTON GROUP r1 USER-COMMAND mm MODIF ID m1   DEFAULT 'X' ,
            r_ds RADIOBUTTON GROUP r1 MODIF ID m1.

PARAMETERS: p_dlv TYPE C AS CHECKBOX DEFAULT '' MODIF ID m2.
PARAMETERS: p_fhz TYPE C AS CHECKBOX DEFAULT '' MODIF ID m2.
SELECTION-SCREEN:END OF BLOCK b2.

*&---------------------------------------------------------------------*
*          定义结构
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_alv,
        aufnr TYPE aufk-aufnr,
       END OF ty_alv.

*&---------------------------------------------------------------------*
*          定义内表和工作区
*&---------------------------------------------------------------------*
DATA: gt_alv  TYPE STANDARD TABLE OF ty_alv,
      gs_item TYPE ty_alv.

DATA bigmatnr_range TYPE RANGE OF mara-matnr.
*&---------------------------------------------------------------------*
*          定义变量
*&---------------------------------------------------------------------*
FIELD-SYMBOLS: <fs_alv> TYPE ty_alv.

FIELD-SYMBOLS: <fs_alvtab> TYPE STANDARD TABLE.

*&---------------------------------------------------------------------*
*          定义常量
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*          定义ALV参数
*&---------------------------------------------------------------------*

DATA: gt_fieldcat   TYPE lvc_t_fcat,
      gs_fieldcat   TYPE lvc_s_fcat,
      gs_layout     TYPE lvc_s_layo,
      gv_repid      TYPE sy-repid,
      gt_listheader TYPE slis_t_listheader,
      gs_settings   TYPE lvc_s_glay.
*&---------------------------------------------------------------------*
*          定义宏
*&---------------------------------------------------------------------*
DEFINE macro_fcat.
  gs_fieldcat-fieldname  = &1.  "字段名
  gs_fieldcat-ref_table  = &2.  "参考表名
  gs_fieldcat-ref_field  = &3.  "参考字段名
  gs_fieldcat-coltext    = &4.  "列标题
  gs_fieldcat-icon       = &5.  "图标
  gs_fieldcat-edit_mask  = &6.  "为输出编辑掩码
  gs_fieldcat-edit       = &7.  "可编辑
  gs_fieldcat-qfieldname = &8.  "数量参考字段
  gs_fieldcat-fix_column = &9.  "固定列
*  gs_fieldcat-no_zero    = &10. "去除前导零
*  gs_fieldcat-convexit   = &11. "转换例程 "填domain里的例程,如物料的则是MATN1
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.
END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*        LOAD-OF-PROGRAM
*        初始化事件:屏幕初始化加载,默认执行,执行完毕后执行 INITIALIZATION
*        当执行一个1类型(可执行程序),M类型(Module Pool),F类型(Function Group),S类型(Subroutine Pool)程序时,SAP系统自动将程序加载至内存中时,自动调用这个事件
*&---------------------------------------------------------------------*
LOAD-OF-PROGRAM.
*&---------------------------------------------------------------------*
*        INITIALIZATION
*        初始化事件:屏幕初始化赋值(只能书写在可执行程序中)
*&---------------------------------------------------------------------*

INITIALIZATION.
  PERFORM FRM_INIT_DATA.

*&---------------------------------------------------------------------*
*        AT SELECTION-SCREEN OUTPUT
*        PBO事件:屏幕属性组件更改,组件显隐
*       在选择屏幕输出之前执行的事件,一般用来设置选择屏幕的控件属性
*&---------------------------------------------------------------------*

AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
  IF screen-group1 = 'M1'.
    IF r_zs = 'X'.
      screen-invisible = '0'.
      screen-active = '1'.
    ELSE.
      screen-invisible = '1'.
      screen-active = '0'.
    ENDIF.
   MODIFY SCREEN.
  ENDIF.
ENDLOOP.

*&---------------------------------------------------------------------*
*        AT SELECTION-SCREEN
*        PAI事件:屏幕数值变更之后,即点击界面组件或者输入数值之后
*       在选择屏幕有输入之后执行的事件,一般用来进行有效性查询;(PAI 执行完毕后,自动执行一次PBO事件)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  PERFORM FRM_CHECK_AUTHORITY."权限检查

*&---------------------------------------------------------------------*
*        START-OF-SELECTION
*        屏幕数值验证,即按F8之后
*       这个事件是报表程序选择屏幕事件和初始化事件完成后默认进行的事件
*&---------------------------------------------------------------------*

START-OF-SELECTION.
  PERFORM FRM_PROCESS_DATA. " 获取列表数据的处理逻辑

*&---------------------------------------------------------------------*
*        END-OF-SELECTION
*        这个事件报表程序选择完并且处理完数据后 LIST 输出的事件,也是逻辑数据库选择数据结束的标志
*&---------------------------------------------------------------------*

END-OF-SELECTION.

  PERFORM FRM_SET_FIELDCAT.          "设置输出字段
  PERFORM FRM_SET_LAYOUT.            "设置输出格式
  PERFORM FRM_DISPLAY_ALV.           "输出ALV

*&---------------------------------------------------------------------*
*& Form FRM_INIT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_INIT_DATA .

ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_CHECK_AUTHORITY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_CHECK_AUTHORITY .

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .
 " 注意:栏位与布局变式有关系,栏位改变,布局变式不会自动刷新
  MACRO_FCAT 'AUFNR'     'AUFK'  'AUFNR'     '工单号' '' '' '' '' '' .

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
  gv_repid = sy-repid.       "程序名
  gs_layout-cwidth_opt    = 'X'. "自适应宽度
  gs_layout-zebra         = 'X'. "斑马线
  "gs_layout-box_fname     = 'ZZBOX'. "选择框 内表结构声明对应名称的字段即ZZBOX
  gs_layout-countfname = '%ALVCOUNT'.

  gs_settings-edt_cll_cb = 'X'.  "回写
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  FRM_PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_PROCESS_DATA .

  IF lv_aufnr IS INITIAL.
    MESSAGE s005(z_sd_msg) WITH '请至少输入一个条件!' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
  "取数逻辑处理
  gs_item-aufnr = lv_aufnr.
  APPEND gs_item TO gt_alv.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .

  ASSIGN gt_alv TO <fs_alvtab>.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*     I_INTERFACE_CHECK        = ' '
*     I_BYPASSING_BUFFER       =
*     I_BUFFER_ACTIVE          =
      i_callback_program       = gv_repid
*      i_callback_pf_status_set = 'FRM_SET_STATUS'
     i_callback_user_command  = 'FRM_USER_COMMAND'
*     I_CALLBACK_TOP_OF_PAGE   = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE = 'HTML_TOP_OF_PAGE '
*     I_CALLBACK_HTML_END_OF_LIST = ' '
*     I_STRUCTURE_NAME         =
*     I_BACKGROUND_ID          = ' '
*     I_GRID_TITLE             =
      i_grid_settings          = gs_settings
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat[]
*     IT_EXCLUDING             =
*     IT_SPECIAL_GROUPS_LVC    =
*     IT_SORT_LVC              =
*     IT_FILTER_LVC            =
*     IT_HYPERLINK             =
*     IS_SEL_HIDE              =
      i_default                = 'X'
      i_save                   = 'A'
*     IS_VARIANT               =
*     IT_EVENTS                = GT_EVENTS
*     IT_EVENT_EXIT            =
*     IS_PRINT_LVC             =
*     IS_REPREP_ID_LVC         =
*     I_SCREEN_START_COLUMN    = 0
*     I_SCREEN_START_LINE      = 0
*     I_SCREEN_END_COLUMN      = 0
*     I_SCREEN_END_LINE        = 0
*     I_HTML_HEIGHT_TOP        =
*     I_HTML_HEIGHT_END        =
*     IT_ALV_GRAPHICS          =
*     IT_EXCEPT_QINFO_LVC      =
*     IR_SALV_FULLSCREEN_ADAPTER  =
* IMPORTING
*     E_EXIT_CAUSED_BY_CALLER  =
*     ES_EXIT_CAUSED_BY_USER   =
    TABLES
      t_outtab                 = <fs_alvtab>  " 输出的列表数据集  内表
    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.

*&---------------------------------------------------------------------*
*&      Form  FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text  获取alv表事件处理
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING PV_UCOMM TYPE SY-UCOMM
                             PS_SELFIELD TYPE SLIS_SELFIELD .
  CASE PV_UCOMM.
    WHEN '&IC1' .   " 双击
      IF PS_SELFIELD-FIELDNAME = 'AUFNR'.
        READ TABLE GT_ALV ASSIGNING <FS_ALV> INDEX PS_SELFIELD-TABINDEX.  "获取单击行
        IF SY-SUBRC = 0.
          SET PARAMETER ID 'ANR' FIELD <FS_ALV>-AUFNR. "设置内存级别的数据到"ANR"字符里
          CALL TRANSACTION 'CO03' AND SKIP FIRST SCREEN. "跳转到CO03事务码界面,并在第一个屏幕设置"ANR"值
        ENDIF.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
  PS_SELFIELD-REFRESH    = 'X' .   " 当用户在显式界面上对数据进行修改时,同时内表中的数据也随之刷新
  PS_SELFIELD-COL_STABLE = 'X'.
  PS_SELFIELD-ROW_STABLE = 'X'.
ENDFORM.

0 人点赞