SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Toolbar & User command in ALV(OOPS)



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 08, 2007 12:04 am    Post subject: Toolbar & User command in ALV(OOPS) Reply with quote

Author: J.Jayanthi

Description:

This document describes how to use
toolbar
user command
in ALV using OOPS method.

This document describes the how to use the events for toolbar and user command in OOPS ALV.

Procedure
Step 1: Create object for event. Data o_event type ref to lcl_event_receiver.
Since the normal practice is to define declarations, we need to mention that lcl_event_receiver class is defined somewhere in the program. So before writing the declaration of o_event, this statement should be written.
CLASS lcl_event_receiver DEFINITION DEFERRED.
Data o_event type ref to lcl_event_receiver.
In PBO, create the object for event.
* Create the event reciever
CREATE OBJECT o_event.
Step 2: Define the class for events toolbar and user_command. Go to tcode SE24 and check the parameters tab for the events toolbar and user_command of class cl_gui_alv_grid.
CLASS lcl_event_receiver DEFINITION.
* event receiver definitions for ALV actions
PUBLIC SECTION.
METHODS:
* Tool bar
handle_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object
e_interactive,
* Status bar
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
Step 3: Write the implementation part for the defined method.
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.
perform toolbar.
APPEND w_toolbar TO e_object->mt_toolbar.
ENDMETHOD.
METHOD handle_user_command.
* In event handler method for event USER_COMMAND: Query your
* function codes defined in step 2 and react accordingly.
perform user_command using e_ucomm.
ENDMETHOD.
ENDCLASS.
For a button to appear in the toolbar, we need to pass the function code and other details as below.
form toolbar.
CLEAR w_toolbar.
MOVE : 'COUNT' TO w_toolbar-function,"Function code
'' TO w_toolbar-icon,"Name of an Icon
'' TO w_toolbar-quickinfo,"Quickinfo for an icon
'' TO w_toolbar-butn_type,"Toolbar button type
'' TO w_toolbar-disabled,"Disabled
'COUNT' TO w_toolbar-text."Text
endform. " toolbar
This user command event is handled in a such a way that once the user selects the required records and then presses the new button COUNT, the user_command will be triggered to find out the no. of rows selected.
form user_command using p_e_ucomm.
DATA :li_rows TYPE STANDARD TABLE OF lvc_s_row,
"Internal table for getting selected rows
lv_ln TYPE i."No. of rows selected
CALL METHOD o_grid->get_selected_rows
IMPORTING
et_index_rows = li_rows.
CASE p_e_ucomm.
WHEN 'COUNT'.
DESCRIBE TABLE li_rows LINES lv_ln.
MESSAGE s001(zalv) WITH 'No. of rows selected' lv_ln.
ENDCASE.
endform. " user_command
Step 4: These events should be handled in PBO.
* Handling Events
SET HANDLER o_event->handle_toolbar FOR o_grid.
SET HANDLER o_event->handle_user_command FOR o_grid.

Complete Code
Screen 9000, GUI Status ZSTATUS and GUI Title ZTITLE should be created and in Flow logic of the screen, PBO and PAI should be uncommented.

Code:
* 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.

* Data Declaration
data : itab type standard table of MARA,"Output Internal table
       i_fieldcat type standard table of lvc_s_fcat,"Field catalog
       wa type MARA,
       w_variant type disvariant,
       o_docking type ref to cl_gui_docking_container,"Docking Container
       o_grid type ref to cl_gui_alv_grid,"Grid
       o_event type ref to lcl_event_receiver,"Events
       w_toolbar type stb_button,"For toolbar
       w_layout  TYPE lvc_s_layo."Layout structure

select * from mara into corresponding fields of table itab up to 10 rows.
call screen 9000.

CLASS lcl_event_receiver DEFINITION.
*   event receiver definitions for ALV actions
  PUBLIC SECTION.
    METHODS:
* Tool bar
       handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object
                      e_interactive,
* Status bar
       handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING e_ucomm.
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.
    perform toolbar.
    APPEND w_toolbar TO e_object->mt_toolbar.
  ENDMETHOD.
    METHOD handle_user_command.
* In event handler method for event USER_COMMAND: Query your
*   function codes defined in step 2 and react accordingly.
    perform user_command using e_ucomm.
    ENDMETHOD.
ENDCLASS.

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       PBO
*----------------------------------------------------------------------*
module STATUS_9000 output.
if o_docking is initial.
  SET PF-STATUS 'ZSTATUS'. "GUI Status
  SET TITLEBAR 'ZTITLE'.   "Title
* Creating Objects
  perform create_objects.
