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

ALV Object Model - Simple 2D Table - Event Handling



 
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: 1640

PostPosted: Tue Jun 24, 2008 5:05 pm    Post subject: ALV Object Model - Simple 2D Table - Event Handling Reply with quote

ALV Object Model - Simple 2D Table - Event Handling
Author(s): Rich Heilman
Original: https: //www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/cda3992d-0e01-0010-90b2-c4e1f899ac01

Applies to:
Netweaver 2004 and Netweaver 2004s

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.

  call method cl_salv_table=>factory
    importing
      r_salv_table = gr_table
    changing
      t_table      = ispfli.


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

  call method cl_salv_table=>factory
    importing
      r_salv_table = gr_table
    changing
      t_table      = ispfli.

  gr_table->set_screen_status(
              pfstatus      =  'SALV_TABLE_STANDARD'
              report        =  sy-repid
              set_functions = gr_table->c_functions_all ).

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

  call method cl_salv_table=>factory
    importing
      r_salv_table = gr_table
    changing
      t_table      = ispfli.

  gr_table->set_screen_status(
              pfstatus      =  'SALV_TABLE_STANDARD'
              report        =  sy-repid
              set_functions = gr_table->c_functions_all ).

  gr_events = gr_table->get_event( ).

  create object  event_handler.

  set handler event_handler->on_user_command 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.

    case e_salv_function.
      when 'MYFUNCTION'.

       lr_selections = gr_table->get_selections( ).
        lt_rows = lr_selections->get_selected_rows( ).

        read table lt_rows into ls_rows index 1.

        read table ispfli into xspfli index ls_rows.

        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.

  call method cl_salv_table=>factory
    importing
      r_salv_table = gr_table
    changing
      t_table      = ispfli.

  gr_table->set_screen_status(
              pfstatus      =  'SALV_TABLE_STANDARD'
              report        =  sy-repid
              set_functions = gr_table->c_functions_all ).

  gr_events = gr_table->get_event( ).

  create object  event_handler.

  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.

    case e_salv_function.
      when 'MYFUNCTION'.

        lr_selections = gr_table->get_selections( ).
        lt_rows = lr_selections->get_selected_rows( ).

        read table lt_rows into ls_rows index 1.

        read table ispfli into xspfli index ls_rows.

        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

  method on_double_click.

    data: message type string.
    data: row_c(4) type c.

    row_c = row.

    concatenate 'Row' row_c 'Column' column
                   into message separated by space.
    message i001(00) with 'You double-clicked on ' message.


  endmethod.                    "on_double_click
endclass.                    "lcl_handle_events IMPLEMENTATION


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.
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.