matinal:SAP ABAP ALV可编辑,搜索帮助的实现

2023-10-14 15:35:39 浏览数 (2)

步骤:

1.设置Fieldcat目录LGORT字段的可编辑及F4属性。
代码语言:javascript复制
CLEAR gs_fieldcat.

gs_fieldcat-edit = 'X'.        "ALV字段可编辑"

gs_fieldcat-f4availabl = 'X'.  "ALV字段F4帮助"

MODIFY gt_fieldcat FROM gs_fieldcat TRANSPORTING edit f4availabl

WHERE fieldname = 'LGORT' .
2.定义需要的参数变量
代码语言:javascript复制
DATA: gt_event    TYPE slis_t_event.     "ALV事件参数"

DATA: stbl        TYPE lvc_s_stbl.       "ALV屏幕刷新参数

DATA: g_grid      TYPE REF TO cl_gui_alv_grid.    "ALV grid对象
3.定义ALV实例类及F4方法
代码语言:javascript复制
*----------------------------------------------------------------------*

*       CLASS lcl_event_handler DEFINITION

*----------------------------------------------------------------------*

*       类定义 && F4方法声明

*----------------------------------------------------------------------*

CLASS lcl_event_handler DEFINITION."类定义

  PUBLIC SECTION.

    METHODS:

*     F4搜索帮助事件

      handle_f4 FOR EVENT onf4 OF cl_gui_alv_grid

        IMPORTING e_fieldname

                  es_row_no

                  er_event_data

                  et_bad_cells.

ENDCLASS. "lcl_event_handler DEFINITION


*----------------------------------------------------------------------*

*       CLASS lcl_event_handler IMPLEMENTATION

*----------------------------------------------------------------------*

*       类实现 && F4方法实现

*----------------------------------------------------------------------*

CLASS lcl_event_handler IMPLEMENTATION."类实现

  METHOD:handle_f4.

*     窗口时间参数的自定义f4检索帮助

    CASE e_fieldname.

      WHEN 'LGORT'.   "内表字段名称

        PERFORM f4_help_lgort USING e_fieldname

                                    es_row_no.

        er_event_data->m_event_handled = 'X'.

    ENDCASE.

*     设置后,alv稳定刷新

    stbl-row = 'X'." 基于行的稳定刷新

    stbl-col = 'X'." 基于列稳定刷新

    CALL METHOD g_grid->refresh_table_display

      EXPORTING

        is_stable = stbl.

  ENDMETHOD.                    "HANDLE_F4

ENDCLASS. "lcl_event_handler IMPLEMENTATION
4.注册事件
代码语言:javascript复制
*&---------------------------------------------------------------------*

*& Form frm_display_alv

*&---------------------------------------------------------------------*

*& ALV展示函数

*&---------------------------------------------------------------------*

FORM frm_display_alv .

* 在调用ALV函数之前,将事件注册到事件参数里面。

  APPEND VALUE #( name = 'CALLER_EXIT' form = 'FRM_REGISTER_EVENTS' ) TO gt_event.

    

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'

    EXPORTING

      i_callback_program       = sy-repid

      i_callback_pf_status_set = 'SET_PF_STATUS'

      i_callback_user_command  = 'ALV_USER_COMMAND'

      is_layout_lvc            = gs_layout

      it_fieldcat_lvc          = gt_fieldcat

      it_events                = gt_event

      i_save                   = 'A'

    TABLES

      t_outtab                 = gt_output

    EXCEPTIONS

      program_error            = 1

      OTHERS                   = 2.


ENDFORM.
5.将事件与处理方法绑定
代码语言:javascript复制
*&---------------------------------------------------------------------*

*&      Form  register_events

*&---------------------------------------------------------------------*

*       注册事件

*----------------------------------------------------------------------

FORM frm_register_events USING e_grid TYPE slis_data_caller_exit.

  DATA:lt_f4 TYPE  lvc_t_f4,

       ls_f4 TYPE  lvc_s_f4.


  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'

    IMPORTING

      e_grid = g_grid.

*

  DATA: gr_event_handler TYPE REF TO lcl_event_handler.

  CREATE OBJECT gr_event_handler.

  

* 如果有多个字段需要搜素帮助,以同样方式插入数据      

  ls_f4-fieldname = 'LGORT'.  

  ls_f4-register   = 'X'.

  ls_f4-getbefore  = 'X'.

  ls_f4-chngeafter = 'X'.

  INSERT ls_f4 INTO TABLE lt_f4.


* 注册F4帮助事件

  SET HANDLER gr_event_handler->handle_f4 FOR g_grid.

  CALL METHOD g_grid->register_f4_for_fields

    EXPORTING

      it_f4 = lt_f4.

ENDFORM. "register_events
6.实现F4子程序块
代码语言:javascript复制
*&---------------------------------------------------------------------*

*& Form f4_help_route

*&---------------------------------------------------------------------*

*& ROUTE搜索帮助

*&---------------------------------------------------------------------*

FORM f4_help_route  USING   p_fieldname TYPE lvc_fname

                             p_row_no TYPE lvc_s_roid.

  DATA: lt_return TYPE STANDARD TABLE OF ddshretval,

        ls_return TYPE ddshretval.

* 获取ALV上点击的行数据

  READ TABLE gt_output ASSIGNING <fs_opt> INDEX p_row_no-row_id.

            

  IF p_fieldname = 'LGORT'.

    SELECT lgort,lgobe INTO TABLE @DATA(lt_t001l) FROM t001l .

    SORT lt_t001l BY lgort.

    CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'

      EXPORTING

        retfield        = 'LGORT'            "筛选内表里面的字段

        dynpprog        = sy-repid

        dynpnr          = sy-dynnr

        dynprofield     = 'GS_OUTPUT-LGORT'            "ALV内表字段

        value_org       = 'S'

  "     CALLBACK_PROGRAM = SY-REPID

      TABLES

        value_tab       = lt_t001l        "需要显示帮助的值内表

        return_tab      = lt_return          "返回值

      EXCEPTIONS

        parameter_error = 1

        no_values_found = 2

        OTHERS          = 3.

* 将搜索帮助选中的返回值写到ALV上

    IF sy-subrc = 0.

      READ TABLE lt_return INTO ls_return INDEX 1.

      IF ls_return-fieldval IS NOT INITIAL .

        <fs_opt>-lgort = ls_return-fieldval.

      ENDIF.

    ENDIF.

  ENDIF.


ENDFORM.

0 人点赞