Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

Understanding the events of OOPS ALV

$
0
0

REPORT  ysuv_alv_editable_oops_003.

*&----------------------------------------------------------------------*
*               WHAT THIS REPORT DOES
*&----------------------------------------------------------------------*
* This Report tells the use of the five events in OOPS ALV .
*         1.) Event Toolbar .
*         2.) Event Double_click .
*         3.) Event Top_of_page .
*         4.) Event Hotspot_click .
*         5.) Event User_command .
* Also the colour of the particular cell is set based on a condition .
*
* -> The report is Editable and interactive .
* -> A button SAVE is defined in the toolbar .
* -> When that save button is pressed then the changed data will be saved
*    in the database using the event user_command .
* -> Double click on the entite row will call transaction ME23N .
* -> Hotspot Click on the first field will call ME23N .
*&----------------------------------------------------------------------*
*                   ESSENTIAL STEPS
*&----------------------------------------------------------------------*
*STEP 1: Create a screen with screen no. 0600 and create a custom
*        container and give it a name 'CUSTOM_CONTAINER' . Create a PF
*        status for this screen .
*STEP 2: Define a local class for event handling lcl_event_handler .
*        And define the methods for  each event within this class .
*        Implement the event handler methods .
*STEP 3: Link all the events with the event handler methods using
*        the statement SET_HANDLER before displaying the ALV
*&----------------------------------------------------------------------*
*                UNDERSTANDING THE REPORT
*&----------------------------------------------------------------------*
* NOTE 1: TOP_OF_PAGE event uses the object of class CL_DD_DOCUMENT.
*         In this class there are methods ADD_TEXT, ADD_PICTURE, and ADD_GAP
*         which are useful to to show the contenet in the TOP_OF_PAGE.
*         One important thing is to split the screen into two parts using the
*         splitter container and then use the first part to TOP_OF_PAGE and
*         the second one to show the Grid data.
* NOTE 2: If a tool is created in the toolbar of the ALV Grid using the
*         event toolbar , the functionality of that tool can be handeled in
*         the event user_command .
*&----------------------------------------------------------------------*


CLASS :lcl_event_receiver DEFINITION DEFERRED.

*Type pools for ALV Cell Attributes (Colour)
INCLUDE<cl_alv_control>.

*To use icons in the toolbar of the ALV .
*To see all the icons just go to the transparent table ICON .
TYPE-POOLS : icon .

* Type Pool for ALV
TYPE-POOLS : slis.

TYPES: BEGINOF t_ekpo,
ebeln
TYPE ekpo-ebeln,
ebelp
TYPE ekpo-ebelp,
statu
TYPE ekpo-statu,
aedat
TYPE ekpo-aedat,
matnr
TYPE ekpo-matnr,
menge
TYPE ekpo-menge,
meins
TYPE ekpo-meins,
netpr
TYPE ekpo-netpr,
peinh
TYPE ekpo-peinh,
field_style 
TYPE lvc_t_styl, "For Making the cell editable / non editable
color_cell
TYPE lvc_t_scol, " Cell color
ENDOF t_ekpo.

DATA: it_ekpo TYPESTANDARDTABLEOF t_ekpo INITIALSIZE0,
wa_ekpo
TYPE t_ekpo ,
wa_upd_ekpo
TYPE ekpo .

*ALV data declarations
DATA: it_fieldcat TYPE lvc_t_fcat, "slis_t_fieldcat_alv WITH HEADER LINE,
wa_fieldcat
TYPE lvc_s_fcat,
it_layout
TYPE lvc_s_layo,
it_color
TYPETABLEOF lvc_s_scol,
wa_color
TYPE lvc_s_scol.

*Data declarations for ALV
DATA: custom_container TYPEREFTO cl_gui_custom_container,   "Custom container
alv_grid
TYPEREFTO cl_gui_alv_grid.   "ALV grid object

