ABAP-ALV-列:F4搜索帮助

 业务需求

在SAP项目中,会经常碰到如下需求:

::  ALV的显示界面中,需要控制特定ALV的增加客户化的F4搜索帮助

: F4搜索帮助:

ABAP-ALV-列:F4搜索帮助_第1张图片

实现步骤:

一、设置ALV显示函数REUSE_ALV_GRID_DISPLAY_LVC,输入参数IT_FIELDCAT_LVC,当字段为ZXM时,启用F4搜索帮助,如下

ABAP-ALV-列:F4搜索帮助_第2张图片 ABAP-ALV-列:F4搜索帮助_第3张图片ABAP-ALV-列:F4搜索帮助_第4张图片

二、设置ALV显示函数REUSE_ALV_GRID_DISPLAY_LVC,输入参数IT_EVENTS,增加CALLER_EXIT事件,如下:

ABAP-ALV-列:F4搜索帮助_第5张图片

响应FROM(FRM_CALLER_EXIT)代码如下:

ABAP-ALV-列:F4搜索帮助_第6张图片

主要功能:

1:建立当前显示ALV与全局ALV对象GO_GIRD的联系。

2:注册激活GO_GIRD的单元格输入事件。

3:注册激活字段ZXM的F4帮助事件,并绑定F4事件处理到客户化处理方法LO_EVENT_RECEIVER->HANDLE_F4。

实现代码:

GS_FCAT-F4AVAILABL = 'X'.

*&---------------------------------------------------------------------*
*& Form FRM_ALV_SET_EVENTS
*&---------------------------------------------------------------------*
*& ALV事件注册
*&---------------------------------------------------------------------*
FORM FRM_ALV_SET_EVENTS .
  DATA: LS_EVENTS TYPE SLIS_ALV_EVENT.

  LS_EVENTS-NAME = 'CALLER_EXIT' .
  LS_EVENTS-FORM =  'FRM_CALLER_EXIT'.
  APPEND LS_EVENTS TO GT_EVENTS.
ENDFORM.


*&---------------------------------------------------------------------*
*& Form FRM_CALLER_EXIT
*&---------------------------------------------------------------------*
*& CALLER_EXIT响应
*&---------------------------------------------------------------------*
FORM FRM_CALLER_EXIT USING E_GRID TYPE SLIS_DATA_CALLER_EXIT.
  DATA: LO_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER,
        LT_F4             TYPE LVC_T_F4 WITH HEADER LINE.

  IF GO_GRID IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        E_GRID = GO_GRID.
  ENDIF.

**注册ALV事件-单元格修改
  CALL METHOD GO_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER
    EXCEPTIONS
      ERROR      = 1
      OTHERS     = 2.

**指定OOALV 中F4字段
  LT_F4-FIELDNAME = 'ZXM'.   "指定字段'PLANE'有搜索帮助
  LT_F4-REGISTER  = 'X'.
  LT_F4-GETBEFORE = 'X'.
  LT_F4-CHNGEAFTER  = 'X'.
  APPEND LT_F4.

  CALL METHOD GO_GRID->REGISTER_F4_FOR_FIELDS
    EXPORTING
      IT_F4 = LT_F4[].
**** 注册F4 搜索帮助处理方法
  CREATE OBJECT LO_EVENT_RECEIVER.
  SET HANDLER LO_EVENT_RECEIVER->HANDLE_F4 FOR GO_GRID.
ENDFORM.

*&---------------------------------------------------------------------*
*& 包含               ZHYR_202308_HF04_CLASS
*&---------------------------------------------------------------------*
 DATA: GO_GRID TYPE REF TO CL_GUI_ALV_GRID.

*&-----------------------------------------------------------------*
*&  类定义
*&-----------------------------------------------------------------*
 CLASS   LCL_EVENT_RECEIVER DEFINITION.
   PUBLIC SECTION.
     METHODS: HANDLE_F4
       FOR EVENT ONF4 OF CL_GUI_ALV_GRID
       IMPORTING E_FIELDNAME      "字段名称
                 ES_ROW_NO        "行号,注意这是个结构
                 ER_EVENT_DATA .  "事件数据
 ENDCLASS.

