Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ALV using OOPS, How to handle events

surya_gupta3
Explorer
0 Kudos

Hi,

I need to handle EVENTS (user_command) using OOPS in ALV. Please let me know in general - how do we define event handlers for the events declared in a CLASS.

In This case, I need to handle EVENT user_command of cl_gui_alv_grid CLASS.

I am aware of the syntax:

SET HANDLER o_eventreceiver->handle_user_command FOR o_Alvgrid.

Is it necessary to have a self defined class (like one above quoted - o_eventreceiver) to set the handler? Cant I use something like -

SET HANDLER handle_user_command FOR EVENT user_command of o_Alvgrid.

Thanks & Regards,

Surya

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Yes, it is a very common practice to define a local class as the event handler class for all events.

You should follow the example in program BCALV_GRID_02.

Regards,

Rich Heilman

2 REPLIES 2

Former Member
0 Kudos

Hi Surya,

Use like this

INCLUDE <icon>.

* Predefine a local class for event handling to allow the
* declaration of a reference variable before the class is defined.
CLASS lcl_event_receiver DEFINITION DEFERRED.
class cl_gui_container definition load.

DATA : o_alvgrid          TYPE REF TO cl_gui_alv_grid,
       o_dockingcontainer TYPE REF TO cl_gui_docking_container,
       o_eventreceiver    TYPE REF TO lcl_event_receiver,

CONSTANTS: c_a(1)    TYPE c VALUE 'A',             "All Layouts
           c_x(1)    TYPE c VALUE 'X',
           c_handle  TYPE slis_handl VALUE 'V001'. "ALV Handle
CLASS lcl_event_receiver DEFINITION.

*   event receiver definitions for ALV actions
  PUBLIC SECTION.
    CLASS-METHODS:
* Status bar
       handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING e_ucomm,

* Tool bar
       handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object
                      e_interactive,
* Hot Spot Click
       handle_hotspot
         FOR EVENT hotspot_click OF cl_gui_alv_grid
            IMPORTING e_row_id
                      e_column_id
                      es_row_no,
* Row Double click for dirll down.
       handle_double_click
         FOR EVENT double_click OF cl_gui_alv_grid
            IMPORTING e_row
                      e_column
                      es_row_no.

ENDCLASS.
CLASS lcl_event_receiver IMPLEMENTATION.
*  create the relevant buttons on the toolbars
  METHOD handle_toolbar.
* This method handles the user interaction with the tool bar.
    CASE w_action.
      WHEN 'SELECT'.
*========================
        REFRESH i_toolbar.
        PERFORM f9801_toolbar_create USING: '3' '' '' '' '' '',
                                    '0' 'FCODE' icon_modify
                                     text-006 '' '',
                                     '0' 'RFBACK' icon_arrow_left
                                     text-007 '' ''.

        LOOP AT i_toolbar INTO w_toolbar.
          APPEND w_toolbar TO e_object->mt_toolbar.
        ENDLOOP.


      WHEN OTHERS.

        REFRESH i_toolbar.
        PERFORM f9801_toolbar_create USING: '3' '' '' '' '' '',
                                    '0' 'FCODE' icon_checked
                                     text-008 '' text-005.

        LOOP AT i_toolbar INTO w_toolbar.
          APPEND w_toolbar TO e_object->mt_toolbar.
        ENDLOOP.
    ENDCASE.

  ENDMETHOD.
  METHOD handle_user_command.
* In event handler method for event USER_COMMAND: Query your
*   function codes defined in step 2 and react accordingly.

    CASE e_ucomm.

      WHEN 'FCODE'.

        CALL METHOD o_alvgrid->get_selected_rows
          IMPORTING
            et_index_rows = i_selected_rows
*            ET_ROW_NO     =
WHEN OTHERS.

    ENDCASE.
  ENDMETHOD.
  METHOD handle_hotspot.
* The hotspot processing coded in the form below.
    PERFORM f9802_handle_hotspot USING e_row_id
                                      e_column_id
                                      es_row_no.

  ENDMETHOD.
METHOD handle_double_click.
* The double click drill down processing should be
* coded in the form below.
    PERFORM f9803_handle_double_click USING e_row
                                           e_column
                                           es_row_no.

  ENDMETHOD.

ENDCLASS.

IN PBO
  CREATE OBJECT o_alvgrid
     EXPORTING
       i_parent = cl_gui_container=>default_screen. "o_dockingcontainer.

* Check to see if we are runnng on online
  IF cl_gui_alv_grid=>offline( ) IS INITIAL.
* Create the event reciever
    IF o_eventreceiver IS INITIAL.
      CREATE OBJECT o_eventreceiver.
    ENDIF.
  ENDIF.
  CALL METHOD o_alvgrid->set_table_for_first_display
     EXPORTING
*    I_BYPASSING_BUFFER            =
*    I_BUFFER_ACTIVE               =
*    I_CONSISTENCY_CHECK           =
*    I_STRUCTURE_NAME              =
       is_variant                    = w_variant
       i_save                        = c_a
*    I_DEFAULT                     = 'X'
       is_layout                     = p_layout
*    IS_PRINT                      =
       it_special_groups             = p_groups[]
       it_toolbar_excluding          = p_exclude[]
*    IT_HYPERLINK                  =
*    IT_ALV_GRAPHICS               =
*    IT_EXCEPT_QINFO               =
    CHANGING
       it_outtab                     = p_output[]
       it_fieldcatalog               = p_fieldcat[]
*    IT_SORT                       =
*    IT_FILTER                     =
    EXCEPTIONS
       invalid_parameter_combination = 1
       program_error                 = 2
       too_many_lines                = 3
       OTHERS                        = 4.

  IF sy-subrc <> 0.
    MESSAGE i278.
    LEAVE LIST-PROCESSING.
  ENDIF.

IN PBO,
module STATUS_9001 output.
*enable the back and cancel buttons
  SET PF-STATUS 'Z_TEMPLATE_STATUS'.

*  SET TITLEBAR 'xxx'.

* This form create the Docking container and the ALV grid.
  perform f9000_objects_create.

* If the any of the standard toolbar functions are to be excluded
* then use the form below.

* Here the print icon will be excluded from tool bar
    perform f9100_exclude_functions using : i_Exclude[]  '&PRINT'.


* Set handlers for events
SET HANDLER o_eventreceiver->handle_hotspot       FOR o_Alvgrid.
SET HANDLER o_eventreceiver->handle_double_click  FOR o_Alvgrid.
SET HANDLER o_eventreceiver->handle_toolbar       FOR o_Alvgrid.
SET HANDLER o_eventreceiver->handle_user_command  FOR o_Alvgrid.

* Create the field catalogue for the output structure.
* Note the <structure> value below should be replaced with the
* structure for I_output as defined in the Data Dictionary.
  perform f9200_build_field_cat tables i_fieldcat
                           using 'zalv_template1'.

* Code to modify the structure should be added in here.
  perform f9300_modify_field_cat tables i_fieldcat.

* Set the Grid layout options
* Title, Zebra, Mode, Optimise Width, Display variant
  PERFORM f9400_layout using sy-title 'X' 'A' 'X' p_layout.

* generate the grid data.
  perform f9500_display_data tables i_OUTPUT
                              i_GROUPS
                              i_EXCLUDE
                              i_FIELDCAT
                        using w_layout.

endmodule.                 " STATUS_9001  OUTPUT

Hope this helps you.

You can refer teh template program which I have created.

Regards,

Judith.

Please reward points if this helps.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Yes, it is a very common practice to define a local class as the event handler class for all events.

You should follow the example in program BCALV_GRID_02.

Regards,

Rich Heilman