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: 

OOPS ALV: class for user command

Former Member
0 Kudos

Hi experts,

I have a screen into which an ALV Grid is embedded. At the screen, there is a user command, say, 'SCREEN_COMD'; at the ALV Grid, there is another one, named 'ALV_COMD' which perform the same action as what screen_comd1 does. If the user press 'ALV_COMD', 'SCREEN_COMD' should also be triggered. Would please tell me which class method can achieve this?

BR,

ts

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi T S,

    Please find the below code:

Say for eg: I have two toolbar say, SAVE and DOWNLOAD. On pressing Save, the entries should get saved and ask for a pop up to download the same entries into an excel sheet. Similarly if the user presses Download, the same functionality of downloading the entries into an excel sheet should be done.

CLASS alv DEFINITION.

   PUBLIC SECTION.

        handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

                             IMPORTING e_object e_interactive,

        handle_user_command FOR EVENT user_command OF cl_gui_alv_grid

                                           IMPORTING e_ucomm.

ENDCLASS.

CLASS alv IMPLEMENTATION.

METHOD handle_toolbar.

     DATA: lx_toolbar TYPE stb_button.

     CLEAR: lx_toolbar.

     lx_toolbar-function  = 'SAVE'.

     lx_toolbar-butn_type = 0.

     lx_toolbar-text      = 'Save'.

     APPEND lx_toolbar TO e_object->mt_toolbar.

     CLEAR: lx_toolbar.

     lx_toolbar-function  = 'DOWNLOAD'.

     lx_toolbar-butn_type = 0.

     lx_toolbar-text      = 'Download'.

     APPEND lx_toolbar TO e_object->mt_toolbar.

     CLEAR: lx_toolbar.

   ENDMETHOD.

  

  METHOD handle_user_command.

     DATA: v_answer TYPE c.

     CASE e_ucomm.

       WHEN 'SAVE'.

           The code for saving the entries should be written and then the

            code for downloading the same to excel should be written.

        

        WHEN 'DOWNLOAD'.

            Here the code for downloading into excel should be written.

ENDCLASS.

START-OF-SELECTION.

DATA: o_alv TYPE REF TO alv.

   CREATE OBJECT o_alv.

In the PBO write the code for displaying the ALV.

Call the Set handler method for triggering the HANDLE USER COMMAND method.

MODULE status_9000 OUTPUT.


  SET HANDLER o_alv->handle_toolbar FOR o_grid.

   CALL METHOD o_grid->set_table_for_first_display

*    EXPORTING

*      i_buffer_active               =

*      i_bypassing_buffer            =

*      i_consistency_check           =

*      i_structure_name              =

     CHANGING

       it_outtab                     = t_vbak

       it_fieldcatalog               = t_fieldcat

     EXCEPTIONS

       invalid_parameter_combination = 1

       program_error                 = 2

       too_many_lines                = 3

       OTHERS                        = 4.

   IF sy-subrc <> 0.

*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

   ENDIF.

   SET HANDLER o_alv->handle_user_command FOR o_grid.


ENDMODULE.                 " STATUS_9000  OUTPUT


Thank You.

8 REPLIES 8

sandeep_ramesh88
Explorer
0 Kudos

Hi,

Please find the screenshot below:

You should create a custom class in your program with a method say "HANDLE_USER_COMMAND". the method needs to be declared as per the event in the above screenshot.

Eg: HANDLE_USER_COMMAND FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID. For the parameters to be passed, refer the parameters of the event "USER_COMMAND".

After all the above steps, implement the method HANDLE_USER_COMMAND inside the class implementation.

METHOD HANDLE_USER_COMMAND

CASE E_UCOMM.  "E_UCOMM is the parameter of the event USER_COMMAND.

WHEN 'Your Function Code".

""Do the needful code as per the requirement.

ENDCASE.

ENDMETHOD.

Thank You.

0 Kudos

Hi Sandeep Ramesh,

Thank you for you reply but it's not what I want. Actually, I want the ALV Grid user command to trigger PAI command.

BR,

ts

Former Member
0 Kudos

Hi T S,

    Please find the below code:

Say for eg: I have two toolbar say, SAVE and DOWNLOAD. On pressing Save, the entries should get saved and ask for a pop up to download the same entries into an excel sheet. Similarly if the user presses Download, the same functionality of downloading the entries into an excel sheet should be done.

CLASS alv DEFINITION.

   PUBLIC SECTION.

        handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

                             IMPORTING e_object e_interactive,

        handle_user_command FOR EVENT user_command OF cl_gui_alv_grid

                                           IMPORTING e_ucomm.

ENDCLASS.

CLASS alv IMPLEMENTATION.

