如何利用 ABAP 实现自定义 F4 搜索帮助
【摘要】 介绍在之前的文章中,介绍了如何通过自定义搜索帮助,但是这种搜索帮助仅限于简单的单字段描述带出。对于一些对上下文有要求的搜索帮助,就需要利用代码来做了,比如一个物料有不同的品牌,每个品牌下关联的类别又不一样,子类别也需要根据上面的类型。那么这种层层递进的搜索帮助如何做呢?在业务中我们有这样的选择屏幕: 代码实现主要用到的函数名叫做 F4IF_INT_TABLE_VALUE_REQUEST,返...
介绍
在之前的文章中,介绍了如何通过自定义搜索帮助,但是这种搜索帮助仅限于简单的单字段描述带出。
对于一些对上下文有要求的搜索帮助,就需要利用代码来做了,比如一个物料有不同的品牌,每个品牌下关联的类别又不一样,子类别也需要根据上面的类型。那么这种层层递进的搜索帮助如何做呢?
在业务中我们有这样的选择屏幕:
代码实现
主要用到的函数名叫做 F4IF_INT_TABLE_VALUE_REQUEST
,返回在内部表中选择的值。
主要有几个重要的参数:
RETFIELD
: 搜索帮助内表要输出的的帮助字段名,注:要大写DYNPPROG
:执行程序DYNPNR
: 屏幕编号DYNPROFIELD
: 指定屏幕参数的名称VALUE_TAB
: 这个就是我们用于存储搜索帮助内容的内表,必填
*AT SELECTION-SCREEN .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR zbrand-low.
SELECT zbrand zbrandt INTO CORRESPONDING FIELDS OF TABLE gt_zbrand FROM zrbc ORDER BY zbrand.
SELECT zbrand zbrandt APPENDING CORRESPONDING FIELDS OF TABLE gt_zbrand FROM zrbc1 WHERE loekz NE 'X'.
SORT gt_zbrand BY zbrand.
DELETE ADJACENT DUPLICATES FROM gt_zbrand COMPARING zbrand.
CLEAR: l_ddshretval,t_ddshretval.
REFRESH t_ddshretval.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' "调用函数
EXPORTING
retfield = 'ZBRAND' "搜索帮助内表要输出的的帮助字段名,注:要大写
dynpprog = sy-repid
dynpnr = sy-dynnr
value_org = 'S'
dynprofield = 'ZBRAND' "指定屏幕参数的名称
TABLES
value_tab = gt_zbrand "存储搜索帮助内容的内表
return_tab = t_ddshretval.
READ TABLE t_ddshretval INTO l_ddshretval INDEX 1.
l_zbrand = l_ddshretval-fieldval.
mara-zbrand = l_zbrand.
自此,完成了对品牌描述的搜索帮助,接下来,就需要通过这次选择的品牌带出对应的类别,代码如下:
START-OF-SELECTION .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR zctgry-low.
SELECT zctgry zctgryt INTO CORRESPONDING FIELDS OF TABLE gt_zctgry FROM zrbc WHERE zbrand = mara-zbrand ORDER BY zctgry.
SELECT zctgry zctgryt APPENDING CORRESPONDING FIELDS OF TABLE gt_zctgry FROM zrbc1 WHERE zbrand = mara-zbrand AND loekz NE 'X'.
SORT gt_zctgry BY zctgry.
DELETE ADJACENT DUPLICATES FROM gt_zctgry COMPARING zctgry.
CLEAR: l_ddshretval,t_ddshretval.
REFRESH t_ddshretval.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' "调用函数
EXPORTING
retfield = 'ZCTGRY' "搜索帮助内表要输出的的帮助字段名,注:要大写
dynpprog = sy-repid
dynpnr = sy-dynnr
value_org = 'S'
dynprofield = 'ZCTGRY' "指定屏幕参数的名称
TABLES
return_tab = t_ddshretval
value_tab = gt_zctgry. "存储搜索帮助内容的内表
READ TABLE t_ddshretval INTO l_ddshretval INDEX 1.
* MESSAGE 'CATEGORY IS :' && L_DDSHRETVAL-FIELDVAL TYPE 'E'.
IF sy-subrc = 0.
l_zctgry = l_ddshretval-fieldval.
mara-zctgry = l_zctgry.
ENDIF.
* SELECT SINGLE ZCTGRYT INTO GS_TEXT-ZCTGRY FROM ZRBC WHERE ZBRAND = MARA-ZBRAND AND ZCTGRY = MARA-ZCTGRY.
*DATA: dynpfields TYPE TABLE OF dynpread WITH HEADER LINE.
IF sy-subrc <> 0.
mara-zctgry = ''.
ENDIF.
子类别也类似,你可以根据实际业务需求来进行变更。
使用 SALV 显示 F4 弹窗
CLASS lcl_f4 DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
msgnr_salv
RETURNING VALUE(rv_msgnr) TYPE t100-msgnr.
PRIVATE SECTION.
TYPES:
BEGIN OF lty_t100,
msgnr TYPE t100-msgnr,
text TYPE t100-text,
END OF lty_t100.
CLASS-DATA: t_t100 TYPE STANDARD TABLE OF lty_t100.
ENDCLASS. "lcl_f4 DEFINITION
*
CLASS lcl_f4 IMPLEMENTATION.
METHOD msgnr_salv.
DATA: o_salv_f4 TYPE REF TO cl_salv_table.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
DATA: lo_selections TYPE REF TO cl_salv_selections.
DATA: lt_rows TYPE salv_t_row,
lv_row TYPE i.
DATA: ls_t100 LIKE LINE OF t_t100.
SELECT msgnr text
INTO TABLE t_t100
FROM t100
UP TO 10 ROWS
WHERE arbgb = '00'
AND sprsl = sy-langu.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_salv_f4
CHANGING
t_table = t_t100 ).
*
lo_functions = o_salv_f4->get_functions( ).
lo_functions->set_default( 'X' ).
lo_selections = o_salv_f4->get_selections( ).
lo_selections->set_selection_mode( if_salv_c_selection_mode=>SINGLE ).
* ALV as Popup
o_salv_f4->set_screen_popup(
start_column = 80
end_column = 140
start_line = 3
end_line = 10 ).
* Display
o_salv_f4->display( ).
* Get the result
lt_rows = lo_selections->get_selected_rows( ).
READ TABLE lt_rows INTO lv_row INDEX 1.
IF sy-subrc EQ 0.
READ TABLE t_t100 INTO ls_t100 INDEX lv_row.
rv_msgnr = ls_t100-msgnr.
ENDIF.
ENDMETHOD. "msgnr_salv
ENDCLASS. "lcl_f4 IMPLEMENTATION
使用 ALV 弹出窗口
CLASS-METHODS:
msgnr_alv
RETURNING VALUE(rv_msgnr) TYPE t100-msgnr.
***
METHOD msgnr_alv.
TYPE-POOLS: slis.
DATA: lt_fcat TYPE STANDARD TABLE OF slis_fieldcat_alv.
DATA: ls_fcat TYPE slis_fieldcat_alv.
DATA: ls_tabix TYPE slis_selfield.
DATA: ls_t100 LIKE LINE OF t_t100.
SELECT msgnr text
INTO TABLE t_t100
FROM t100
UP TO 10 ROWS
WHERE arbgb = '00'
AND sprsl = sy-langu.
ls_fcat-fieldname = 'MSGNR'.
ls_fcat-tabname = 'T_T100'.
ls_fcat-reptext_ddic = 'Message'.
ls_fcat-outputlen = '10'.
APPEND ls_fcat TO lt_fcat.
ls_fcat-fieldname = 'TEXT'.
ls_fcat-tabname = 'T_T100'.
ls_fcat-reptext_ddic = 'Message Text'.
ls_fcat-outputlen = '50'.
APPEND ls_fcat TO lt_fcat.
CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
EXPORTING
i_title = 'Popup To Select'
i_allow_no_selection = 'X'
i_zebra = 'X'
i_tabname = 'T_T100'
it_fieldcat = lt_fcat
IMPORTING
es_selfield = ls_tabix
TABLES
t_outtab = t_t100.
IF ls_tabix IS NOT INITIAL.
READ TABLE t_t100 INTO ls_t100 INDEX ls_tabix-tabindex.
rv_msgnr = ls_t100-msgnr.
ENDIF.
ENDMETHOD. "msgnr_alv
FM F4IF_INT_TABLE_VALUE_REQUEST
CLASS-METHODS:
msgnr_f4fm
RETURNING VALUE(rv_msgnr) TYPE t100-msgnr.
***
METHOD msgnr_f4fm.
SELECT msgnr text
INTO TABLE t_t100
FROM t100
UP TO 10 ROWS
WHERE arbgb = '00'
AND sprsl = sy-langu.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'MSGNR'
dynpprog = sy-repid
dynpnr = sy-dynnr
dynprofield = 'P_MSGNR'
window_title = 'Select Value'
value_org = 'S'
display = ' '
TABLES
value_tab = t_t100
EXCEPTIONS
parameter_error = 1
no_values_found = 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.
ENDMETHOD. "msgnr_f4FM
将 FM F4IF_INT_TABLE_VALUE_REQUEST 与一张列表结合使用
CLASS-METHODS:
msgnr_f4fm_column
RETURNING VALUE(rv_msgnr) TYPE t100-msgnr.
***
METHOD msgnr_f4fm_column.
SELECT msgnr text
INTO TABLE t_t100
FROM t100
UP TO 10 ROWS
WHERE arbgb = '00'
AND sprsl = sy-langu.
TYPES:
BEGIN OF lty_char100,
VALUE TYPE char100,
END OF lty_char100.
DATA: lt_values TYPE STANDARD TABLE OF lty_char100.
DATA: ls_values LIKE LINE OF lt_values.
DATA: ls_t100 LIKE LINE OF t_t100.
DATA: lt_ftab TYPE STANDARD TABLE OF dfies ,
ls_ftab LIKE LINE OF lt_ftab.
DATA: lt_ret_tab TYPE STANDARD TABLE OF ddshretval,
ls_ret_tab LIKE LINE OF lt_ret_tab.
LOOP AT t_t100 INTO ls_t100.
ls_values = ls_t100-msgnr.
APPEND ls_values TO lt_values.
ls_values = ls_t100-text.
APPEND ls_values TO lt_values.
ENDLOOP.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'T100'
TABLES
dfies_tab = lt_ftab
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
ENDIF.
DELETE lt_ftab WHERE fieldname NE 'MSGNR'
AND fieldname NE 'TEXT'.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'MSGNR'
dynpprog = sy-repid
dynpnr = sy-dynnr
* dynprofield = 'P_MSGNR'
window_title = 'Select Value'
value_org = 'C'
display = ' '
TABLES
value_tab = lt_values
field_tab = lt_ftab
return_tab = lt_ret_tab
EXCEPTIONS
parameter_error = 1
no_values_found = 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.
READ TABLE lt_ret_tab INTO ls_ret_tab INDEX 1.
rv_msgnr = ls_ret_tab-fieldval.
ENDMETHOD. "msgnr_f4FM_Column
调用方式:
PARAMETERS: p_msgnr TYPE t100-msgnr OBLIGATORY.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_msgnr.
p_msgnr = lcl_f4=>msgnr_salv( ).
"p_msgnr = lcl_f4=>msgnr_alv( ).
"p_msgnr = lcl_f4=>msgnr_f4FM( ).
"p_msgnr = lcl_f4=>msgnr_f4fm_column( ).
参考链接:
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)