ALV相关概念
ALV是系统的一种网格的显示方式,这种方式带有汇总\排序\筛选等功能,ALV格式的数据是以单元格为单位显示,不象一般的写屏方式拷出来或是
导出成文件不同列的内容粘在一块,这种方式便于数据导出来放在电子表格里进行加工。
ALV即能显示简单表单(SIMPLE LIST)又能显示有序表单(SEQUENTIALLIST):
ALV实现方式:
ALV 可以通过两种方式实现:Two Approaches
Conventional (Using SAP Standard Function Modules).
Object Oriented (Using SAP Standard Classes and Methods).
以下我们要讲的为Function Modules方式。
ALV实现流程:
第一步:定义将要用到的表,即TALBES定义部分,然后定义TYPE-POOLS: SLIS.
第二步:定义数据类型或者内表的实体对象.
第三步:定义一些需要用到的变量.
第四步:定义自己的选择屏幕.
第五步:start-of-selection部分.
1)用一个子函数完成对ALV表单标题区域的赋值(i_list_comments).
2)用一个子函数完成自己所需要数据的抓取.
3)用一个子函数完成要显示列表的列名行(第一行)的相关赋值(i_fieldcat_alv)以及设置.
4)用一个子函数完成输出格式的设置(i_layout),比如双击一条记录是否弹出对话框啊?是用哪个功能键触发等等.
5)用一个子函数FORM DISPLAY_DATA来显示上面我们已经分别封装好的数据,需要调用两个常用的FUNCTION MODULE.
下面根据一个普通的例子来具体解释各步骤:
*&---------------------------------------------------------------------*
*& Report ZHYALVREPORTDEMO2
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZHYALVREPORTDEMO2.
*第一步:添加TYPE-POOLS和所需要的TABLES
TYPE-POOLS:SLIS.
TABLES:ZHYUSERMAIN,ZHYUSERLINE.
*第二步:定义ALV报表的内表数据类型和内表,用来作为ALV报表的DATASOURCE
TYPES:
BEGIN
OF T_OUT,
ID
LIKE ZHYUSERMAIN-
ID,
NAME
LIKE ZHYUSERMAIN-NAME,
AGE
LIKE ZHYUSERMAIN-AGE,
ADDR
LIKE ZHYUSERLINE-ADDR,
SCHOOL
LIKE ZHYUSERLINE-SCHOOL,
END
OF T_OUT.
DATA: IT_OUT
TYPE
STANDARD
TABLE
OF T_OUT
WITH
HEADER
LINE,
ST_OUT
LIKE
LINE
OF IT_OUT.
*第三步:定义ALV报表的相关的参数
DATA:ST_LAYOUT
TYPE SLIS_LAYOUT_ALV,
"ALV报表的LAYOUT设置
IT_EVENT
TYPE SLIS_T_EVENT,
"ALV的事件内表
ST_EVENT
LIKE
LINE
OF IT_EVENT,
"ALV的单个事件对象
* ST_EVENT TYPE SLIS_ALV_EVENT,
IT_COMMENT
TYPE SLIS_T_LISTHEADER,
"存储表头信息的内表
ST_COMMENT
LIKE
LINE
OF IT_COMMENT,
"存储单个表头信息
IT_FIELDCAT
TYPE SLIS_T_FIELDCAT_ALV,
"存储ALV报表的COLUMNS信息的内表
ST_FIELDCAT
LIKE
LINE
OF IT_FIELDCAT,
"单个COLUMN信息
* ST_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
IT_SORT
TYPE SLIS_T_SORTINFO_ALV,
"保存排序列的信息
ST_SORT
LIKE
LINE
OF IT_SORT.
"单个SORT信息
* ST_SORT TYPE SLIS_SORTINFO_ALV.
**1)SLIS_LAYOUT_ALV是一个类型,它用来定义ALV报表的整体属性例如:
* *ST_LAYO-ZEBRA = 'X'.“显示成斑马纹样式
* *ST_LAYO-DETAIL_POPUP = 'X'.“是否弹出详细信息窗口
* *ST_LAYO-F2CODE = '&ETA'. “设置触发弹出详细信息窗口的功能码,这里是双击
* *ST_LAYO-COLWIDTH_OPTIMIZE = 'X'. “优化列宽选项是否设置
* *ST_LAYO-DETAIL_INITIAL_LINES = 'X'.
* *ST_LAYO -no_vline = 'X'.“这个用来设置列间隔线
* *ST_LAYO -detail_titlebar = '详细内容'. “设置弹出窗口的标题栏
**2)SLIS_T_EVENT是一个取得事件的内表,通过函数REUSE_ALV_EVENTS_GET取得多个事件(包括操作页眉页脚的事件).
**3)SLIS_T_LISTHEADER操作页眉页脚的内表.
**4)SLIS_T_FIELDCAT_ALV操作ALV报表列的内表.
**5)SLIS_T_SORTINFO_ALV对显示数据排序的内表。
*第四步:定义自己的选择屏幕的查询条件包括PARAMETERS和SELECT-OPTIONS
SELECT-OPTIONS:S_ID
FOR ZHYUSERMAIN-
ID.
*在选择屏幕处做一些输入的条件的验证。
AT SELECTION-
SCREEN.
PERFORM FRM_CHECK_SDATA.
"调用验证子例程FRM_CHECK_SDATA
* 第五步:在START-OF-SELECTION部分调用以下子例程设置ALV报表所要使用到的相关参数
*即对第三步定义的一些ALV参数进行设置
*1:FRM_GET_DATA获得数据赋值给ALV数据源内表IT_OUT
*2:FRM_SET_LAYOUT设置ALV报表样式
*3:FRM_SET_EVENT设置ALV报表的事件
*4:FRM_SET_FIELDCAT设置ALV的列属性。
*5:FRM_SET_SORT设置ALV的排序属性。
*6:FRM_DISPLAY_ALV显示ALV报表。
START-
OF-SELECTION.
PERFORM FRM_GET_DATA.
PERFORM FRM_SET_LAYOUT.
PERFORM FRM_SET_EVENT.
PERFORM FRM_SET_FIELDCAT.
PERFORM FRM_SET_SORT.
PERFORM FRM_DISPLAY_ALV.
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_SDATA
*&---------------------------------------------------------------------*
* 判断录入参数的合法性
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_CHECK_SDATA .
*做一些判断
IF S_ID-HIGH =
3.
MESSAGE
'用户ID不能等于3'
TYPE
'E' .
LEAVE
TO LIST-PROCESSING.
ENDIF.
ENDFORM.
" FRM_CHECK_SDATA
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* 取数据到内表中
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT
ZHYUSERMAIN~
ID
ZHYUSERMAIN~NAME
ZHYUSERMAIN~AGE
ZHYUSERLINE~ADDR
ZHYUSERLINE~SCHOOL
INTO CORRESPONDING
FIELDS
OF
TABLE IT_OUT
FROM ZHYUSERMAIN
INNER
JOIN ZHYUSERLINE
ON ZHYUSERLINE~MAINID = ZHYUSERMAIN~
ID.
IF SY-SUBRC <>
0 .
MESSAGE
'读取数据进内表失败'
TYPE
'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
" FRM_GET_DATA
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
CLEAR ST_LAYOUT.
ST_LAYOUT-ZEBRA =
'X'.
ST_LAYOUT-DETAIL_POPUP =
'X'.
ST_LAYOUT-F2CODE =
'&ETA'.
ST_LAYOUT-COLWIDTH_OPTIMIZE =
'X'.
ST_LAYOUT-DETAIL_INITIAL_LINES =
'X'.
ENDFORM.
" FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& Form FRM_SET_EVENT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_EVENT .
CALL
FUNCTION
'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE =
0
IMPORTING
ET_EVENTS = IT_EVENT
EXCEPTIONS
LIST_TYPE_WRONG =
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.
LEAVE
TO LIST-PROCESSING.
ENDIF.
READ
TABLE IT_EVENT
WITH
KEY NAME = SLIS_EV_END_OF_LIST
INTO ST_EVENT.
IF SY-SUBRC =
0.
ST_EVENT-
FORM =
'ALV_END_OF_PAGE'.
"将页尾子程名称赋值给 ST_EVENT-FORM
MODIFY IT_EVENT
FROM ST_EVENT
INDEX SY-TABIX.
ENDIF.
READ
TABLE IT_EVENT
WITH
KEY NAME = SLIS_EV_TOP_OF_PAGE
INTO ST_EVENT.
IF SY-SUBRC =
0 .
ST_EVENT-
FORM =
'ALV_TOP_OF_PAGE'.
MODIFY IT_EVENT
FROM ST_EVENT
INDEX SY-TABIX.
ENDIF.
ENDFORM.
" FRM_SET_EVENT
*页眉处理子例程
FORM ALV_TOP_OF_PAGE.
DATA:CONTXT
TYPE SLIS_LISTHEADER-INFO.
DATA:L,H
TYPE STRING.
MOVE S_ID-LOW
TO L .
MOVE S_ID-HIGH
TO H.
CONCATENATE L
'~' H
INTO CONTXT.
REFRESH IT_COMMENT.
PERFORM INSERT_IT_COMMENT
USING:
'H' SPACE
'用户信息表',
'S'
'ID' CONTXT.
CALL
FUNCTION
'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = IT_COMMENT
I_LOGO =
'ENJOYSAP_LOGO'.
IF SY-SUBRC <>
0.
MESSAGE
ID SY-MSGID
TYPE SY-MSGTY
NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
LEAVE
TO LIST-PROCESSING.
ENDIF.
ENDFORM.
"ALV_TOP_OF_PAGE
*页脚处理子例程
FORM ALV_END_OF_PAGE.
DATA:W_DATA
TYPE SLIS_LISTHEADER-INFO.
DATA:W_UNAME
TYPE SLIS_LISTHEADER-INFO.
DATA:Z_DATA
TYPE STRING.
CONCATENATE
SY-DATUM+
0(
4)
SY-DATUM+
4(
2)
SY-DATUM+
6(
2)
INTO W_DATA
SEPARATED
BY
'/'.
MOVE SY-UNAME
TO W_UNAME.
REFRESH IT_COMMENT.
PERFORM INSERT_IT_COMMENT
USING:
'S'
'用户名:' W_UNAME,
'S'
'时间:' W_DATA.
CALL
FUNCTION
'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = IT_COMMENT
I_END_OF_LIST_GRID =
'X'.
ENDFORM.
"ALV_END_OF_PAGE
*&---------------------------------------------------------------------*
*& Form INSERT_IT_COMMENT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->TYP text
* -->KEY text
* -->INFO text
*----------------------------------------------------------------------*
FORM INSERT_IT_COMMENT
USING TYP
TYPE SLIS_LISTHEADER-TYP
KEY
TYPE SLIS_LISTHEADER-
KEY
INFO
TYPE SLIS_LISTHEADER-INFO.
CLEAR ST_COMMENT.
ST_COMMENT-TYP = TYP.
"H=Header, S=Selection, A=Action供选择
ST_COMMENT-
KEY =
KEY.
ST_COMMENT-INFO = INFO.
APPEND ST_COMMENT
TO IT_COMMENT.
ENDFORM.
"INSERT_IT_COMMENT
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
* 设置ALV报表列属性
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .
REFRESH IT_FIELDCAT.
PERFORM INSERT_IT_FIELDCAT
USING:
0
'ID'
'ZHYUSERMAIN-ID'
'ID' SPACE,
1
'NAME'
'ZHYUSERMAIN-NAME'
'姓名' SPACE,
2
'AGE'
'ZHYUSERMAIN-AGE'
'年龄' SPACE,
3
'ADDR'
'ZHYUSERMAIN-ADDR'
'地址' SPACE,
4
'SCHOOL'
'ZHYUSERMAIN-SCHOOL'
'学校' SPACE.
ENDFORM.
" FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form INSERT_IT_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->COL_POS 列序
* -->FIELDNAME 绑定的内表列名
* -->REF_FIELDNAME 录入时,帮助制作时指定表中参考列名称
* -->SELTEXT_M 字段描述
* -->EDIT 是否可编辑
*----------------------------------------------------------------------*
FORM INSERT_IT_FIELDCAT
USING
COL_POS
TYPE SLIS_FIELDCAT_ALV-COL_POS
FIELDNAME
TYPE SLIS_FIELDCAT_ALV-FIELDNAME
REF_FIELDNAME
TYPE SLIS_FIELDCAT_ALV-REF_FIELDNAME
SELTEXT_M
TYPE SLIS_FIELDCAT_ALV-SELTEXT_M
EDIT
TYPE SLIS_FIELDCAT_ALV-
EDIT.
CLEAR ST_FIELDCAT.
ST_FIELDCAT-COL_POS = COL_POS.
ST_FIELDCAT-FIELDNAME = FIELDNAME.
ST_FIELDCAT-REF_FIELDNAME = REF_FIELDNAME.
ST_FIELDCAT-SELTEXT_M = SELTEXT_M.
ST_FIELDCAT-
EDIT =
EDIT.
APPEND ST_FIELDCAT
TO IT_FIELDCAT.
ENDFORM.
"INSERT_IT_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_SET_SORT
*&---------------------------------------------------------------------*
* 显示前页表数据的排序
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_SORT .
REFRESH IT_SORT.
PERFORM INSERT_IT_SORT
USING:
2
'AGE'
'X',
1
'ID'
'X'.
ENDFORM.
" FRM_SET_SORT
FORM INSERT_IT_SORT
USING
SPOS
TYPE SLIS_SORTINFO_ALV-SPOS
FIELDNAME
TYPE SLIS_SORTINFO_ALV-FIELDNAME
UP
TYPE SLIS_SORTINFO_ALV-
UP.
CLEAR ST_SORT.
ST_SORT-SPOS = SPOS.
ST_SORT-FIELDNAME = FIELDNAME.
ST_SORT-
UP =
UP.
APPEND ST_SORT
TO IT_SORT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
* ALV报表的显示
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .
CALL
FUNCTION
'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_INTERFACE_CHECK = SPACE
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = ST_LAYOUT
IT_FIELDCAT = IT_FIELDCAT
IT_SORT = IT_SORT
I_DEFAULT =
'X'
I_SAVE =
'A'
IT_EVENTS = IT_EVENT
TABLES
T_OUTTAB = IT_OUT
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.
" FRM_DISPLAY_ALV