function ALV 獲取OO ALV event ID

Lan_dev發表於2024-04-10

SAP ABAP ALV(LVC)的一個自定義事件(F4幫助事件,回車ENTER按鈕事件)的一個例項

https://blog.csdn.net/zhongguomao/article/details/51775112

1. 定義和註冊事件接受器類
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED
*----------------------------------------------------------------------*
*       LCL_EVENT_RECEIVER
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION DEFERRED.
DATA gv_flg  TYPE char01.
 
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
*       lcl_event_receiver的類定義:
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_modify
        FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING e_modified
                  et_good_cells.
 
    METHODS:
      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_RECEIVER DEFINITION
 
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
 
  METHOD  handle_modify.
    DATA: lv_enter    TYPE REF TO cl_gui_event,
          lv_eventid  TYPE i.
 
*   獲取當前事件
    lv_enter   = tem_grid->cur_event.
 
*   例項化成功的時候
    IF lv_enter IS NOT INITIAL.
      lv_eventid = lv_enter->eventid.
    ENDIF.
 
*   只有在[回車]事件的時候,再進行檢查(19:代表回車事件)
    IF lv_eventid = 19
   AND sy-ucomm   = ''.
 
      IF p_mmvds = 'X'.
*       設定供應商描述欄位和物料描述欄位
        PERFORM set_name1_maktx.
 
*       設定後,alv穩定重新整理
        PERFORM refresh_table_alv.
 
*       新增行專案檢查(多頻次包裝基礎表)
        PERFORM check_line_mmvds.
 
      ELSEIF p_detail = 'X'.
*       新增行專案檢查(視窗時間表)
        PERFORM check_line_dtdetail.
 
      ELSEIF p_vds = 'X'.
*       設定供應商描述欄位(供應商供貨資訊基礎表)
        PERFORM set_name1_vds.
 
*       設定後,alv穩定重新整理
        PERFORM refresh_table_alv.
 
*       新增行專案檢查(供應商供貨資訊基礎表)
        PERFORM check_line_vds.
      ENDIF.
 
    ENDIF.
 
*   設定後,alv穩定重新整理
    PERFORM refresh_table_alv.
 
  ENDMETHOD.                    "HANDLE_MODIFY
 
 
  METHOD  handle_f4.
*   供應商供貨資訊基礎表的情況
    IF p_vds = 'X'.
*     視窗時間引數的自定義f4檢索幫助
      PERFORM f4_help_zsjcs USING e_fieldname
                                  es_row_no.
 
*     設定後,alv穩定重新整理
      PERFORM refresh_table_alv.
 
    ENDIF.
 
  ENDMETHOD.                    "HANDLE_F4
 
ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION
2. 呼叫alv函式

**EVENT LAYOUT
  DATA: ls_events     TYPE slis_alv_event,
        lt_event      TYPE slis_t_event,
        ls_layout     TYPE lvc_s_layo.
 
  ls_events-name = 'CALLER_EXIT'.
  ls_events-form = 'FM_BUTTON'.
  APPEND ls_events TO lt_event.
 
  ls_layout-stylefname = 'FIELD_STYL'.
  ls_layout-zebra      = 'X'.
  ls_layout-box_fname  = 'CHK_BOX'.
  ls_layout-cwidth_opt = 'X'.
 
 
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PF_STATUS_SET'
      i_callback_user_command  = 'USER_COMMAND'
      is_layout_lvc            = ls_layout
      it_fieldcat_lvc          = gt_fieldcat_lvc
      i_save                   = 'X'
      it_events                = lt_event
    TABLES
      t_outtab                 = pt_outdata
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
3. 定義是事件子程式
*&---------------------------------------------------------------------*
*&      Form  FM_BUTTON
*&---------------------------------------------------------------------*
*       設定enter事件
*----------------------------------------------------------------------*
*      -->E_GRID     text
*----------------------------------------------------------------------*
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
  DATA: lv_event_receiver TYPE REF TO lcl_event_receiver,
        lt_f4             TYPE lvc_t_f4,
        ls_f4             TYPE lvc_s_f4.
 
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = tem_grid.
 
* 設定enter事件
  CALL METHOD tem_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
 
  CREATE OBJECT lv_event_receiver.
  SET HANDLER lv_event_receiver->handle_modify FOR tem_grid.
 
 
* 設定f4事件
  CHECK gv_flg IS INITIAL.
 
  gv_flg = 'X'.
 
  ls_f4-fieldname  = 'ZSJCS'.   "視窗時間引數(需要定義F4幫助按鈕的欄位)
  ls_f4-register   = 'X'.
  ls_f4-getbefore  = 'X'.
  ls_f4-chngeafter = 'X'.
  INSERT ls_f4 INTO TABLE lt_f4.
 
  CREATE OBJECT lv_event_receiver.
  SET HANDLER lv_event_receiver->handle_f4 FOR tem_grid.
 
  CALL METHOD tem_grid->register_f4_for_fields
    EXPORTING
      it_f4 = lt_f4[].
 
ENDFORM.                    "FM_BUTTON
4. 定義alv-status子程式
*&---------------------------------------------------------------------*
*&      Form  PF_STATUS_SET
*&---------------------------------------------------------------------*
*       status設定
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM pf_status_set USING rt_extab TYPE slis_t_extab.

  SET PF-STATUS 'STATUS_ALV'.

