接上一篇传统ALV:https://www.cnblogs.com/BruceKing/p/11320165.html。
首先介绍下什么是ALV,在R/3 4.6C之前ALV全称为ABAP List Viewer,在其后的版本中,已经正式更名为SAP List Viewer。ALV是SAP系统中心的列表标准,可以在ABAP程序中进行报表输出。除去传统列表的显示和少量交互功能之外,ALV还提供给系统用户多种其它丰富的交互功能。
使用ALV有两种方式,一种为传统的Call Function,另一种为面向对象的编程,为了有良好的编程习惯和跟随SAP技术的发展,在此,强烈推荐使用面向对象的编程来实现ALV,在此,本文档也是根据此来写的。
下面进行OALV的演示练习
1、使用se80创建程序及建立屏幕8002.
2、绘制容器
点击保存,激活后关闭。
3、创建对象时候的调用类
调用方法
REPORT YJQCS003. *定义变量 DATA:CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER, "存放ALV容器 ALV TYPE REF TO CL_GUI_ALV_GRID, "ALV的网格实例 LT_FIELDCAT TYPE LVC_T_FCAT, "存放字段目录的内表 LS_FIELDCAT TYPE LVC_S_FCAT, WA_LAYOUT TYPE LVC_S_LAYO. "布局结构 DATA:OK_CODE TYPE SY-UCOMM."元素清单里的一致 *定义内表结构 TYPES:BEGIN OF TY_ALVSHOW, VBELN LIKE VBAK-VBELN, ERDAT LIKE VBAK-ERDAT, ERNAM LIKE VBAK-ERNAM, KUNNR LIKE VBAK-KUNNR, POSNR LIKE VBAP-POSNR, MATNR LIKE VBAP-MATNR, MATKL LIKE VBAP-MATKL, ZMENG LIKE VBAP-ZMENG, ZIEME LIKE VBAP-ZIEME, WERKS LIKE VBAP-WERKS, LGORT LIKE VBAP-LGORT, END OF TY_ALVSHOW. *定义内表及工作区 DATA:IT_ALVSHOW TYPE STANDARD TABLE OF TY_ALVSHOW. DATA:WA_ALVSHOW TYPE TY_ALVSHOW. CALL SCREEN 8002."在调用module前调用屏幕,很重要 MODULE DISPLAY_ALV OUTPUT. PERFORM DISPLAY_ALV. "封装显示数据函数 ENDMODULE. FORM DISPLAY_ALV. "创建容器对象 IF ALV IS INITIAL. CREATE OBJECT CONTAINER "创建容器对象 EXPORTING CONTAINER_NAME = 'CONTAINER'. IF CONTAINER IS NOT INITIAL. CREATE OBJECT ALV "创建ALV的网格实例 EXPORTING I_PARENT = CONTAINER. ENDIF. PERFORM SET_FIELDCAT ."封装定义显示字段 PERFORM SET_LAYOUT ."封装定义布局 PERFORM GET_DATA."封装取数 CALL METHOD ALV->SET_TABLE_FOR_FIRST_DISPLAY "调用显示ALV的方法 EXPORTING I_SAVE = 'A' IS_LAYOUT = WA_LAYOUT CHANGING IT_OUTTAB = IT_ALVSHOW IT_FIELDCATALOG = LT_FIELDCAT EXCEPTIONS INVALID_PARAMETER_COMBINATION = 1 PROGRAM_ERROR = 2 TOO_MANY_LINES = 3 OTHERS = 4. IF SY-SUBRC <> 0. ENDIF. ELSE. CALL METHOD ALV->REFRESH_TABLE_DISPLAY "如果有容器的话,调用刷新ALV的方法 EXCEPTIONS FINISHED = 1 OTHERS = 2. IF SY-SUBRC <> 0. ENDIF. ENDIF. ENDFORM. MODULE STATUS_8002 OUTPUT. SET PF-STATUS 'STANDARD'. "这里使用SE41复制一个菜单 * SET TITLEBAR 'xxx'. ENDMODULE. MODULE USER_COMMAND_8002 INPUT. "调用屏幕事件 CLEAR:OK_CODE. CASE OK_CODE. WHEN '&ADD'. LEAVE SCREEN. WHEN OTHERS. ENDCASE. ENDMODULE. FORM SET_FIELDCAT . "定义显示字段 DATA:COL_POS TYPE I. * fieldcat COL_POS = 0. *销售凭证列 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'VBELN'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-KEY = 'X'. LS_FIELDCAT-SCRTEXT_M = '销售凭证'. "这个字段和常规的不一样,常规的为SELTEXT_M APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *创建日期 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'ERDAT'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-REF_TABLE = 'VBAK'."这个字段和常规的不一样,常规的为REF_TABNAME APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *创建者 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'ERNAM'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '创建人'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *售达方 自己起字段描述 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'KUNNR'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '客户'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *售达方1 参考数据字典表名 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'KUNNR'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-REF_TABLE = 'VBAK'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *行号 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'POSNR'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-REF_TABLE = 'VBAP'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *物料 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'MATNR'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '物料编码'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *物料组 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'MATKL'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '物料组'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *数量 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'ZMENG'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '数量'. LS_FIELDCAT-DO_SUM = 'X'. "总计 LS_FIELDCAT-EDIT = 'X'. "总计 LS_FIELDCAT-DECIMALS = 3. "因为当编辑数量的时候,如果是小数,要指定小数位,否则输入数据之后回车会自动缩小,如果使用ref则不需要设置 APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *单位 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'ZIEME'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '单位'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *工厂 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'WERKS'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-REF_TABLE = 'VBAP'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. *库位 COL_POS = COL_POS + 1. LS_FIELDCAT-FIELDNAME = 'LGORT'. LS_FIELDCAT-COL_POS = COL_POS. LS_FIELDCAT-SCRTEXT_M = '库位'. APPEND LS_FIELDCAT TO LT_FIELDCAT. CLEAR:LS_FIELDCAT. ENDFORM. FORM SET_LAYOUT . "定义页面布局 WA_LAYOUT-CWIDTH_OPT = 'X'. WA_LAYOUT-ZEBRA = 'X'. WA_LAYOUT-GRID_TITLE = '销售订单查询报表'. "小标题 WA_LAYOUT-SMALLTITLE = 'X'. ENDFORM. FORM GET_DATA."取数 SELECT VBAK~VBELN VBAK~ERDAT VBAK~ERNAM VBAK~KUNNR VBAP~POSNR VBAP~MATNR VBAP~MATKL VBAP~ZMENG VBAP~ZIEME VBAP~WERKS VBAP~LGORT FROM VBAK INNER JOIN VBAP ON VBAK~VBELN = VBAP~VBELN INTO CORRESPONDING FIELDS OF TABLE IT_ALVSHOW UP TO 50 ROWS. ENDFORM.
显示效果如下