* Reference to local class that handles events of ALV Grid .
* The Class lcl_event_receiver is a local class which will be defined in this program
DATA : event_receiver TYPEREFTO lcl_event_receiver.

* Data Declaration to handle the top_of_page Event .
DATA : dg_dyndoc_id TYPEREFTO cl_dd_document,
* Reference to split container
dg_splitter         
TYPEREFTO cl_gui_splitter_container,
* Reference to grid container
dg_parent_grid    
TYPEREFTO cl_gui_container,
* Reference to html container
dg_html_cntrl       
TYPEREFTO cl_gui_html_viewer,
* Reference to html container
dg_parent_html    
TYPEREFTO cl_gui_container.


*Start-of-selection.
START-
OF-SELECTION.

PERFORM data_retrieval.
PERFORM set_specific_field_attributes.
PERFORM build_fieldcatalog.
PERFORM build_layout.

CALLSCREEN0600 .

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.

PUBLICSECTION.
METHODS:
handle_toolbar
FOREVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object
e_interactive,

handle_user_command
FOREVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm ,

handle_top_of_page
FOREVENT top_of_page OF cl_gui_alv_grid
IMPORTING e_dyndoc_id ,

handle_double_click
FOREVENT double_click OF cl_gui_alv_grid
IMPORTING e_row
e_column ,

handle_hotspot_click
FOREVENT hotspot_click OF cl_gui_alv_grid
IMPORTING  e_row_id
e_column_id
es_row_no
sender.

ENDCLASS.                    "lcl_event_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.

METHOD handle_toolbar.
* § 2.In event handler method for event TOOLBAR: Append own functions
*   by using event parameter E_OBJECT.
DATA: ls_toolbar  TYPE stb_button.
*....................................................................
* E_OBJECT of event TOOLBAR is of type REF TO CL_ALV_EVENT_TOOLBAR_SET.
* This class has got one attribute, namly MT_TOOLBAR, which
* is a table of type TTB_BUTTON. One line of this table is
* defined by the Structure STB_BUTTON (see data deklaration above).
*

* A remark to the flag E_INTERACTIVE:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*         'e_interactive' is set, if this event is raised due to
*         the call of 'set_toolbar_interactive' by the user.
*         You can distinguish this way if the event was raised
*         by yourself or by ALV
*         (e.g. in method 'refresh_table_display').
*         An application of this feature is still unknown... :-)

* append a separator to normal toolbar
CLEAR ls_toolbar.
MOVE'SAVE'TO ls_toolbar-function.
MOVE icon_system_save TO ls_toolbar-icon. " See this icon in the transparent table ICON .
MOVE'Save Edited Data'(111) TO ls_toolbar-quickinfo.
MOVE'Save'(112) TO ls_toolbar-text.
APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.                    "handle_toolbar

METHOD handle_user_command.
* If any changes were made in the editable cell of the ALV that will be
* stored during runtime
CASE e_ucomm .
WHEN'SAVE'.
LOOPAT it_ekpo INTO wa_ekpo.
SELECTSINGLE * FROM ekpo INTO wa_upd_ekpo
WHERE ebeln = wa_ekpo-ebeln AND
ebelp = wa_ekpo-ebelp .
wa_upd_ekpo-netpr = wa_ekpo-netpr .
UPDATE ekpo FROM wa_upd_ekpo .
ENDLOOP.
ENDCASE.
ENDMETHOD.                    "handle_double_click

METHOD handle_top_of_page.
PERFORM event_top_of_page USING dg_dyndoc_id.
ENDMETHOD.                    "handle_top_of_page

METHOD handle_double_click .
* The double click will work on the entire line(row) of the ALV
READTABLE it_ekpo INTO wa_ekpo INDEX e_row-indexTRANSPORTING ebeln.
*     Set parameter ID for transaction screen field
SETPARAMETERID'BES'FIELD wa_ekpo-ebeln.
*     Sxecute transaction ME23N, and skip initial data entry screen
CALLTRANSACTION'ME23N'ANDSKIPFIRSTSCREEN.
ENDMETHOD.                    "handle_double_click