METHOD handle_toolbar.

     DATA: lx_toolbar TYPE stb_button.

     CLEAR: lx_toolbar.

     lx_toolbar-function  = 'SAVE'.

     lx_toolbar-butn_type = 0.

     lx_toolbar-text      = 'Save'.

     APPEND lx_toolbar TO e_object->mt_toolbar.

     CLEAR: lx_toolbar.

     lx_toolbar-function  = 'DOWNLOAD'.

     lx_toolbar-butn_type = 0.

     lx_toolbar-text      = 'Download'.

     APPEND lx_toolbar TO e_object->mt_toolbar.

     CLEAR: lx_toolbar.

   ENDMETHOD.

  

  METHOD handle_user_command.

     DATA: v_answer TYPE c.

     CASE e_ucomm.

       WHEN 'SAVE'.

           The code for saving the entries should be written and then the

            code for downloading the same to excel should be written.

        

        WHEN 'DOWNLOAD'.

            Here the code for downloading into excel should be written.

ENDCLASS.

START-OF-SELECTION.

DATA: o_alv TYPE REF TO alv.

   CREATE OBJECT o_alv.

In the PBO write the code for displaying the ALV.

Call the Set handler method for triggering the HANDLE USER COMMAND method.

MODULE status_9000 OUTPUT.


  SET HANDLER o_alv->handle_toolbar FOR o_grid.

   CALL METHOD o_grid->set_table_for_first_display

*    EXPORTING

*      i_buffer_active               =

*      i_bypassing_buffer            =

*      i_consistency_check           =

*      i_structure_name              =

     CHANGING

       it_outtab                     = t_vbak

       it_fieldcatalog               = t_fieldcat

     EXCEPTIONS

       invalid_parameter_combination = 1

       program_error                 = 2

       too_many_lines                = 3

       OTHERS                        = 4.

   IF sy-subrc <> 0.

*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

   ENDIF.

   SET HANDLER o_alv->handle_user_command FOR o_grid.


ENDMODULE.                 " STATUS_9000  OUTPUT


Thank You.

0 Kudos

Hi Sindhu Shanmugasundaram,

Thank you for your reply. To take your reply as an example, my situation is SAVE is on screen toolbar; and DOWNLOAD is embed into ALV Grid toolbar. When I press DOWNLOAD, the codes under SAVE should also be implemented as well as the code under DOWNLOAD. I don't want to repeat the same codes at two places

Best regards,

ts

0 Kudos

Hi TS,

Try like this:

When you press download, write the following code in your user command:

METHOD handle_user_command.

CASE e_ucomm.

   WHEN 'DOWNLOAD'.

         CALL METHOD cl_gui_cfw=>set_new_ok_code

           EXPORTING

             new_code = 'SAVE'.

         CALL METHOD cl_gui_cfw=>flush( ).

ENDCASE.

ENDMETHOD.

This will set your sy-ucomm value to 'SAVE'. Hence now in your PAI, you can write the code under 'SAVE'.

Thank You.

Former Member
0 Kudos

Hi,

Register ON_USERCOMMAND_EVENT.

then in this reg method implementation for SCREEN_COMD event we can set OK CODE as ALV_COMD using

CALL METHOD cl_gui_cfw=>set_new_ok_code

         EXPORTING

           new_code = ALV_COMD.

Regards,

Praveen

rosenberg_eitan
Active Contributor
0 Kudos

Hi ,

Please look at program BCALV_GRID_05:

From BCALV_GRID_05:

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* Demonstrate the creation of an own toolbar button.
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* The report shows a list of flights of one airline.
* Select one or more lines and press the 'Detail'-Button to popup
* a dialog window with related bookings.
*-------------------------------------------------------------------
* Essential steps (Search for '§')
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 1.Apply steps for event handling for events TOOLBAR and
*   USER_COMMAND (see example for print events)
* 2.In event handler method for event TOOLBAR: Append own functions
*   by using event parameter E_OBJECT.
* 3.In event handler method for event USER_COMMAND: Query your
*   function codes defined in step 2 and react accordingly.
* 4.Call method 'set_toolbar_interactive' to raise event TOOLBAR.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Regards.

rosenberg_eitan
Active Contributor
0 Kudos

Hi ,

It is also possible to override the standard toolbar .

So you can add your own code  .

From "An Easy Reference for ALV Grid Control.pdf"

It is available on the web.

"  The ALV Grid control also gives the opportunity to override its standard functions. For this purpose,

we utilize the event “before_user_command” and the method which sets ALV user command, called

“set_user_command”.  At  “before_user_command”,  you  control  the  user  command  to  process  your

own  function  and  then  use  the  method  “set_user_command”  to  set  the  ALV  Grid  user  command  to

space to avoid ALV Grid execute further with the standard function. "

regards.