SAP:SALV重新整理按鈕事件

samrv發表於2024-06-13

SAP SALV重新整理事件

1、主程式

*&---------------------------------------------------------------------*
*& Report  Z17_04
*&
*&---------------------------------------------------------------------*
*& 全屏Classic List模式
*& LIST_DISPLAY : 用SALV顯示與傳統式REUSE_ALV_LIST_DISPLAY函式輸出ALV
*& 一樣效果的報表。
*& 36、USER-COMMAND事件是追加在TOOLBAR事件中追加的圖示功能的事件。追加當
*&   單擊REFRESS按鈕時從表SFLIGHT中取得10件資料的邏輯。
*& 問題: 沒有搞懂類的定義、實現與子程式之間的順序 。
*&---------------------------------------------------------------------*

REPORT Z17_36.



DATA: gt_itab type table of sflight.
DATA: gr_table TYPE REF TO CL_SALV_TABLE.
DATA: gr_function type ref to cl_salv_functions_list." 應用工具欄
DATA: gr_container type ref to cl_gui_custom_container.
DATA: OK_CODE type syucomm.




SELECT * FROM  SFLIGHT INTO CORRESPONDING FIELDS OF TABLE
  gt_itab up to 25 rows.

*& #REGION: 用 螢幕100代替
*cl_salv_table=>factory(
*  EXPORTING
*    list_display = abap_true
*  IMPORTING
*    r_salv_table = gr_table
*  CHANGING
*    t_table = gt_itab
*      ).

*& #region:  應用工具欄
*gr_function = gr_table->get_functions( ).
*gr_function->set_default( ).
*gr_function->set_all( abap_true ).
*& #endregion:  應用工具欄
*


*gr_table->display( ).
*& #ENDREGION: 用 螢幕100代替

*& 呼叫自定義的螢幕100
CALL SCREEN 0100.



*&---------------------------------------------------------------------*
*& CLASS lcl_handle_events DEFINTION
*&---------------------------------------------------------------------*
CLASS lcl_handle_events DEFINITION.
  PUBLIC SECTION.
    methods:
    on_user_command FOR EVENT added_function of cl_salv_events
    IMPORTING e_salv_function.
ENDCLASS. " lcl_handle_events DEFINITION

*&---------------------------------------------------------------------*
*& CLASS lcl_handle_event IMPLEMENTATION
*&---------------------------------------------------------------------*
CLASS lcl_handle_events IMPLEMENTATION.
  METHOD on_user_command.
    PERFORM handle_user_command USING e_salv_function.
  ENDMETHOD. " on_user_command
ENDCLASS. " lcl_handle_events IMPLEMENTATION

DATA: gr_event type ref to lcl_handle_events.

data: lr_event TYPE REF TO cl_salv_events_table.  " 定義事件變數





*& 螢幕輸出後事件 子程式
INCLUDE Z17_36_PBO.
*INCLUDE Z17_35_PBO.
*INCLUDE z17_04_pbo.

*& 螢幕的輸入前事件 子程式
INCLUDE Z17_36_PAI.
*INCLUDE Z17_35_PAI.
*INCLUDE z17_04_pai.



*&---------------------------------------------------------------------*
*&  FORM handle_user_command
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
FORM handle_user_command USING p_function type salv_de_function.
  CASE p_function .
    when 'REFRESH'.
      perform refresh.
    WHEN OTHERS.
  ENDCASE.

ENDFORM. " handle__user_command.
*&---------------------------------------------------------------------*
*& FORM REFRESH 重新整理事件
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
FORM  Refresh.
  SELECT * FROM SFLIGHT
    INTO CORRESPONDING FIELDS OF TABLE gt_itab
    UP TO 10 rows.

  gr_table->refresh( ).
ENDFORM. " REFRESH FORM.

  

2、螢幕邏輯流

PROCESS BEFORE OUTPUT.
* MODULE STATUS_0100.
  MODULE 0100_PBO.
*
PROCESS AFTER INPUT.
* MODULE USER_COMMAND_0100.
  MODULE 0100_PAI.

  

3、 PAI 子程式

*----------------------------------------------------------------------*
***INCLUDE Z17_36_PAI.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Module  0100_PAI  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE 0100_PAI INPUT.
  CASE OK_CODE.
    WHEN 'BACK' OR 'EXIT' OR  'CANC'.
      SET SCREEN 0.
      LEAVE SCREEN.
  ENDCASE.
ENDMODULE.                 " 0100_PAI  INPUT

  

4、 PBO子程式

*----------------------------------------------------------------------*
***INCLUDE Z17_36_PBO.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Module  0100_PBO  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE 0100_PBO OUTPUT.
  SET PF-STATUS '0100'.

  IF gr_container is not bound.


*& 1. Create Container
    CREATE OBJECT gr_container
     exporting
       container_name = 'CONTAINER'.

*& 2. CREATE ALV
    cl_salv_table=>factory(
      exporting
        r_container = gr_container
        container_name = 'CONTAINER'
      IMPORTING
        r_salv_table = gr_table
      CHANGING
        t_table = gt_itab
    ).

*& 3. SET ALV BUTTON
    gr_function = gr_table->get_functions( ).
    gr_function->set_all( abap_true ). " 追加ALV所有功能

*& 3.1
*& #region: 追加重新整理按鈕
    INCLUDE <icon>.
    DATA: l_icon TYPE string .
    l_icon = icon_refresh.

    gr_function->add_function(
    name = 'REFRESH'
    icon = l_icon
    text = 'REFRESH'
    tooltip = 'REFRESH'
    position = if_salv_c_function_position=>right_of_salv_functions
    ).
*&
*& #endregion: 追加重新整理按鈕

*& 3.2
*& #REGION: 註冊事件
lr_event = gr_table->get_event( ).
Create object gr_event .
SET HANDLER gr_event->on_user_command for lr_event .
*& #ENDREGION: 註冊事件


*& 4. Call alv
    gr_table->display( ).
  ENDIF.
ENDMODULE.                 " 0100_PBO  OUTPUT

  效果圖:

相關文章