METHOD handle_hotspot_click .
*     The hotspot is set in the field catalog level
READTABLE it_ekpo INTO wa_ekpo INDEX e_row_id-indexTRANSPORTING ebeln.
*     Set parameter ID for transaction screen field
SETPARAMETERID'BES'FIELD wa_ekpo-ebeln.
*     Sxecute transaction ME23N, and skip initial data entry screen
CALLTRANSACTION'ME23N'ANDSKIPFIRSTSCREEN.
ENDMETHOD.                    "handle_hotspot_click

ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION
*&---------------------------------------------------------------------*
*&      Module  STATUS_0600  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0600 OUTPUT.
SET PF-STATUS 'STATUS100'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0600  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  alv_grid  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE alv_grid OUTPUT.

CREATE OBJECT custom_container
EXPORTING
container_name =
'CUSTOM_CONTAINER'.

CREATE OBJECT dg_dyndoc_id
EXPORTING style = 'ALV_GRID'.
* Create Splitter for custom_container
CREATE OBJECT dg_splitter
EXPORTING parent  = custom_container
rows    = 2
columns =
1.

* Split the custom_container to two containers and move the reference
* to receiving containers g_parent_html and g_parent_grid
"i am allocating the space for grid and top of page
CALLMETHOD dg_splitter->get_container
EXPORTING
row       =
1
column    =
1
RECEIVING
container = dg_parent_html.
CALLMETHOD dg_splitter->get_container
EXPORTING
row       =
2
column    =
1
RECEIVING
container = dg_parent_grid.

"you can set the height of it
* Set height for g_parent_html
CALLMETHOD dg_splitter->set_row_height
EXPORTING
id     = 1
height =
33.

CREATE OBJECT alv_grid
EXPORTING
i_parent = dg_parent_grid.
"* SET field for ALV

CALLMETHOD alv_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.

CREATE OBJECT event_receiver.
SETHANDLER event_receiver->handle_user_command FOR alv_grid.
SETHANDLER event_receiver->handle_toolbar FOR alv_grid.
SETHANDLER event_receiver->handle_top_of_page FOR alv_grid.
SETHANDLER event_receiver->handle_double_click FOR alv_grid .
SETHANDLER event_receiver->handle_hotspot_click FOR alv_grid .

* setting focus for created grid control
* Call "set_focus" if you want to make sure that 'the cursor'
* is active in your control.
CALLMETHOD cl_gui_control=>set_focus
EXPORTING
control = alv_grid.

CALLMETHOD alv_grid->set_table_for_first_display
EXPORTING
is_layout                     = it_layout
CHANGING
it_outtab                     = it_ekpo
it_fieldcatalog               = it_fieldcat
EXCEPTIONS
invalid_parameter_combination =
1
program_error                 =
2
too_many_lines                =
3
OTHERS                        = 4.
IF sy-subrc <> 0.
MESSAGEID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

* Initializing document
CALLMETHOD dg_dyndoc_id->initialize_document.

* Processing events
CALLMETHOD alv_grid->list_processing_events
EXPORTING
i_event_name =
'TOP_OF_PAGE'
i_dyndoc_id  = dg_dyndoc_id.


CALLMETHOD alv_grid->set_toolbar_interactive.

ENDMODULE.                 " alv_grid  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0600  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0600 INPUT.
CASE sy-ucomm .
WHEN'BACK'.
LEAVEPROGRAM.
ENDCASE.

ENDMODULE.                 " USER_COMMAND_0600  INPUT
*&---------------------------------------------------------------------*
*&      Form  data_retrieval
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM data_retrieval .
SELECT ebeln
ebelp
statu
aedat
matnr
menge
meins
netpr
peinh
UPTO10ROWSFROM ekpo
INTO  CORRESPONDING FIELDSOFTABLE it_ekpo.