ENDFORM.                    "PF_STATUS_SET
5. 定義alv-comman子程式
*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       USER COMMMAND
*----------------------------------------------------------------------*
*      -->P_UCOMM      text
*      -->PS_SELFIELD  text
*----------------------------------------------------------------------*
FORM user_command USING ucomm    LIKE sy-ucomm
                        selfield TYPE slis_selfield.
 
  DATA l_grid TYPE REF TO cl_gui_alv_grid.
*------------將ALV上修改的內容儲存到內表---------------------------------*
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = l_grid.
  CALL METHOD l_grid->check_changed_data.
 
 
  CASE ucomm.
*   [行專案追加]按鈕
    WHEN 'CREATE'.
*     多頻次包裝基礎表
      IF p_mmvds = 'X'.
*       新增行專案(多頻次包裝基礎表)
        PERFORM cteate_line_mmvds.
 
*     視窗時間基礎表
      ELSEIF p_detail = 'X'.
*       新增行專案(視窗時間基礎表)
        PERFORM cteate_line_dtdetail.
 
*     供應商供貨資訊基礎表
      ELSEIF p_vds = 'X'.
*       新增行專案(供應商供貨資訊基礎表)
        PERFORM cteate_line_vds.
 
      ENDIF.
 
      selfield-refresh = 'X'.      "將更新後內表的資料反饋到alv畫面,自動重新整理功能
 
*   [行專案刪除]按鈕
    WHEN 'DELETE'.
*     多頻次包裝基礎表
      IF p_mmvds = 'X'.
*       刪除行專案(多頻次包裝基礎表)
        PERFORM delete_line_mmvds.
 
*     視窗時間基礎表
      ELSEIF p_detail = 'X'.
*       刪除行專案(視窗時間基礎表)
        PERFORM delete_line_dtdetail.
 
*     供應商供貨資訊基礎表
      ELSEIF p_vds = 'X'.
*       刪除行專案(供應商供貨資訊基礎表)
        PERFORM delete_line_vds.
 
      ENDIF.
 
      selfield-refresh = 'X'. "將更新後內表的資料反饋到alv畫面,自動重新整理功能
 
*   [儲存]按鈕
    WHEN 'SAVE'.
*     多頻次包裝基礎表
      IF p_mmvds = 'X'.
*       儲存處理(多頻次包裝基礎表)
        PERFORM save_data_mmvds.
 
*     視窗時間基礎表
      ELSEIF p_detail = 'X'.
*       儲存處理(視窗時間基礎表)
        PERFORM save_data_dtdetail.
 
*     供應商供貨資訊基礎表
      ELSEIF p_vds = 'X'.
*       儲存處理(供應商供貨資訊基礎表)
        PERFORM save_data_vds.
 
      ENDIF.
 
  ENDCASE.
 
*------------將ALV上修改的內容儲存到內表---------------------------------*
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = l_grid.
  CALL METHOD l_grid->check_changed_data.
 
ENDFORM.                    "USER_COMMAND
6. 其他子程式定義
*&---------------------------------------------------------------------*
*&      Form  REFRESH_TABLE_ALV
*&---------------------------------------------------------------------*
*       設定後,alv穩定重新整理
*----------------------------------------------------------------------*
FORM refresh_table_alv .
  DATA: stbl        TYPE lvc_s_stbl.
*
  stbl-row = 'X'." 基於行的穩定重新整理
  stbl-col = 'X'." 基於列穩定重新整理
  CALL METHOD tem_grid->refresh_table_display
    EXPORTING
      is_stable = stbl.

ENDFORM.                    " REFRESH_TABLE_ALV

*&---------------------------------------------------------------------*
*&      Form  F4_HELP_ZSJCS
*&---------------------------------------------------------------------*
*       視窗時間引數的自定義f4檢索幫助
*----------------------------------------------------------------------*
FORM f4_help_zsjcs USING p_fieldname  TYPE lvc_fname
                         p_row_no     TYPE lvc_s_roid.
  DATA: lt_dtdetail TYPE STANDARD TABLE OF ztpp_dtdetail,
        lt_return   TYPE STANDARD TABLE OF ddshretval,
        ls_return   TYPE ddshretval.
 
  CLEAR gw_vds.
  READ TABLE gt_vds INTO gw_vds INDEX p_row_no-row_id.
 
  IF p_fieldname = 'ZSJCS'.
 
    SELECT *
      INTO TABLE lt_dtdetail
      FROM ztpp_dtdetail
     WHERE werks = gw_vds-werks.
 
    SORT lt_dtdetail BY werks zsjcs.
    DELETE ADJACENT DUPLICATES FROM lt_dtdetail
                          COMPARING werks zsjcs.
 
  ENDIF.
 
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield         = 'ZSJCS'            "lt內表裡面的欄位
      dynpprog         = sy-repid
      dynpnr           = sy-dynnr
      dynprofield      = 'ZSJCS'            "畫面上繫結欄位
      value_org        = 'S'
      callback_program = sy-repid
    TABLES
      value_tab        = lt_dtdetail        "需要顯示幫助的值內表
      return_tab       = lt_return          "返回值
    EXCEPTIONS
      parameter_error  = 1
      no_values_found  = 2
      OTHERS           = 3.
 
 
  IF sy-subrc = 0.
    READ TABLE lt_return INTO ls_return INDEX 1.
    gw_vds-zsjcs = ls_return-fieldval.
    MODIFY gt_vds FROM gw_vds INDEX p_row_no-row_id
                       TRANSPORTING zsjcs.
  ENDIF.
 
ENDFORM.                    " F4_HELP_ZSJCS

相關文章