导语:前面有做过一些F4事件,没有分享,最近在做EPIC的收款认领平台,顾问提出了一个有意思的需求,在填写凭证的时候,根据记账码(BSCHL)的不同,搜索帮助来展示【总账科目】或者是【客户编码】,其实很简单,就在F4事件上稍加变通就可以了。
一、定义内表及实例类等
1、定义F4内表
DATA : gt_f4 TYPE lvc_t_f4, "f4
gs_f4 TYPE lvc_s_f4. "F4
2、增强定义Class
* local class to handle semantic checks
CLASS lcl_event_receiver DEFINITION DEFERRED.
DATA: g_event_receiver TYPE REF TO lcl_event_receiver.
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
"F4搜索帮助
METHODS handle_f4
FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname "列名
es_row_no "行号
er_event_data
et_bad_cells.
ENDCLASS. "lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_f4.
PERFORM frm_handle_f4 USING e_fieldname es_row_no er_event_data et_bad_cells.
ENDMETHOD.
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
3、fieldcat赋值
CASE gs_fieldcat-fieldname.
WHEN 'HKONT'.
gs_fieldcat-f4availabl = 'X'.
ENDCASE.
二、F4事件实现
*&---------------------------------------------------------------------*
*& Form frm_handle_f4
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> E_FIELDNAME
*& --> ES_ROW_NO
*& --> ER_EVENT_DATA
*& --> ET_BAD_CELLS
*&---------------------------------------------------------------------*
FORM frm_handle_f4 USING e_fieldname TYPE lvc_fname
es_row_no TYPE lvc_s_roid
er_event_data TYPE REF TO cl_alv_event_data
et_bad_cells.
DATA : BEGIN OF ls_newko,
hkont TYPE newko,
hkont_t TYPE char20,
END OF ls_newko.
DATA : lt_newko LIKE TABLE OF ls_newko.
DATA : lv_text TYPE char10.
CASE e_fieldname.
WHEN 'HKONT'.
READ TABLE gt_items INTO gs_items INDEX es_row_no-row_id.
IF sy-subrc = 0.
CASE gs_items-bschl(1).
WHEN '4'. "40选择科目
lv_text = '总账科目'.
SELECT a~saknr AS hkont,
b~txt20 AS hkont_t
FROM skb1 AS a
LEFT OUTER JOIN skat AS b
ON a~saknr = b~saknr
AND b~ktopl = 'LXJT'
AND b~spras = '1'
INTO CORRESPONDING FIELDS OF TABLE @lt_newko
WHERE a~bukrs = @gs_header-bukrs.
WHEN '0' OR '1'. "0/1开头选择客户
lv_text = '客户编码'.
SELECT a~kunnr AS hkont,
b~name1 AS hkont_t
FROM knb1 AS a
LEFT OUTER JOIN kna1 AS b
ON a~kunnr = b~kunnr
INTO CORRESPONDING FIELDS OF TABLE @lt_newko
WHERE a~bukrs = @gs_header-bukrs.
ENDCASE.
PERFORM frm_callf4_function USING e_fieldname lv_text lt_newko[] es_row_no er_event_data.
ENDIF.
ENDCASE.
"设置后,alv稳定刷新
stbl-row = 'X'." 基于行的稳定刷新
stbl-col = 'X'." 基于列稳定刷新
CALL METHOD g_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_callf4_function
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> E_FIELDNAME
*& --> LT_NEWKO
*& --> ES_ROW_NO
*& --> ER_EVENT_DATA
*&---------------------------------------------------------------------*
FORM frm_callf4_function USING p_fieldname TYPE lvc_fname
p_text
pt_newko TYPE table
es_row_no TYPE lvc_s_roid
er_event_data TYPE REF TO cl_alv_event_data.
DATA: lt_return TYPE STANDARD TABLE OF ddshretval,
ls_return TYPE ddshretval.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'HKONT'
dynpprog = sy-repid
dynpnr = sy-dynnr
window_title = p_text
callback_program = sy-repid
value_org = 'S'
TABLES
value_tab = pt_newko
return_tab = lt_return
EXCEPTIONS
parameter_error = 1
no_values_found = 2.
IF sy-subrc = 0.
READ TABLE gt_items ASSIGNING FIELD-SYMBOL(<fs_items>) INDEX es_row_no-row_id.
IF sy-subrc = 0.
READ TABLE lt_return INTO ls_return INDEX 1.
IF ls_return-fieldval IS NOT INITIAL.
<fs_items>-hkont = ls_return-fieldval.
<fs_items>-hkont = |{ <fs_items>-hkont ALPHA = IN WIDTH = 10 }|.
CASE <fs_items>-bschl(1).
WHEN '4'.
SELECT SINGLE txt20 FROM skat INTO <fs_items>-hktxt WHERE spras = '1' AND saknr = <fs_items>-hkont.
WHEN '0' OR '1'.
SELECT SINGLE name_org1 FROM but000 INTO <fs_items>-hktxt WHERE partner = <fs_items>-hkont.
WHEN OTHERS.
ENDCASE.
ENDIF.
CLEAR ls_return.
er_event_data->m_event_handled = 'X'.
ENDIF.
ENDIF.
ENDFORM.
三、ALV调用设置
这里以OOALV为例。
“添加F4事件
SET HANDLER g_event_receiver->handle_f4 FOR g_grid.
"F4
gs_f4-fieldname = 'HKONT'. "窗口时间参数(需要定义F4帮助按钮的字段)
gs_f4-register = 'X'.
gs_f4-getbefore = 'X'.
gs_f4-chngeafter = 'X'.
INSERT gs_f4 INTO TABLE gt_f4.
CALL METHOD g_grid->register_f4_for_fields
EXPORTING
it_f4 = gt_f4[].
四、效果展示
五、Function ALV实现
Function ALV就是OOALV套一层壳子
gs_event-name = 'CALLER_EXIT'.
gs_event-form = 'FRM_F4'.
APPEND gs_event TO gt_event.
FORM frm_f4 USING e_grid TYPE slis_data_caller_exit.
DATA: gr_grid TYPE REF TO cl_gui_alv_grid. "用于绑定ALV事件
DATA: lt_f4 TYPE lvc_t_f4,
ls_f4 TYPE lvc_s_f4.
IF gr_grid IS INITIAL.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = gr_grid.
ENDIF.
* 设置enter事件
CALL METHOD gr_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
"注册事件,设置,触发F4的搜索帮助
DATA(lr_event_receiver2) = NEW lcl_event_receiver( ) ."新语法
SET HANDLER lr_event_receiver2->handle_onf4 FOR gr_grid. "绑定CLASS事件到ALV
ls_f4-fieldname = 'CHARG'. "窗口时间参数(需要定义F4帮助按钮的字段)
ls_f4-register = 'X'.
ls_f4-getbefore = 'X'.
ls_f4-chngeafter = 'X'.
INSERT ls_f4 INTO TABLE lt_f4.
CALL METHOD gr_grid->register_f4_for_fields
EXPORTING
it_f4 = lt_f4[].
ENDFORM.
作者:小飞猪猪猪猪猪猪猪–CSDN