Summary
This tutorial will show how to implement event handling when using the new ALV object model. For more
examples, see any program which begins with SALV* in your Netweaver ABAP System.
The Basic Program
Starting with the program below, we will add coding to handle some events for the ALV Grid. In this example
the events DOUBLE_CLICK and ADDED_FUNCTION will be handled.
Code:
report zalvom_demo3.
data: ispfli type table of spfli.
data: xspfli type spfli.
data: gr_table type ref to cl_salv_table.
data: gr_selections type ref to cl_salv_selections.
start-of-selection.
select * into corresponding fields of table ispfli from spfli
up to 100 rows.
* Set up selections.
gr_selections = gr_table->get_selections( ).
gr_selections->set_selection_mode( 1 ). "Single
* Display
gr_table->display( ).
Set the Gui Status
Next, go to function group SALV_METADATA_STATUS and copy the gui status
SALV_TABLE_STANDARD into the ZALVOM_DEMO3 program. This is the standard gui status for the 2
Dimensional Table ALV grid. Once you have copied the status, set the screen status using the appropriate
method of the object GR_TABLE. Go to the gui status and add a new button on the application toolbar and
name it as "MYFUNCTION".
Code:
report zalvom_demo3.
data: ispfli type table of spfli.
data: xspfli type spfli.
data: gr_table type ref to cl_salv_table.
data: gr_selections type ref to cl_salv_selections.
start-of-selection.
select * into corresponding fields of table ispfli from spfli
up to 100 rows.
* Set up selections.
gr_selections = gr_table->get_selections( ).
gr_selections->set_selection_mode( 1 ). "Single
* Display
gr_table->display( ).
Event Handlers - Event ADDED_FUNCTION
Next, create a local class which will act as the event handler, define the event handler method for the
ADDED_FUNCTION event. Define an object reference variable for the local class. Retrieve the events
object from the GR_TABLE, create the event handler object and set the handler method for the event.
Finally, add the implementation for the ON_USER_COMMAND event handler method.
Code:
report zalvom_demo3.
data: ispfli type table of spfli.
data: xspfli type spfli.
data: gr_table type ref to cl_salv_table.
data: gr_events type ref to cl_salv_events_table.
data: gr_selections type ref to cl_salv_selections.
*----------------------------------------------------------------------*
* CLASS lcl_handle_events DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_handle_events definition.
public section.
methods:
on_user_command for event added_function of cl_salv_events
importing e_salv_function,
endclass. "lcl_handle_events DEFINITION
data: event_handler type ref to lcl_handle_events.
start-of-selection.
select * into corresponding fields of table ispfli from spfli
up to 100 rows.
*----------------------------------------------------------------------*
* CLASS lcl_handle_events IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_handle_events implementation.
method on_user_command.
* Get the selection rows
data: lr_selections type ref to cl_salv_selections.
data: lt_rows type salv_t_row.
data: ls_rows type i.
data: message type string.
concatenate xspfli-carrid xspfli-connid
xspfli-cityfrom xspfli-cityto
into message separated by space.
message i001(00) with 'You pushed the button!' message.
endcase.
endmethod. "on_user_command
endclass. "lcl_handle_events IMPLEMENTATION
Run the program, select a row by single clicking on it and click the icon for the new function that you added.
Notice that some of the data in the row that was clicked is now showing in the message.
Event Handlers - Event DOUBLE_CLICK
Define the event handler method for DOUBLE_CLICK event and add the implementation for the
ON_DOUBLE_CLICK event handler method. Remember to set the handler for the event.
Code:
report zalvom_demo3.
data: ispfli type table of spfli.
data: xspfli type spfli.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions_list.
data: gr_events type ref to cl_salv_events_table.
data: gr_selections type ref to cl_salv_selections.
*----------------------------------------------------------------------*
* CLASS lcl_handle_events DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_handle_events definition.
public section.
methods:
on_user_command for event added_function of cl_salv_events
importing e_salv_function,
on_double_click for event double_click of cl_salv_events_table
importing row column.
endclass. "lcl_handle_events DEFINITION
data: event_handler type ref to lcl_handle_events.
start-of-selection.
select * into corresponding fields of table ispfli from spfli
up to 100 rows.
set handler event_handler->on_user_command for gr_events.
set handler event_handler->on_double_click for gr_events.
* Set up selections.
gr_selections = gr_table->get_selections( ).
gr_selections->set_selection_mode( 1 ). "Single
* Display
gr_table->display( ).
*----------------------------------------------------------------------*
* CLASS lcl_handle_events IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_handle_events implementation.
method on_user_command.
* Get the selection rows
data: lr_selections type ref to cl_salv_selections.
data: lt_rows type salv_t_row.
data: ls_rows type i.
data: message type string.
Run the program, double click on the fifth row in the Depart. City column, notice the information message
contains the row number and column name of the cell which you double clicked.
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.