* Handling Events
  perform handle_events.
* Filling the fieldcatalog table
  perform build_fieldcat.
* Displaying the output
  perform display_output.
endif.
endmodule.                 " STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       PAI
*----------------------------------------------------------------------*
module USER_COMMAND_9000 input.
data lv_ucomm type sy-ucomm.
lv_ucomm = sy-ucomm.
  CASE lv_ucomm.
    WHEN 'CANCEl' OR 'EXIT'.
      perform free_objects.
      LEAVE PROGRAM.
    when 'BACK'.
      perform free_objects.
     set screen '0'.
     leave screen.
  ENDCASE.
endmodule.                 " USER_COMMAND_9000  INPUT

*&---------------------------------------------------------------------*
*&      Form  create_objects
*&---------------------------------------------------------------------*
*       Creating Container,Grid and Event receiver
*----------------------------------------------------------------------*
form create_objects .
* Creating Docking Container
  CREATE OBJECT o_docking
         EXPORTING
           RATIO                       = '95'.
  IF sy-subrc eq 0.
*   Creating Grid
    CREATE OBJECT o_grid
      EXPORTING
        i_parent          =  o_docking.
  ENDIF.
* Create the event receiver
      CREATE OBJECT o_event.
endform.                    " create_objects

*&---------------------------------------------------------------------*
*&      Form  handle_events
*&---------------------------------------------------------------------*
*       Handling Events
*----------------------------------------------------------------------*
form handle_events.
* Handling Events
    SET HANDLER o_event->handle_toolbar FOR o_grid.
    SET HANDLER o_event->handle_user_command FOR o_grid.
endform.                    " handle_events

*&---------------------------------------------------------------------*
*&      Form  toolbar
*&---------------------------------------------------------------------*
*       Creating a button in Toolbar
*----------------------------------------------------------------------*
form toolbar.
    CLEAR w_toolbar.
     MOVE : 'COUNT' TO w_toolbar-function,"Function code
           ''  TO w_toolbar-icon,"Name of an Icon
           ''  TO w_toolbar-quickinfo,"Quickinfo for an icon
           '' TO w_toolbar-butn_type,"Toolbar button type
           '' TO w_toolbar-disabled,"Disabled
           'COUNT' TO w_toolbar-text."Text
endform.                    " toolbar

*&---------------------------------------------------------------------*
*&      Form  build_fieldcat
*&---------------------------------------------------------------------*
*       Creating Field catalog
*----------------------------------------------------------------------*
form build_fieldcat .
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
       I_STRUCTURE_NAME             = 'MARA'
    CHANGING
       ct_fieldcat                  =  i_fieldcat
    EXCEPTIONS
          INCONSISTENT_INTERFACE       = 1
          PROGRAM_ERROR                = 2
          OTHERS                       = 3.
endform.                    " build_fieldcat

*&---------------------------------------------------------------------*
*&      Form  display_output
*&---------------------------------------------------------------------*
*       Displaying the Output
*----------------------------------------------------------------------*
form display_output .
  w_variant-report = sy-repid.
  w_layout-sel_mode = 'A'.
   CALL METHOD o_grid->set_table_for_first_display
     EXPORTING
        IS_VARIANT                    = w_variant
        I_SAVE                        = 'A'
        is_layout                      = w_layout
     CHANGING
        it_outtab                     = itab
        IT_FIELDCATALOG               = i_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.
endform.                    " display_output

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*       Handling User command
*----------------------------------------------------------------------*
form user_command using p_e_ucomm.
    DATA :li_rows TYPE STANDARD TABLE OF lvc_s_row,
    "Internal table for getting selected rows
          lv_ln TYPE i."No. of rows selected
    CALL METHOD o_grid->get_selected_rows
        IMPORTING
          et_index_rows = li_rows.
    CASE p_e_ucomm.
      WHEN 'COUNT'.
        DESCRIBE TABLE li_rows LINES lv_ln.
        MESSAGE s001(zalv) WITH 'No. of rows selected' lv_ln.
    ENDCASE.
endform.                    " user_command

*&---------------------------------------------------------------------*
*&      Form  free_objects
*&---------------------------------------------------------------------*
*       Free Objects
*----------------------------------------------------------------------*
form free_objects .
CALL METHOD o_grid->free
  EXCEPTIONS
    CNTL_ERROR        = 1
    CNTL_SYSTEM_ERROR = 2
    others            = 3.
IF sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL METHOD o_docking->free
  EXCEPTIONS
    CNTL_ERROR        = 1
    CNTL_SYSTEM_ERROR = 2
    others            = 3.
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.                    " free_objects
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.