1.定义一个SAP标准类型库
TYPE-POOLS: SLIS.
2.定义一个LAYOUT式样,和一个FIELDCAT字段格式,
DATA: G_F TYPE SLIS_T_FIELDCAT_ALV , "字段目录内表
W_L TYPE SLIS_LAYOUT_ALV. "用于定义ALV表单的相关格式、属性
3.如果选择屏幕复选框参考了表
TABLES : EKKO, EKPO.
4.定义一个结构
TYPES: BEGIN OF G_TY,
BOX TYPE C,
EKORG TYPE EKKO-EKORG,
BSART TYPE EKKO-BSART,
LIFNR TYPE EKKO-LIFNR,
EBELN TYPE EKKO-EBELN,
END OF G_TY.
5.定义表和工作区
DATA: G_TAB TYPE TABLE OF G_TY,
W_TAB LIKE LINE OF G_TAB. 或者
W_TAB TYPEG_TY.
6.定义屏幕
SELECTION-SCREEN BEGIN OF BLOCK XYY WITH FRAME TITLE TEXT-001.
PARAMETERS P_VKORG TYPE VBAK-VKORG OBLIGATORY.
SELECT-OPTIONS: S_VBELN FOR VBAK-VBELN,
S_AUART FOR VBAK-AUART.
SELECTION-SCREEN END OF BLOCK XYY.
这里只定义了单选框(PARAMETERS)和复选框(SELECT-OPTIONS)。一个用TYPE,一个用FOR。表查询会依照屏幕条件筛选数据。
7.初始化屏幕
INITIALIZATION.
加这个条件是告诉系统在这屏幕展示完,要查数据,展示报表了。
8.按顺序执行
START-OF-SELECTION.
PERFORM FRM_SELDATA.
PERFORM FRM_WRITE .
END-OF-SELECTION.
9.数据查询实现PERFORM 方法
FORM FRM_SELDATA .
SELECT:
EKKO~EKORG
EKKO~BSART
EKKO~LIFNR
INTO CORRESPONDING FIELDS OF TABLE G_TAB
FROM EKKO INNER JOIN EKPO ON EKKO~EBELN = EKPO~EBELN
WHERE EKKO~EKORG IN P_EKORG
AND EKKO~BSART IN P_BSART
AND EKKO~LIFNR IN P_LIFNR
AND EKKO~EBELN IN P_EBELN.
ENDFORM. "FRM_SELDATA
查询的时候三个表以下可以用 INNER JOIN
,否则就用内表传递,一个表一个表的查,再传递。
10.实现ALV输出
FORM FRM_WRITE .
PERFORM LAYOUT. "输出样式
PERFORM INIT_FIELDCA. "输出属性
PERFORM LISTADO. "展示
ENDFORM. " FRM_WRITE
(1)实现ALV输出具体的方法:定义ALV样式
FORM LAYOUT .
W_L-BOX_FIELDNAME = 'BOX'.
W_L-ZEBRA = 'X'. "颜色间隔
W_L-COLWIDTH_OPTIMIZE = 'X'. "自适应宽度
ENDFORM. " LAYOUT_19
(2)
FORM INIT_FIELDCA .
CLEAR G_F.
G_F-FIELDNAME = 'EKORG'. "必须为大写
G_F-SELTEXT_S = '采购组织'.
G_F-SELTEXT_M = G_F-SELTEXT_S.
G_F-SELTEXT_L = G_F-SELTEXT_S.
APPEND G_F.
CLEAR G_F.
G_F-FIELDNAME = 'TXZ01'.
G_F-SELTEXT_S = '行文本'.
G_F-SELTEXT_M = G_F-SELTEXT_S.
G_F-SELTEXT_L = G_F-SELTEXT_S.
G_F-EDIT = 'X' .
G_F-REF_FIELDNAME = 'TXZ01'. "参考字段名
G_F-REF_TABNAME = 'EKPO'. "参考表名
APPEND G_F.
ENDFORM. " INIT_FIELDCAT_19
(3)输出
FORM LISTADO .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID "当前程序名
I_CALLBACK_USER_COMMAND = 'USER_COMMAND' "自定义按钮响应事件
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS' "设置ALV的自定义按钮
IS_LAYOUT = W_L "输出样式
IT_FIELDCAT = G_F[] "字段定义描述表
" I_CALLBACK_TOP_OF_PAGE = 'F_TOP_OF_PAGE'
I_SAVE = 'A'
" I_SAVE有4个可选值: I_SAVE= '' --- 不能保存格式;
" I_SAVE = 'X' --- 保存标准格式;
" I_SAVE = 'U' --- 保存特定用户格式;
" I_SAVE = 'A' --- 保存标准格式和特定用户格式。
TABLES
T_OUTTAB = G_TAB "内表输出名称
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. " LISTADO
FORM SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'STATUS' .
ENDFORM.
11.写USER_COMMAND的一个事件(这个作业要求实现更改数据,点击按钮且保存成功)
FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
DATA: GV_GRID TYPE REF TO CL_GUI_ALV_GRID.
"刷新
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = GV_GRID.
CALL METHOD GV_GRID->CHECK_CHANGED_DATA.
CASE R_UCOMM.
WHEN 'SKIP'.
IF SY-SUBRC = 0.
ENDIF.
ENDCASE.
ENDFORM. "LISTADO