ENDFORM.                    " data_retrieval
*&---------------------------------------------------------------------*
*&      Form  set_specific_field_attributes
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_specific_field_attributes .
DATA ls_stylerow TYPE lvc_s_styl .
DATA lt_styletab TYPE lvc_t_styl .

* Populate style variable (FIELD_STYLE) with style properties
*
* The NETPR field/column has been set to editable in the fieldcatalog...
* The following code sets it to be disabled(display only) if 'NETPR'
* is gt than 10.
LOOPAT it_ekpo INTO wa_ekpo.
IF wa_ekpo-netpr EQ555 .
ls_stylerow-fieldname =
'NETPR' .
ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.
"set field to disabled
* APPEND has now been replaced by the INSERT command as it would error
* if entries are not added in correct order
**      APPEND ls_stylerow  TO wa_ekko-field_style.
INSERT ls_stylerow INTOTABLE wa_ekpo-field_style.
MODIFY it_ekpo FROM wa_ekpo.
ENDIF.

*Colouring the particular cell of the ALV Output .
IF wa_ekpo-menge EQ1.
MOVE'MENGE'TO wa_color-fname.
MOVE'6'TO wa_color-color-col. " 6 is for red colour .
MOVE'1'TO wa_color-color-int.
MOVE'1'TO wa_color-color-inv.
APPEND wa_color TO it_color.
wa_ekpo-color_cell[] = it_color[].
MODIFY it_ekpo FROM wa_ekpo TRANSPORTING color_cell .
ENDIF.

ENDLOOP.

ENDFORM.                    " set_specific_field_attributes
*&---------------------------------------------------------------------*
*&      Form  build_fieldcatalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
wa_fieldcat-fieldname   =
'EBELN'.
wa_fieldcat-scrtext_m   =
'Purchase Order'.
wa_fieldcat-col_pos     =
0.
wa_fieldcat-outputlen   =
10.
wa_fieldcat-emphasize   =
'X'.
wa_fieldcat-
key         = 'X'.
wa_fieldcat-
hotspot     = 'X'. " To set the hotspot on this field
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'EBELP'.
wa_fieldcat-scrtext_m   =
'PO Item'.
wa_fieldcat-col_pos     =
1.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'STATU'.
wa_fieldcat-scrtext_m   =
'Status'.
wa_fieldcat-col_pos     =
2.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'AEDAT'.
wa_fieldcat-scrtext_m   =
'Item change date'.
wa_fieldcat-col_pos     =
3.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'MATNR'.
wa_fieldcat-scrtext_m   =
'Material Number'.
wa_fieldcat-col_pos     =
4.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'MENGE'.
wa_fieldcat-scrtext_m   =
'PO quantity'.
wa_fieldcat-col_pos     =
5.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'MEINS'.
wa_fieldcat-scrtext_m   =
'Order Unit'.
wa_fieldcat-col_pos     =
6.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'NETPR'.
wa_fieldcat-scrtext_m   =
'Net Price'.
wa_fieldcat-
edit        = 'X'. "sets whole column to be editable
wa_fieldcat-col_pos     =
7.
wa_fieldcat-outputlen   =
15.
wa_fieldcat-datatype     =
'CURR'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.

wa_fieldcat-fieldname   =
'PEINH'.
wa_fieldcat-scrtext_m   =
'Price Unit'.
wa_fieldcat-col_pos     =
8.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR  wa_fieldcat.


ENDFORM.                    " build_fieldcatalog
*&---------------------------------------------------------------------*
*&      Form  build_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_layout .
* Set layout field for field attributes(i.e. input/output)
it_layout-stylefname =
'FIELD_STYLE'.
it_layout-zebra             =
'X'.
it_layout-ctab_fname =
'COLOR_CELL'.