*&-----------------------------------------------------------------*
*&  类实现
*&-----------------------------------------------------------------*
 CLASS: LCL_EVENT_RECEIVER IMPLEMENTATION.
   METHOD HANDLE_F4.
     DATA: LS_STABLE TYPE LVC_S_STBL.   "定义基于稳定的刷新 所需要参数。
     LS_STABLE-ROW = 'X'.    "行固定
     LS_STABLE-COL = 'X'.   "列固定

     IF E_FIELDNAME = 'ZXM'.   "又是小写的问题,一定要注意
       PERFORM F4_HELP_ZXM USING E_FIELDNAME    "字段名称
                                 ES_ROW_NO       "行号,注意这是个结构
                                 ER_EVENT_DATA .    "事件数据
     ENDIF.

****使用类:CL_GUI_ALV_GRID的实例的方法,稳定刷新
     CALL METHOD GO_GRID->REFRESH_TABLE_DISPLAY
       EXPORTING
         IS_STABLE = LS_STABLE.
   ENDMETHOD.
 ENDCLASS.

*&---------------------------------------------------------------------*
*& Form F4_HELP_ZXM
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> E_FIELDNAME
*&      --> ES_ROW_NO
*&      --> ER_EVENT_DATA
*&---------------------------------------------------------------------*
 FORM F4_HELP_ZXM  USING    P_E_FIELDNAME   TYPE LVC_FNAME   "输入的: 列名称
                            P_ES_ROW_NO     TYPE LVC_S_ROID   "输入的: 行号
                            P_ER_EVENT_DATA TYPE REF TO CL_ALV_EVENT_DATA.

   DATA: LT_RETURN_TAB TYPE STANDARD TABLE OF DDSHRETVAL,
         LS_RETURN_TAB TYPE DDSHRETVAL.

   READ TABLE GT_ITEM INTO GS_ITEM INDEX P_ES_ROW_NO-ROW_ID.

   SELECT ZBJ,
          ZXH,
          ZXM
     INTO TABLE @DATA(LT_F4_HELP)
     FROM ZHYT_XYXX
    WHERE ZBJ = @GS_ITEM-ZBJ.

   SORT LT_F4_HELP BY ZXH.

   CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
     EXPORTING
       RETFIELD         = 'ZXM' "搜索帮助表:GT_F4_HELP 里的字段:ZXM
       DYNPPROG         = SY-REPID    " 当前程序
       DYNPNR           = SY-DYNNR     "当前屏幕号
       VALUE_ORG        = 'S'           "默认为C但是此处不用S不行
       CALLBACK_PROGRAM = SY-REPID     "回诘程序,也为当前程序
     TABLES
       VALUE_TAB        = LT_F4_HELP   " F4 搜索帮助表 (说明:函数对这个表参照类型并未做规定,但返回表的类型是定死的为:DDSHRETVAL
       RETURN_TAB       = LT_RETURN_TAB   "选择后,返回表值:已经选的值。准备用来回写到ALV表对应字段上
     EXCEPTIONS
       PARAMETER_ERROR  = 1
       NO_VALUES_FOUND  = 2
       OTHERS           = 3.
   IF SY-SUBRC = 0.
     READ TABLE LT_RETURN_TAB INTO LS_RETURN_TAB INDEX 1.
     IF LS_RETURN_TAB-FIELDVAL IS NOT INITIAL.
       GS_ITEM-ZXM = LS_RETURN_TAB-FIELDVAL.
     ENDIF.

     MODIFY GT_ITEM FROM GS_ITEM INDEX P_ES_ROW_NO-ROW_ID TRANSPORTING ZXM. "注意工作区内唯一发生改变的是PLANETYPE 字段

****X表示事件已经处理,不会去调取系统标准搜索帮助
     P_ER_EVENT_DATA->M_EVENT_HANDLED = 'X'.
   ENDIF.
 ENDFORM.

你可能感兴趣的:(SAP_ABAP,开发语言)