方法一:
REPORT ZDCH_09_TEST2.
************************************************************************
* DATEN DEFINITION *
************************************************************************
TYPE-POOLS: SLIS.
TABLES:MAKT.
TYPES:
BEGIN OF T_DATA, "查询出来的数据
MATNR TYPE MAKT-MATNR,
MAKTX TYPE MAKT-MAKTX,
SPRAS TYPE MAKT-SPRAS,
END OF T_DATA,
BEGIN OF T_ALV, "alv显示的数据
MATNR TYPE MAKT-MATNR,
MAKTX TYPE MAKT-MAKTX,
SPRAS TYPE MAKT-SPRAS,
DD_HANDLE TYPE INT4, "绑定下拉框对应的组
END OF T_ALV.
DATA:
"fieldcat
GT_FIELDCATALOG TYPE LVC_T_FCAT,
GW_FIELDCATALOG TYPE LVC_S_FCAT,
*定义存储下拉列表的数据
GT_DDVAL TYPE LVC_T_DROP,
GW_DDVAL TYPE LVC_S_DROP,
"alv事件
GT_EVENTS TYPE SLIS_T_EVENT,
GW_EVENTS TYPE SLIS_ALV_EVENT,
"数据库数据
GT_DATA TYPE TABLE OF T_DATA,
GW_DATA TYPE T_DATA,
"alv数据
GT_ALV TYPE TABLE OF T_ALV,
GW_ALV TYPE T_ALV,
G_REPID TYPE SY-REPID VALUE SY-REPID.
************************************************************************
* SELECTION SCREEN *
************************************************************************
SELECT-OPTIONS: S_MATNR FOR MAKT-MATNR .
************************************************************************
* START-OF-SELECTION *
************************************************************************
START-OF-SELECTION.
"获取数据
SELECT MATNR MAKTX SPRAS
INTO TABLE GT_DATA
UP TO 50 ROWS
FROM MAKT
WHERE MATNR IN S_MATNR
AND SPRAS = '1'.
"alv填充数据
LOOP AT GT_DATA INTO GW_DATA.
MOVE-CORRESPONDING GW_DATA TO GW_ALV.
APPEND GW_ALV TO GT_ALV.
ENDLOOP.
"设置fieldcat
PERFORM CREAT_FIELDCAT.
"设置下拉框值
PERFORM CREAT_DROPDOWN_VALUES.
"设置下拉框事件
PERFORM CREAT_EVENT_EXITS.
"显示alv
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = G_REPID
IT_FIELDCAT_LVC = GT_FIELDCATALOG
IT_EVENTS = GT_EVENTS "这一步一定要记得添加上
TABLES
T_OUTTAB = GT_ALV.
*---------------------------------------------------------------------*
* 设置输出字段的格式
*---------------------------------------------------------------------*
FORM CREAT_FIELDCAT.
CLEAR GW_FIELDCATALOG.
GW_FIELDCATALOG-FIELDNAME = 'MATNR'.
GW_FIELDCATALOG-REF_FIELD = 'MATNR'.
GW_FIELDCATALOG-REF_TABLE = 'MAKT'.
APPEND GW_FIELDCATALOG TO GT_FIELDCATALOG.
CLEAR GW_FIELDCATALOG.
GW_FIELDCATALOG-FIELDNAME = 'MAKTX'.
GW_FIELDCATALOG-REF_FIELD = 'MAKTX'.
GW_FIELDCATALOG-REF_TABLE = 'MAKT'.
APPEND GW_FIELDCATALOG TO GT_FIELDCATALOG.
CLEAR GW_FIELDCATALOG.
GW_FIELDCATALOG-FIELDNAME = 'SPRAS'.
GW_FIELDCATALOG-DRDN_FIELD = 'DD_HANDLE'. "设置下拉框
* GW_FIELDCATALOG-DRDN_HNDL = '1'. "分组标识
GW_FIELDCATALOG-EDIT = 'X'. "可编辑
APPEND GW_FIELDCATALOG TO GT_FIELDCATALOG.
ENDFORM. " creat_fieldcat
*---------------------------------------------------------------------*
*根据物料的所有的语言生成不同的生成下拉列表
*---------------------------------------------------------------------*
FORM CREAT_DROPDOWN_VALUES.
DATA: L_SPRAS TYPE MAKT-SPRAS,
L_COUNT TYPE I.
"为了演示简单,这里只设置了一组
"句柄HANDLE 为I类型,相同数字为同一组下拉框"
GW_DDVAL-HANDLE = 1. "对应分组标识第一组
GW_DDVAL-VALUE = 1. "组值
APPEND GW_DDVAL TO GT_DDVAL.
CLEAR GW_DDVAL.
GW_DDVAL-HANDLE = 1. "第一组
GW_DDVAL-VALUE = 2.
APPEND GW_DDVAL TO GT_DDVAL.
CLEAR GW_DDVAL.
GW_DDVAL-HANDLE = 1. "第一组
GW_DDVAL-VALUE = 3.
APPEND GW_DDVAL TO GT_DDVAL.
CLEAR GW_DDVAL.
LOOP AT GT_ALV INTO GW_ALV.
GW_ALV-DD_HANDLE = 1. "ALV绑定组
MODIFY GT_ALV FROM GW_ALV.
ENDLOOP.
ENDFORM. " creat_dropdown_values
*---------------------------------------------------------------------*
* 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
*---------------------------------------------------------------------*
*设置下拉列表,使Grid和内表能链接上
*---------------------------------------------------------------------*
FORM CALLER_EXIT USING LS_DATA TYPE SLIS_DATA_CALLER_EXIT.
DATA: L_REF_ALV TYPE REF TO CL_GUI_ALV_GRID.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = L_REF_ALV.
CALL METHOD L_REF_ALV->SET_DROP_DOWN_TABLE
EXPORTING
IT_DROP_DOWN = GT_DDVAL.
ENDFORM. "CALLER_EXIT
需要注意的是:如果下拉框设置的值是中文汉字,那么就不能输入了。
还有:下拉框设置了值,并且设置了可编辑,此时编辑的时候只能输入下拉框中有的值,其他的值输入不进去。
设置F4搜索帮助
只需要把之前设置的DRDN_FIELD修改成F4AVAILABL,然后设置上参考表和参考字段,去表里给这个字段设置上它的搜索帮助,那么ALV也就可以用了!
方法二:
ALV通过参考可以直接获取表中搜索帮助,但是表中字段没有搜索帮助或者不合要求就需要通过F4事件完成了。
CLASS lcl_event_receiver DEFINITION DEFERRED.
DATA:gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
go_grid TYPE REF TO cl_gui_alv_grid,
go_event_receiver TYPE REF TO lcl_event_receiver.
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_f4_help FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
er_event_data
et_bad_cells
e_display.
ENDCLASS. "lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_f4_help.
FIELD-SYMBOLS:<fs_alv> TYPE gs_alv.
CASE e_fieldname.
WHEN 'ARKTX'.
READ TABLE git_alv ASSIGNING <fs_alv> INDEX es_row_no-row_id.
IF sy-subrc = 0.
PERFORM frm_get_arktx_f4 CHANGING <fs_alv>-arktx.
ENDIF.
* WHEN .
WHEN OTHERS.
ENDCASE.
CALL METHOD go_grid->refresh_table_display.
ENDMETHOD.
ENDCLASS. "lcl_event_receiver
定义alv-fieldcat属性
-
gs_fieldcat-f4availabl = 'X'.
-
gs_fieldcat-edit = 'X'.
将F4事件注册到ALV对象中
DATA:lt_f4 TYPE lvc_t_f4,
ls_f4 TYPE lvc_s_f4.
CALL METHOD go_grid->set_table_for_first_display
EXPORTING
it_toolbar_excluding = lt_exclude
CHANGING
it_fieldcatalog = git_fieldcat
it_outtab = git_alv[].
CLEAR:ls_f4.
ls_f4-fieldname = 'ARKTX'.
ls_f4-register = 'X'.
ls_f4-getbefore = 'X'.
ls_f4-chngeafter = ''.
ls_f4-internal = ''.
INSERT ls_f4 INTO TABLE lt_f4.
CALL METHOD go_grid->register_f4_for_fields
EXPORTING
it_f4 = lt_f4.
CREATE OBJECT go_event_receiver.
SET HANDLER go_event_receiver->handle_f4_help FOR go_grid .
自定义F4,获取数
*&---------------------------------------------------------------------*
*& Form FRM_GET_ARKTX_F4
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_<FS_ALV>_ARKTX text
*----------------------------------------------------------------------*
FORM frm_get_arktx_f4 USING pc_matnr TYPE vbrp-matnr
CHANGING pc_arktx TYPE zfit005-maktx.
DATA:BEGIN OF lt_zfit029 OCCURS 0,
sub_type_m TYPE zfit029-sub_type_m,
sub_name_m TYPE zfit029-sub_name_m,
END OF lt_zfit029.
DATA:lt_ret_tab TYPE TABLE OF ddshretval WITH HEADER LINE.
DATA:lv_matnr TYPE zfit028-matnr.
lv_matnr = pc_matnr.
SELECT sub_type_m
sub_name_m
INTO TABLE lt_zfit029
FROM zfit028
INNER JOIN zfit029 ON zfit028~type_m = zfit029~type_m
WHERE zfit028~matnr = lv_matnr.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'SUB_NAME_M'
value_org = 'S'
TABLES
value_tab = lt_zfit029
return_tab = lt_ret_tab
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc = 0.
READ TABLE lt_ret_tab INDEX 1.
IF sy-subrc = 0 AND lt_ret_tab-fieldval IS NOT INITIAL.
pc_arktx = lt_ret_tab-fieldval.
ENDIF.
ENDIF.
ENDFORM. " FRM_GET_ARKTX_F4
方法三:
自动带出描述的代码:
SELECT SINGLE NAME1
INTO ZPM004-NAME1
FROM T001W
WHERE WERKS = ZPM004-IWERK
AND SPRAS = SY-LANGU.
添加 F4的代码
DATA:BEGIN OF LT_WERKS OCCURS 0,
WERKS LIKE T001W-WERKS,
NAME1 LIKE T001W-NAME1,
END OF LT_WERKS.
SELECT WERKS NAME1
INTO CORRESPONDING FIELDS OF TABLE LT_WERKS
FROM T001W.
SORT LT_WERKS BY WERKS.
DELETE ADJACENT DUPLICATES FROM LT_WERKS COMPARING WERKS.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
RETFIELD = 'WERKS'
DYNPPROG = SY-REPID
DYNPNR = SY-DYNNR
DYNPROFIELD = 'IWERK'
VALUE_ORG = 'S'
TABLES
VALUE_TAB = LT_WERKS
EXCEPTIONS
PARAMETER_ERROR = 1
NO_VALUES_FOUND = 2
OTHERS = 3.