When we use an editable ALV, some fields must have a custom Search Help. Let's describe the simple way to add a custom Search Help to a specific field of an ALV using OOPS.
Assuming we have a field called BWTAR in our alv table GT_OUT.
At fieldcat, we need to set this field editable and with F4:
CLEAR ls_fcat.
ls_fcat-fieldname = 'BWTAR'.
ls_fcat-ref_field = 'BWTAR'.
ls_fcat-ref_table = 'MBEW'.
ls_fcat-edit = 'X'.
ls_fcat-f4availabl = 'X'.
APPEND ls_fcat TO gt_fcat.
After create ALV Object GO_ALV, we need to register BWTAR field for F4 and set ONF4 event handler.
DATA: lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.
CLEAR lt_f4.
lt_f4-fieldname = 'BWTAR'.
lt_f4-register = 'X'.
INSERT table lt_f4.
" Register F4 for BWTAR
CALL METHOD go_alv->register_f4_for_fields
EXPORTING
it_f4 = lt_f4[].
" Set Handler
SET HANDLER lo_handler->handle_on_f4 FOR go_alv.
In our local event handler class, we need to define the ONF4 event and code it.
CLASS lcl_event_handler DEFINITION FINAL.
PUBLIC SECTION.
METHODS:
handle_on_f4
FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
er_event_data.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION.
*--Handle On F4
METHOD handle_on_f4.
PERFORM handle_on_f4
USING e_fieldname
es_row_no
er_event_data.
ENDMETHOD.
ENDCLASS.
At last, let's code handle_on_f4 perform:
FORM handle_on_f4
USING e_fieldname TYPE lvc_fname
es_row_no TYPE lvc_s_roid
er_event_data TYPE REF TO cl_alv_event_data.
" Types
TYPES: BEGIN OF ys_bwtar,
bwtar TYPE mbew-bwtar,
END OF ys_bwtar.
" Local Vars
DATA: lt_bwtar TYPE TABLE OF ys_bwtar,
ls_bwtar TYPE ys_bwtar,
lt_map TYPE TABLE OF dselc,
ls_map TYPE dselc,
lt_return TYPE TABLE OF ddshretval,
ls_return TYPE ddshretval,
ls_stable TYPE lvc_s_stbl.
" Field-symbols
FIELD-SYMBOLS: <l_out> TYPE ys_out. " ALV table line
" Check which field raise f4 event
CASE e_fieldname.
WHEN 'BWTAR'.
" Read current line
READ TABLE gt_out ASSIGNING <l_out>
INDEX es_row_no-row_id.
" Load F4 Data
SELECT bwtar
FROM mbew
INTO TABLE lt_bwtar
WHERE matnr = <l_out>-matnr AND
bwkey = <l_out>-werks.
" Set return field
CLEAR ls_map.
ls_map-fldname = 'F0001'.
ls_map-dyfldname = 'BWTAR'.
APPEND ls_map TO lt_map.
" Call Search Help Popup Function
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'BWTAR'
value_org = 'S'
TABLES
value_tab = lt_bwtar
dynpfld_mapping = lt_map
return_tab = lt_return
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
" Read selected f4 value
READ TABLE lt_return INTO ls_return WITH KEY fieldname = 'F0001'.
IF ls_return IS NOT INITIAL.
" Update ALV table
<l_out>-bwtar = ls_return-fieldval.
ENDIF.
ENDCASE.
ls_stable = 'XX'. " Set stable refresh for row and column
" ALV Refresh
CALL METHOD go_alv->refresh_table_display
EXPORTING
is_stable = ls_stable
i_soft_refresh = 'X'
EXCEPTIONS
finished = 1
OTHERS = 2.
" Avoid possible standard Search Help
er_event_data->m_event_handled = 'X'.
ENDFORM.
That's it!