Vous êtes sur la page 1sur 4

Auto Refresh ALV List - CL_GUI_TIMER

Class CL_GUI_TIMER trigger event FINISHED at given intervals (in seconds). This event can be used to
select the data, carry out processing and update the content internal table attached to ALV. Below
sample code illustrate how to use this class to refresh ALV. To summarise the program logic in short,
you need to define object of CL_GUI_ALV_GRID, a local class to handle event FINISHED, couple this
event method defined in local class, specify time interval in seconds and kick start the timer.

Note that I have used SELECT.. APPENDING TABLE to show change in data after refresh. And I think it is
good idea to inform user about last time report was refresh with MESSAGE statement.

REPORT

zpwtest8.

CLASS lcl_timer DEFINITION DEFERRED .

DATA : i_spfli TYPE TABLE OF spfli .

DATA : ob_grid

TYPE REF TO cl_gui_alv_grid ,

ob_recev

TYPE REF TO lcl_timer

ob_timer

TYPE REF TO cl_gui_timer

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

CLASS lcl_receiver DEFINITION

*----------------------------------------------------------------------*
CLASS lcl_timer DEFINITION.
PUBLIC SECTION.
METHODS:
handle_finished FOR EVENT finished OF cl_gui_timer.
ENDCLASS.

"lcl_receiver DEFINITION

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

CLASS lcl_receiver IMPLEMENTATION

*----------------------------------------------------------------------*
CLASS lcl_timer IMPLEMENTATION.
METHOD handle_finished.
PERFORM refresh_data .
MESSAGE s003(z001) WITH 'Report refreshed at' sy-uzeit.

CALL METHOD ob_timer->run.


ENDMETHOD.

"handle_finished

ENDCLASS.

"lcl_receiver IMPLEMENTATION

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

Start of Selection

*--------------------------------------------------------------------*
START-OF-SELECTION .
PERFORM select_data .

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

End of Selection

*--------------------------------------------------------------------*
END-OF-SELECTION .

* Step 1 : Initialise Time control


CREATE OBJECT ob_timer.

* Step 2 : Initialise object to receive event


CREATE OBJECT ob_recev.

* Step 3 : Couple event to method


SET HANDLER ob_recev->handle_finished FOR ob_timer. "

* Step 4 : Set Interval in seconds


ob_timer->interval = 20 .

* Step 5 : Start Timer


CALL METHOD ob_timer->run.

PERFORM display_data .

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

Form

Select_Date

*--------------------------------------------------------------------*
FORM select_data .

SELECT *
APPENDING TABLE i_spfli
FROM spfli .
ENDFORM.

"SELECT_DATA

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

Form

refresh_data

*&---------------------------------------------------------------------*
FORM refresh_data .

* Get grid reference


IF ob_grid IS INITIAL .
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ob_grid.
ENDIF.

IF ob_grid IS NOT INITIAL. "Sanity Test


PERFORM select_data .
CALL METHOD ob_grid->refresh_table_display.
ENDIF.

ENDFORM.

"refresh_data

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

Form

display_data

*&---------------------------------------------------------------------*
FORM display_data .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'


EXPORTING
i_structure_name = 'SPFLI'
TABLES
t_outtab

= i_spfli

EXCEPTIONS
program_error

= 1

OTHERS

= 2.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM .

"display_data

Vous aimerez peut-être aussi