[ZT]ABAP:利用SAP定时器自动刷新ALV

http://www.cublog.cn/u2/86821/showart_2055218.html

曾于无意之中发现,SAP系统中有个名为CL_GUI_TIMER的类,测试下来发现还是挺好用的,比网上那些自动刷新的例子直观多了。这里我们就做个演示实例。
先做好准备工作:建立程序z04_gui_timer及其屏幕0100,屏幕上画个CUSTOM CONTAINER作为ALV的容器。顺便可以画个退出按钮用于退出界面,这样就省得SET PF-STATUS了。
下面就是我们的程序代码了,其实还是挺简单的:
REPORT  z04_gui_timer.
CLASS lcl_event_handler DEFINITION DEFERRED.
DATA: ok_code LIKE sy-ucomm,
      go_timer TYPE REF TO cl_gui_timer,
      go_evt_hndl TYPE REF TO lcl_event_handler.
DATA: gt_spfli TYPE TABLE OF spfli,
      go_grid TYPE REF TO cl_gui_alv_grid,
      go_container TYPE REF TO cl_gui_custom_container.
START-OF-SELECTION.
  CALL SCREEN 0100.
****** Module  status_9000  OUTPUT
MODULE status_0100 OUTPUT.
  DATA: g_init TYPE flag.
  CLEAR ok_code.
  IF g_init IS INITIAL.
    g_init = 'X'.
    PERFORM init_0100.
  ENDIF.
ENDMODULE.                 " status_0100  OUTPUT
****** Module user_command_0100 INPUT
MODULE user_command_0100 INPUT.
  CASE ok_code.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.                 " user_command_0100  INPUT
****** CLASS lcl_event_handler DEFINITION
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_timer FOR EVENT finished OF cl_gui_timer.
ENDCLASS.                    "lcl_event_handler DEFINITION
****** CLASS lcl_event_handler IMPLEMENTATION
CLASS lcl_event_handler IMPLEMENTATION.
  METHOD handle_timer.
    DELETE gt_spfli INDEX 1.
    CALL METHOD go_grid->refresh_table_display.
*   CALL METHOD go_timer->cancel
*     EXCEPTIONS
*       OTHERS = 9.
    CALL METHOD go_timer->run "必须重新激活定时器
      EXCEPTIONS
        OTHERS = 9.
  ENDMETHOD.                    "handle_timer
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION
****** Form  init_0100
FORM init_0100.
  SELECT * INTO TABLE gt_spfli
    FROM spfli UP TO 30 ROWS.
  CREATE OBJECT go_container
    EXPORTING
      container_name = 'CC_ALV'.
  CREATE OBJECT go_grid
    EXPORTING
      i_parent = go_container.
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'SPFLI'
    CHANGING
      it_outtab        = gt_spfli.
  CREATE OBJECT go_evt_hndl.
  CREATE OBJECT go_timer
    EXCEPTIONS
      OTHERS = 9.
  SET HANDLER go_evt_hndl->handle_timer FOR go_timer.
  go_timer->interval = 3. "设置间隔为3秒
  CALL METHOD go_timer->run
    EXCEPTIONS
      OTHERS = 9.
ENDFORM.                  " init_0100
说明:
(1) lcl_event_handler是自定义的类,用于处理cl_gui_timer的EVENT ‘finished’;
(2) 定时器的用法与VC、DELPHI有所不同,上次定时事件处理完毕后,必须CALL METHOD go_timer->run重新激活,如此循环;
(3) 定时器并不会自动触发PAI事件,如有必要则可以在handle_timer中CALL METHOD cl_gui_cfw=>set_new_ok_code来触发;
(4) 虽然我们的例子是基于screen的,但是基于List也是完全可行的。

你可能感兴趣的:(abap)