既然面试问到了,那么就正好复习一下,
这里的EVENT有两种,先说我比较熟悉的那种,就是BAPI 'REUSE_ALV_GRID_DISPLAY_LVC’里的 it_events 和 it_event_exit
以下为代码示例
data:lt_event_exit type slis_t_event_exit,
ls_event_exit type slis_event_exit.
ls_event_exit-ucomm = '&SAL'.
ls_event_exit-after = 'X'.
append ls_event_exit to lt_event_exit.
ls_event_exit-ucomm = '&ALL'.
ls_event_exit-after = 'X'.
append ls_event_exit to lt_event_exit.
perform frm_build_events changing it_events.
call function 'REUSE_ALV_GRID_DISPLAY_LVC'
exporting
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
is_layout_lvc = gt_layout
it_fieldcat_lvc = gt_fieldcat
i_save = 'X'
it_events = it_events[]
it_event_exit = lt_event_exit
tables
t_outtab = gt_out
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
//Implement suitable error handling here
endif.
下面是网上手机的一些关于ALV的 IT_EVENT事件的一些说明
原文链接:https://blog.csdn.net/Leo520liang/article/details/138300597
1.事件类型,
ALV框架提供了多种标准事件,例如 ,TOP_OF_PAGE(页头信息)、END_OF_PAGE(页尾信息)、USER_COMMAND(用户按钮)这些事件允许开发者在特定的时刻执行自定义的逻辑代码,例如当用户执行某个命令时或者页面状态发生变化时。
2.注册事件
可以通过向内表it_events 添加事件来注册想要的事件,每个事件由一个数据类型为slis_alv_event的结构体表示,它包含了事件的名称和处理该事件的表单(Form)的名称
3.事件处理
一旦事件发生,系统会调用阈值关联的表单。表单是一个ABAP子程序。它接收特定的参数,允许开发者在其中编写处理事件的代码。
4.使用场景
通过使用事件,可以实现对ALV显示的精细控制,如根据用户的选择改变屏幕元素的状态,或者在用户执行某些操作时触发特定的业务逻辑。
DATA: gt_events TYPE slis_t_event. "ALV 事件
…
*屏幕开始事件
START-OF-SELECTION.
…
PERFORM f_get_event.
PERFORM f_alvshow.
…
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_events = gt_events
…
FORM f_get_event.
DATA ls_formname TYPE slis_formname VALUE 'F_TOP_OF_PAGE'.
DATA ls_events TYPE LINE OF slis_t_event.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 0
IMPORTING
et_events = gt_events
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.
ENDIF.
* 修改TOP_OF_PAGE以加入表头,设置处理子程序为'F_TOP_OF_PAGE'
CLEAR gv_index.
CLEAR ls_events.
READ TABLE gt_events INTO ls_events WITH KEY name = slis_ev_top_of_page.
IF sy-subrc = 0.
gv_index = sy-tabix.
MOVE ls_formname TO ls_events-form.
MODIFY gt_events FROM ls_events INDEX gv_index.
ELSE.
ls_events-form = ls_formname.
ls_events-name = slis_ev_top_of_page.
APPEND ls_events TO gt_events.
ENDIF.
* 也可以直接添加
ls_events-name = 'CALLER_EXIT'. "自定义事件
ls_events-form = 'F_BUTTON'.
APPEND ls_events TO gt_events.
ENDFORM.
FORM f_button USING e_grid TYPE slis_data_caller_exit.
…
ENDFORM.
FORM f_top_of_page.
…
ENDFORM.
此处可以设置页头信息文字。
同时将自己的代码和网上的代码结合,得到
*&---------------------------------------------------------------------*
*& Report ZITEVENT_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZITEVENT_TEST.
TYPE-POOLS:slis.
DATA:gt_fieldcat TYPE slis_t_fieldcat_alv,
gs_fieldcat TYPE slis_fieldcat_alv,
gs_layout TYPE slis_layout_alv,
gv_repid TYPE sy-repid.
DATA:gt_spfli LIKE STANDARD TABLE OF spfli WITH HEADER LINE.
* 全局变量
DATA: gv_index TYPE sy-tabix.
DATA: gt_events TYPE slis_t_event. "ALV 事件
INITIALIZATION.
gv_repid = sy-repid.
*屏幕开始事件
START-OF-SELECTION.
PERFORM f_getdata.
PERFORM f_catalog.
PERFORM f_get_event.
PERFORM f_alvshow.
FORM f_getdata.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_spfli.
ENDFORM.
FORM f_catalog.
*fieldcat属性
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = gv_repid
i_structure_name = 'SPFLI'
CHANGING
ct_fieldcat = gt_fieldcat "导出gt_fieldcat内表数据
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CLEAR gs_fieldcat.
*"对gt_fieldcat属性的修改
READ TABLE gt_fieldcat INTO gs_fieldcat INDEX 2. "将第二列设为热点显示
gs_fieldcat-hotspot = 'X'.
MODIFY gt_fieldcat FROM gs_fieldcat INDEX 2.
*layout属性
gs_layout-zebra = 'X'."斑马线显示
gs_layout-colwidth_optimize = 'X'."列宽度最优化显示
ENDFORM.
FORM f_alvshow.
data:lt_event_exit type slis_t_event_exit,
ls_event_exit type slis_event_exit.
ls_event_exit-ucomm = '&SAL'.
ls_event_exit-after = 'X'.
append ls_event_exit to lt_event_exit.
ls_event_exit-ucomm = '&ALL'.
ls_event_exit-after = 'X'.
append ls_event_exit to lt_event_exit.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = gv_repid
i_grid_title = 'ALV REPORT TEST' "ALV显示标题
is_layout = gs_layout
it_fieldcat = gt_fieldcat
it_events = gt_events
it_event_exit = lt_event_exit
TABLES
t_outtab = gt_spfli
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 f_get_event.
DATA ls_formname TYPE slis_formname VALUE 'F_TOP_OF_PAGE'.
DATA ls_events TYPE LINE OF slis_t_event.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 0
IMPORTING
et_events = gt_events
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.
ENDIF.
* 修改TOP_OF_PAGE以加入表头,设置处理子程序为'F_TOP_OF_PAGE'
CLEAR gv_index.
CLEAR ls_events.
READ TABLE gt_events INTO ls_events WITH KEY name = slis_ev_top_of_page.
IF sy-subrc = 0.
gv_index = sy-tabix.
MOVE ls_formname TO ls_events-form.
MODIFY gt_events FROM ls_events INDEX gv_index.
ELSE.
ls_events-form = ls_formname.
ls_events-name = slis_ev_top_of_page.
APPEND ls_events TO gt_events.
ENDIF.
ls_events-name = 'CALLER_EXIT'. "自定义事件
ls_events-form = 'F_BUTTON'.
APPEND ls_events TO gt_events.
ENDFORM.
FORM f_button USING e_grid TYPE slis_data_caller_exit.
MESSAGE '事件触发' TYPE 'S'.
ENDFORM.
FORM f_top_of_page.
DATA: i_header TYPE slis_t_listheader,
wa_header TYPE slis_listheader.
* 位于标题第一行
wa_header-typ = 'H'.
wa_header-info = 'ALV DEMO' .
APPEND wa_header TO i_header .
CLEAR wa_header .
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = i_header
i_alv_form = 'X'.
ENDFORM.