在用ALV顯示數據的時候,有時候,一些列需要更改,客戶可能要求像標准程序一樣,按F4按鈕,彈出該列的相關數據,選擇後,反填到單元格中,以下程序是示例程序。
REPORT Z_EXAMPLE.
************************************************************************
* DATEN DEFINITION *
************************************************************************
TYPE-POOLS: slis.
*———————————————————————*
* CLASS lcl_event_receiver DEFINITION
*———————————————————————*
class lcl_event_receiver definition.
public section.
class-methods:
on_f4 for event onf4 of cl_gui_alv_grid
importing sender
e_fieldname
e_fieldvalue
es_row_no
er_event_data
et_bad_cells
e_display,
on_data_changed for event data_changed of cl_gui_alv_grid
importing e_onf4
e_onf4_before
e_onf4_after
er_data_changed
e_ucomm
sender.
private section.
types: ddshretval_table type table of ddshretval.
class-methods: my_f4
importing sender type ref to cl_gui_alv_grid
et_bad_cells type lvc_t_modi
es_row_no type lvc_s_roid
er_event_data type ref to cl_alv_event_data
e_display type c
e_fieldname type lvc_fname
exporting IM_lt_f4 type ddshretval_table.
endclass. “lcl_event_receiver DEFINITION
tables: mara.
TYPES:
BEGIN OF t_alv,
matnr TYPE makt-matnr,
MTART type mara-MTART,
MTBEZ type t134t-MTBEZ,
maktx TYPE makt-maktx,
spras TYPE makt-spras,
Value Type i,
END OF t_alv.
DATA:
gt_fieldcatalog TYPE slis_t_fieldcat_alv,
gt_events TYPE slis_t_event,
gw_events TYPE slis_alv_event,
gt_data TYPE TABLE OF t_alv,
gw_data TYPE t_alv,
g_repid TYPE sy-repid VALUE sy-repid.
data: g_grid type ref to cl_gui_alv_grid.
data: gt_bad_cells type lvc_t_modi.
data: gt_fieldcat type lvc_t_fcat.
data: g_row_id type lvc_s_roid-row_id.
field-symbols: <gs_outtab> type any.
************************************************************************
* SELECTION SCREEN *
************************************************************************
SELECT-OPTIONS: s_matnr FOR mara-matnr .
************************************************************************
* START-OF-SELECTION *
************************************************************************
START-OF-SELECTION.
SELECT mara~matnr makt~maktx mara~MTART t134t~MTBEZ makt~spras
INTO CORRESPONDING FIELDS OF TABLE gt_data up to 50 rows
FROM mara
inner join makt on mara~matnr = makt~matnr and makt~SPRAS = ‘EN’
inner join t134t on mara~MTART = t134t~MTART and t134t~SPRAS = ‘EN’
WHERE mara~matnr IN s_matnr.
PERFORM creat_fieldcat.
PERFORM creat_event_exits.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = g_repid
it_fieldcat = gt_fieldcatalog
it_events = gt_events
TABLES
t_outtab = gt_data.
*———————————————————————*
* Create Output Field Format
*———————————————————————*
FORM creat_fieldcat.
data gw_fieldcatalog TYPE slis_fieldcat_alv.
CLEAR gw_fieldcatalog.
gw_fieldcatalog-fieldname = ‘MATNR’.
gw_fieldcatalog-ref_fieldname = ‘MATNR’.
gw_fieldcatalog-ref_tabname = ‘MAKT’.
APPEND gw_fieldcatalog TO gt_fieldcatalog.
CLEAR gw_fieldcatalog.
gw_fieldcatalog-fieldname = ‘MAKTX’.
gw_fieldcatalog-ref_fieldname = ‘MAKTX’.
gw_fieldcatalog-ref_tabname = ‘MAKT’.
APPEND gw_fieldcatalog TO gt_fieldcatalog.
CLEAR gw_fieldcatalog.
gw_fieldcatalog-fieldname = ‘MTART’.
gw_fieldcatalog-ref_fieldname = ‘MTART’.
gw_fieldcatalog-ref_tabname = ‘MARA’.
gw_fieldcatalog-EDIT = ‘X’.
APPEND gw_fieldcatalog TO gt_fieldcatalog.
CLEAR gw_fieldcatalog.
gw_fieldcatalog-fieldname = ‘MTBEZ’.
gw_fieldcatalog-ref_fieldname = ‘MTBEZ’.
gw_fieldcatalog-ref_tabname = ‘T134T’.
APPEND gw_fieldcatalog TO gt_fieldcatalog.
CLEAR gw_fieldcatalog.
gw_fieldcatalog-fieldname = ‘VALUE’.
gw_fieldcatalog-seltext_s = ‘VALUE’.
gw_fieldcatalog-outputlen = 13.
APPEND gw_fieldcatalog TO gt_fieldcatalog.
ENDFORM. ” creat_fieldcat
*———————————————————————*
* Form creat_event_exits
*———————————————————————*
FORM creat_event_exits .
gw_events-name = ‘CALLER_EXIT’.
gw_events-form = ‘CALLER_EXIT’.
APPEND gw_events TO gt_events.
ENDFORM. ” creat_event_exits
*———————————————————————*
*———————————————————————*
FORM caller_exit USING ls_data TYPE slis_data_caller_exit.
DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.
data: lt_wa_f4 type lvc_s_f4,
lt_f4 type lvc_t_f4.
CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCR’
IMPORTING
e_grid = l_ref_alv.
set handler lcl_event_receiver=>on_f4 for l_ref_alv.
set handler lcl_event_receiver=>on_data_changed for l_ref_alv.
lt_wa_f4-fieldname = ‘MTART’.
lt_wa_f4-register = ‘X’.
lt_wa_f4-getbefore = ‘X’.
lt_wa_f4-chngeafter = ‘X’.
append lt_wa_f4 to lt_f4.
call method l_ref_alv->register_f4_for_fields
EXPORTING
it_f4 = lt_f4.
ENDFORM. “CALLER_EXIT
*&———————————————————————*
*& Form f4_set
*———————————————————————-*
form f4_set using r_grid type ref to cl_gui_alv_grid
rt_fieldcat type lvc_t_fcat
rt_bad_cells type lvc_t_modi
r_row_id type lvc_s_roid-row_id
rs_outtab.
g_grid = r_grid.
gt_fieldcat = rt_fieldcat.
gt_bad_cells = rt_bad_cells.
g_row_id = r_row_id.
assign rs_outtab to <gs_outtab>.
endform. “f4_set
*&———————————————————————*
*& Form f4
*&———————————————————————*
*———————————————————————-*
form f4 tables it_dummy structure seahlpres
changing r_shlp type shlp_descr_t
r_callcontrol like ddshf4ctrl.
data: ls_interface type ddshiface.
data: ls_fieldcat type lvc_s_fcat.
data: ls_bad_cells type lvc_s_modi.
data: l_tabix type sy-tabix.
field-symbols: <l_field>.
ls_interface-shlpfield = ‘MTBEZ’.
ls_interface-valfield = ‘MTBEZ’.
append ls_interface to r_shlp-interface.
loop at r_shlp-interface into ls_interface.
l_tabix = sy-tabix.
check ls_interface-f4field is initial
and not ls_interface-valtabname is initial.
read table gt_fieldcat into ls_fieldcat
with key ref_table = ls_interface-valtabname
ref_field = ls_interface-valfield.
if sy-subrc = 0.
read table gt_bad_cells into ls_bad_cells
with key fieldname = ls_fieldcat-fieldname
row_id = g_row_id.
if sy-subrc = 0.
ls_interface-value = ls_bad_cells-value.
ls_interface-internal = ‘ ‘.
modify r_shlp-interface from ls_interface index l_tabix.
else.
assign component ls_fieldcat-fieldname of structure
<gs_outtab> to <l_field>.
if sy-subrc = 0 and
not ( <l_field> is initial and
ls_fieldcat-no_init_ch ca ‘FB’ ).
ls_interface-value = <l_field>.
ls_interface-internal = ‘X’.
modify r_shlp-interface from ls_interface index l_tabix.
endif.
endif.
endif.
endloop.
endform. “f4
*———————————————————————*
* CLASS lcl_event_receiver IMPLEMENTATION
*———————————————————————*
class lcl_event_receiver implementation.
method on_f4.
data: ls_f4 type ddshretval,
lt_f4 type table of ddshretval.
field-symbols: <itab> type lvc_t_modi.
data: ls_modi type lvc_s_modi.
call method my_f4
EXPORTING
sender = sender
es_row_no = es_row_no
er_event_data = er_event_data
et_bad_cells = et_bad_cells
e_display = e_display
e_fieldname = e_fieldname
IMPORTING
IM_lt_f4 = lt_f4.
assign er_event_data->m_data->* to <itab>.
read table lt_f4 into ls_f4 with key fieldname = ‘MTART’.
if not ls_f4 is initial.
ls_modi-row_id = es_row_no-row_id.
ls_modi-fieldname = ‘MTART’.
ls_modi-value = ls_f4-fieldval.
append ls_modi to <itab>.
ls_modi-row_id = es_row_no-row_id.
ls_modi-fieldname = ‘VALUE’.
if ls_f4-fieldval < ‘D’.
ls_modi-value = 100.
elseif ls_f4-fieldval < ‘R’.
ls_modi-value = 1000.
else.
ls_modi-value = 10000.
endif.
append ls_modi to <itab>.
endif.
read table lt_f4 into ls_f4 with key fieldname = ‘MTBEZ’.
if not ls_f4 is initial.
ls_modi-row_id = es_row_no-row_id.
ls_modi-fieldname = ‘MTBEZ’.
ls_modi-value = ls_f4-fieldval.
append ls_modi to <itab>.
endif.
er_event_data->m_event_handled = ‘X’.
endmethod. “on_f4
*———————————————————————*
* METHOD on_data_changed *
*———————————————————————*
method on_data_changed.
endmethod. “on_data_changed
*———————————————————————*
* METHOD my_f4 insert here your own f4-help *
*———————————————————————*
method my_f4.
data: lw_tab like line of gt_data,
lt_fcat type lvc_t_fcat,
ls_fieldcat type lvc_s_fcat,
l_tabname type dd03v-tabname,
l_fieldname type dd03v-fieldname,
l_help_valu type help_info-fldvalue,
lt_bad_cell type lvc_t_modi,
lp_wa type ref to data.
field-symbols: <l_field_value> type any,
<ls_wa> type any.
call method sender->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = lt_fcat.
read table gt_data index es_row_no-row_id into lw_tab.
create data lp_wa like line of gt_data.
assign lp_wa->* to <ls_wa>.
<ls_wa> = lw_tab.
read table lt_fcat with key fieldname = e_fieldname into ls_fieldcat.
move ls_fieldcat-ref_table to l_tabname.
move ls_fieldcat-fieldname to l_fieldname.
assign component ls_fieldcat-fieldname of structure lw_tab to <l_field_value>.
write <l_field_value> to l_help_valu.
perform f4_set using sender
lt_fcat
lt_bad_cell
es_row_no-row_id
<ls_wa>.
call function ‘F4IF_FIELD_VALUE_REQUEST’
EXPORTING
tabname = l_tabname
fieldname = l_fieldname
display = e_display
value = l_help_valu
callback_program = ‘ZTEST7′
callback_form = ‘F4′
TABLES
return_tab = IM_lt_f4
exceptions
parameter_error = 1
no_values_found = 2
others = 3.
endmethod. “my_f4
endclass. “lcl_event_receiver IMPLEMENTATION