本文主要是介绍ABAP程序示例:交互式ALV演示程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个程序是一个用于显示和刷新事务代码(TCode)列表的报表程序。它使用ALV(ABAP List Viewer)来显示数据,并提供了一个刷新功能,以便用户可以查看新的事务代码。以下是程序的主要部分及其功能:
-
数据类型和内部表声明:定义了用于ALV显示的数据类型和内部表。
-
数据获取子程序:从数据库的tstc表中获取数据,并根据条件筛选出屏幕编号不为'0000'的事务代码。
-
ALV输出子程序:构建ALV显示所需的字段目录、布局,并调用REUSE_ALV_GRID_DISPLAY函数来显示数据。
-
事件退出子程序:设置ALV的事件退出处理,以便在用户执行特定操作(如刷新或点击事务代码)时执行相应的处理。
-
字段目录构建子程序:定义了ALV显示中每个字段的属性,如字段名、表名和选择文本。
-
布局构建子程序:设置了ALV显示的布局属性,如是否启用斑马线效果和列宽优化。
-
ALV显示子程序:调用ALV显示函数,设置回调程序、字段目录、布局和事件退出处理,并指定输出表。
-
用户命令处理子程序:处理用户在ALV中执行的命令,如点击事务代码或刷新列表。
-
设置PF状态子程序:设置ALV的PF(Program Function)状态,这里设置为标准全屏模式。
请注意,这个程序是一个完整的报表程序,它包含了初始化、数据获取、ALV显示以及用户交互处理的所有必要部分。在实际使用中,可根据具体需求调整这些子程序的逻辑。
REPORT zalv_tcoderefresh. " 报告名称*type pools for alv declarations
TYPE-POOLS: slis. " 引入ALV类型池*structure declaration for tstc table
TYPES: BEGIN OF ty_tstc, " 定义TSTC表的结构tcode TYPE tcode, " 事务代码pgmna TYPE program_id, " 程序名称dypno TYPE dynpronr, " 屏幕编号END OF ty_tstc. " 结束结构定义* Internal table and workarea declarations for tstc
DATA: it_tstc TYPE STANDARD TABLE OF ty_tstc, " 内部表声明wa_tstc TYPE ty_tstc. " 工作区声明*data declarations for ALV
DATA: it_layout TYPE slis_layout_alv, " ALV布局数据类型wa_fieldcat TYPE slis_fieldcat_alv, " 字段目录数据类型it_fieldcat TYPE slis_t_fieldcat_alv, " 字段目录表类型it_eventexit TYPE slis_t_event_exit, " 事件退出数据类型wa_eventexit TYPE slis_event_exit. " 事件退出工作区类型*initialisation event
INITIALIZATION. " 初始化事件
*start of selection event
START-OF-SELECTION. " 开始选择事件*subroutine to fetch data from the db table
PERFORM fetch_data. " 执行数据获取子程序*subroutine for output display
PERFORM alv_output. " 执行ALV输出子程序*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
* *subroutine to fetch data from the db table
*----------------------------------------------------------------------*
FORM fetch_data.
*Internal table and work area declaratin for TSTC (local tables)
DATA : lt_tstc TYPE STANDARD TABLE OF ty_tstc, " 内部表声明ls_tstc TYPE ty_tstc. " 工作区声明*Static field definition
*Reads the last tcode and stores it in l_tstc that on refresh further data
*beyond this value is fetched
STATICS l_tstc TYPE tcode. " 静态字段声明* Selection from the tstc table
*we select till 25 rows and on further refresh next 25 are selected
*we select transactions having screen numbers only
SELECT tcodepgmnadypnoFROM tstcINTO CORRESPONDING FIELDS OF TABLE lt_tstcUP TO 25 ROWSWHERE tcode GT l_tstc " 选择大于l_tstc的tcodeAND tcode LIKE 'M%' " 选择以'M'开头的tcodeAND dypno NE '0000'. " 排除屏幕编号为'0000'的记录* Code for transferring the values of local table to output table
* for 25 rows as sy-tfill is 25.
*In case there are no records a message pops up.
IF sy-subrc EQ 0. " 如果没有错误DESCRIBE TABLE it_tstc. " 获取内部表的描述READ TABLE lt_tstc INTO ls_tstc INDEX sy-tfill. " 读取最后一行数据l_tstc = ls_tstc-tcode. " 更新l_tstc为最后一行的tcodeit_tstc[] = lt_tstc[]. " 将数据赋值给输出表
ELSE. " 如果有错误MESSAGE 'No Records found ' TYPE 'i'. " 显示错误消息
ENDIF.
ENDFORM. " 读取数据子程序结束*&---------------------------------------------------------------------*
*& Form alv_output
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM alv_output.
*subroutine to refresh alv
PERFORM event_exits. " 执行事件退出子程序
*field catalogue
PERFORM build_fieldcat. " 执行字段目录子程序
*Layout for alv
PERFORM build_layout. " 执行布局子程序
*output display
PERFORM alv_display. " 执行ALV显示子程序
ENDFORM. " ALV输出子程序结束*&---------------------------------------------------------------------*
*& Form event_exits
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*subroutine to refresh alv
FORM event_exits.
CLEAR wa_eventexit. " 清除事件退出工作区
wa_eventexit-ucomm = '&REFRESH'. " 设置刷新命令
wa_eventexit-after = 'X'. " 设置事件退出条件
APPEND wa_eventexit TO it_eventexit. " 将事件退出添加到列表
ENDFORM. " 事件退出子程序结束*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*Field catalogue
FORM build_fieldcat.
CLEAR wa_fieldcat. " 清除字段目录工作区
wa_fieldcat-row_pos = '1'. " 设置行位置
wa_fieldcat-col_pos = '1'. " 设置列位置
wa_fieldcat-fieldname = 'TCODE'. " 设置字段名
wa_fieldcat-tabname = 'it_tstc'. " 设置表名
wa_fieldcat-seltext_m = 'TRANSACTION'. " 设置选择文本
APPEND wa_fieldcat TO it_fieldcat. " 将字段目录添加到列表CLEAR wa_fieldcat. " 清除字段目录工作区
wa_fieldcat-row_pos = '1'. " 设置行位置
wa_fieldcat-col_pos = '2'. " 设置列位置
wa_fieldcat-fieldname = 'PGMNA'. " 设置字段名
wa_fieldcat-tabname = 'it_tstc'. " 设置表名
wa_fieldcat-seltext_m = 'PROGRAM'. " 设置选择文本
APPEND wa_fieldcat TO it_fieldcat. " 将字段目录添加到列表CLEAR wa_fieldcat. " 清除字段目录工作区
wa_fieldcat-row_pos = '1'. " 设置行位置
wa_fieldcat-col_pos = '3'. " 设置列位置
wa_fieldcat-fieldname = 'DYPNO'. " 设置字段名
wa_fieldcat-tabname = 'it_tstc'. " 设置表名
wa_fieldcat-seltext_m = 'SCREEN'. " 设置选择文本
APPEND wa_fieldcat TO it_fieldcat. " 将字段目录添加到列表
ENDFORM. " 构建字段目录子程序结束*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*Layout
FORM build_layout.
it_layout-zebra = 'X'. " 设置斑马线效果
it_layout-colwidth_optimize = 'X'. " 设置列宽优化
ENDFORM. " 构建布局子程序结束*&---------------------------------------------------------------------*
*& Form alv_display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*ALV output
FORM alv_display.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'EXPORTINGi_callback_program = sy-repid " 当前程序IDi_callback_user_command = 'USER_COMMAND' " 用户命令回调i_callback_pf_status_set = 'PFSTATUS' " 状态设置回调it_fieldcat = it_fieldcat " 字段目录is_layout = it_layout " 布局it_event_exit = it_eventexit " 事件退出i_screen_start_column = 10 " 屏幕开始列i_screen_start_line = 20 " 屏幕开始行i_screen_end_column = 70 " 屏幕结束列i_screen_end_line = 45 " 屏幕结束行i_grid_title = 'Call Tcode Refresh ALV' " 网格标题TABLESt_outtab = it_tstc. " 输出表
ENDFORM. " ALV显示子程序结束*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*User actions on ALV
FORM user_command USING r_ucomm TYPE sy-ucomm " 用户命令rs_selfield TYPE slis_selfield.CASE r_ucomm. " 根据用户命令进行操作
*User clicks a transaction code and that tcode is called from ALVWHEN '&IC1'.READ TABLE it_tstc INDEX rs_selfield-tabindex INTO wa_tstc. " 读取用户点击的行数据IF sy-subrc = 0. " 如果读取成功CALL TRANSACTION wa_tstc-tcode. " 调用事务代码ENDIF.
*user clicks the refresh button and the next 25 records are displayedWHEN '&REFRESH'.PERFORM fetch_data. " 执行数据获取子程序rs_selfield-refresh = 'X'. " 设置刷新标志rs_selfield-col_stable = 'X'. " 设置列稳定标志rs_selfield-row_stable = 'X' ." 设置行稳定标志ENDCASE.
ENDFORM. " 用户命令处理子程序结束*&---------------------------------------------------------------------*
*& Form pfstatus
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*Form for settings the pf status to the alv
FORM pfstatus USING ut_extab TYPE slis_t_extab.SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'. " 设置PF状态为标准全屏
ENDFORM. " 设置PF状态子程序结束
这篇关于ABAP程序示例:交互式ALV演示程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!