ENDFORM.                    " build_layout
*&---------------------------------------------------------------------*
*&      Form  event_top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_E_DYNDOC_ID  text
*      -->P_0222   text
*----------------------------------------------------------------------*
FORM event_top_of_page USING dg_dyndoc_id TYPEREFTO cl_dd_document.

DATA : dl_text(255) TYPEc"Text
* Populating header to top-of-page
CALLMETHOD dg_dyndoc_id->add_text
EXPORTING
text      = 'Test Report'
sap_style = cl_dd_area=>heading.

CALLMETHOD dg_dyndoc_id->add_gap
EXPORTING
width =
200.

CALLMETHOD dg_dyndoc_id->add_picture
EXPORTING
picture_id =
'ENJOYSAP_LOGO'.

CLEAR : dl_text.
* Move program ID
CONCATENATE'Program Name :' sy-repid
INTO dl_text SEPARATEDBY space.

CALLMETHOD dg_dyndoc_id->new_line.

CLEAR : dl_text.
* Move User ID
CONCATENATE'User ID :' sy-uname INTO dl_text SEPARATEDBY space
.
* Add User ID to Document
PERFORM add_text USING dl_text.
* Add new-line
CALLMETHOD dg_dyndoc_id->new_line.

CLEAR : dl_text.
* Move Client
CONCATENATE'Client :' sy-mandt INTO dl_text SEPARATEDBY space.
* Add Client to Document
PERFORM add_text USING dl_text.
* Add new-line
CALLMETHOD dg_dyndoc_id->new_line.

CLEAR : dl_text.
* Move date
WRITE sy-datum TO dl_text.
CONCATENATE'Date :' dl_text INTO dl_text SEPARATEDBY space.
* Add Date to Document
PERFORM add_text USING dl_text.
* Add new-line
CALLMETHOD dg_dyndoc_id->new_line.

CLEAR : dl_text.
* Move time
WRITE sy-uzeit TO dl_text.
CONCATENATE'Time :' dl_text INTO dl_text SEPARATEDBY space.
* Add Time to Document
PERFORM add_text USING dl_text.
* Add new-line
CALLMETHOD dg_dyndoc_id->new_line.
* Populating data to html control
PERFORM html.

ENDFORM.                    " selection_screen_details
*&---------------------------------------------------------------------*
*&      Form  add_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_DL_TEXT  text
*----------------------------------------------------------------------*
FORM add_text USING p_text TYPE sdydo_text_element.
* Adding text
CALLMETHOD dg_dyndoc_id->add_text
EXPORTING
text         = p_text
sap_emphasis = cl_dd_area=>heading.
ENDFORM.                    " ADD_TEXT
*&---------------------------------------------------------------------*
*&      Form  commentary_write
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM html .

DATA : dl_length  TYPEi,                           " Length
dl_background_id
TYPE sdydo_key VALUE space. " Background_id

* Creating html control
IF dg_html_cntrl ISINITIAL.
CREATE OBJECT dg_html_cntrl
EXPORTING
parent    = dg_parent_html.
ENDIF.

CALLFUNCTION'REUSE_ALV_GRID_COMMENTARY_SET'
EXPORTING
document = dg_dyndoc_id
bottom   = space
IMPORTING
length   = dl_length.
* Get TOP->HTML_TABLE ready
CALLMETHOD dg_dyndoc_id->merge_document.
* Set wallpaper
CALLMETHOD dg_dyndoc_id->set_document_background
EXPORTING
picture_id = dl_background_id.
* Connect TOP document to HTML-Control
dg_dyndoc_id->html_control = dg_html_cntrl.
* Display TOP document
CALLMETHOD dg_dyndoc_id->display_document
EXPORTING
reuse_control      =
'X'
parent             = dg_parent_html
EXCEPTIONS
html_display_error =
1.
IF sy-subrc NE0.
*    MESSAGE i999 WITH 'Error in displaying top-of-page'(036).
ENDIF.

ENDFORM.                    " commentary_write


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>