SAP ALV demo---自用
作者:互联网
留着自己用,很多代码都是可以复用的,每次开发ALV都重新写一遍太浪费时间了。
*&---------------------------------------------------------------------*
*& Report ZPPU_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALV_TEST.
TYPE-POOLS:SLIS.
TABLES:VBAP,SSCRFIELDS.
*----------------------------------------------------------------------*
* ALV定义
*----------------------------------------------------------------------*
DATA: GW_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
G_LAYOUT TYPE SLIS_LAYOUT_ALV.
*----------------------------------------------------------------------*
* 内表和工作区定义
*----------------------------------------------------------------------*
TYPES:BEGIN OF TY_ALV,
SEL TYPE CHAR1,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
CHARG TYPE VBAP-CHARG,
NETWR TYPE VBAP-NETWR,
KWMENG TYPE VBAP-KWMENG,
END OF TY_ALV.
DATA:GT_ALV TYPE TABLE OF TY_ALV,
WA_ALV TYPE TY_ALV.
*----------------------------------------------------------------------*
* 选择屏幕
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:
S_VBELN FOR VBAP-VBELN. "订单号
SELECTION-SCREEN END OF BLOCK B1.
*----------------------------------------------------------------------*
* Initialization
*----------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* at selection screen
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
AT SELECTION-SCREEN.
*----------------------------------------------------------------------*
* Event top of page
*----------------------------------------------------------------------*
TOP-OF-PAGE.
*----------------------------------------------------------------------*
* event Start of Selection
*----------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM FRM_GET_DATA.
PERFORM FRM_LAYOUT_BUILD.
PERFORM FRM_FIELD_BUILD.
PERFORM FRM_DATA_OUT.
*----------------------------------------------------------------------*
*EVENT End-of selection
*----------------------------------------------------------------------*
END-OF-SELECTION.
*----------------------------------------------------------------------*
*EVENT End-of page
*----------------------------------------------------------------------*
END-OF-PAGE.
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
** forms
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_AUTHORITY
*&---------------------------------------------------------------------*
* " 检查权限
*----------------------------------------------------------------------*
FORM FRM_CHECK_AUTHORITY.
ENDFORM.
*----------------------------------------------------------------------*
** forms
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_LAYOUT_BUILD
*&---------------------------------------------------------------------*
* " 设置表格样式
*----------------------------------------------------------------------*
FORM FRM_LAYOUT_BUILD .
G_LAYOUT-BOX_FIELDNAME = 'SEL'. " 选择模式,在最左端有选择按钮
G_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. " 自动调整列宽
G_LAYOUT-ZEBRA = 'X'. " 表格斑马线
"G_LAYOUT-EDIT = 'X'. "可编辑
G_LAYOUT-INFO_FIELDNAME = 'CLR'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FIELD_BUILD
*&---------------------------------------------------------------------*
* " 建立表头数据
*----------------------------------------------------------------------*
FORM FRM_FIELD_BUILD .
CLEAR GT_FIELDCAT.
PERFORM FIELD_SET:
USING 'VBELN' '销售订单号' '15' 'X' ,
USING 'POSNR' '行号' '15' '' ,
USING 'MATNR' '产品编码' '15' '' ,
USING 'CHARG' '批次' '15' '' ,
USING 'NETWR' '净值' '15' '' ,
USING 'KWMENG' '订单数量' '15' ''.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_OUT
*&---------------------------------------------------------------------*
* 输出数据
*----------------------------------------------------------------------*
FORM FRM_DATA_OUT.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = G_LAYOUT
IT_FIELDCAT = GT_FIELDCAT
I_SAVE = 'X'
I_CALLBACK_PF_STATUS_SET = 'FRM_STATE'
I_CALLBACK_USER_COMMAND = 'FRM_USER_COMMAND'
TABLES
T_OUTTAB = GT_ALV
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_SET_PF_STATUS
*&---------------------------------------------------------------------*
FORM FRM_STATE USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'STANDARD'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
READ TABLE GT_ALV INTO WA_ALV INDEX RS_SELFIELD-TABINDEX.
CHECK SY-SUBRC = 0.
CASE R_UCOMM.
WHEN '&IC1'.
SET PARAMETER ID 'AUN' FIELD WA_ALV-VBELN.
CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
WHEN 'PRT'.
PERFORM FRM_PRINT.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FIELD_SET
*&---------------------------------------------------------------------*
FORM FIELD_SET USING P_FIELDNAME
P_SELTEXT_L
P_OUTPUTLEN
P_HOTSPOT.
GW_FIELDCAT-FIELDNAME = P_FIELDNAME .
GW_FIELDCAT-SELTEXT_L = P_SELTEXT_L .
GW_FIELDCAT-OUTPUTLEN = P_OUTPUTLEN .
GW_FIELDCAT-HOTSPOT = P_HOTSPOT.
APPEND GW_FIELDCAT TO GT_FIELDCAT.
CLEAR GW_FIELDCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT
VBAP~VBELN
VBAP~POSNR
VBAP~KWMENG
VBAP~MATNR
VBAP~NETWR
VBAP~CHARG
INTO CORRESPONDING FIELDS OF TABLE GT_ALV
FROM VBAP
WHERE VBAP~VBELN IN S_VBELN.
"整理数据
SORT: GT_ALV BY VBELN POSNR.
ENDFORM.
FORM FRM_PRINT.
DATA:FM_NAME TYPE RS38L_FNAM.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
FORMNAME = 'ZPPU_TEST'
IMPORTING
FM_NAME = FM_NAME
EXCEPTIONS
NO_FORM = 1
NO_FUNCTION_MODULE = 2
OTHERS = 3.
CALL FUNCTION FM_NAME
EXPORTING
USER_SETTINGS = 'X'
TABLES
IT_DATA = GT_ALV
EXCEPTIONS
FORMATTING_ERROR = 1
INTERNAL_ERROR = 2
SEND_ERROR = 3
USER_CANCELED = 4.
ENDFORM.
标签:SY,LAYOUT,FIELDCAT,demo,FRM,ALV,SAP,TYPE 来源: https://blog.csdn.net/anji0207/article/details/95960668