Вы находитесь на странице: 1из 14

SALV Table 1 : Normal ALV Table Display ᄃ

SAP has introduced a new programming model to devleop Object Oriented ALV (OO ALV) using
class CL_SALV family class. This class family is avaliable in the SAP Netweaver 04.
Prior to SAP Netweaver, we have so many different starting point to start the ALV. The starting point
entirly depends of the flavour of ALV (Tabular, Tree, Hierarchical), Type of ALV (List or Grid) etc. If
we want to use the Control framework, than we need to start the ALV by using the class
CL_GUI_ALV_GRID. Moreover to this, we need to make different calles for TOP-OF-PAGE, END-
OF-PAGE etc. To avoid this SAP has developed common model – which is entirely based on the
object oriented.
These new ALV object oriented model has many advantages:
 Simplified design: This Model uses a highly integrated object oriented design which provides
the simplicity to programmers to develop the ALV.
 Unified Object models: This model has only one main class which will get and set the
parameters of entire layout.
The main classes for the different flavour of ALV:
ALV Flavour Class
Simple 2D table display CL_SALV_TABLE
Hierarchical ALV display CL_SALV_HIERSEQU_TABLE
Tree ALV using class CL_SALV_TREE
All Classes has static method FACTORY which will get back the instance of the ALV. Like for the
simple table dispaly we must call the method CL_SALV_TABLE=>FACTORY to get the instance of
the ALV.
In this post we will see how we can cretae a simple table output just by calling two methods.
Code Snippet to generate ALV using class CL_SALV_TABLE
I had tried to build the report in the object oriented fashion. UML of that report is as follow. This
design will give us flexibility to add more methods which will set and get the different properties of
the O_ALV object. For example, if we need to change the Heading, we will add one private method
SET_COLUMN_NAME and write our logic to change the O_ALV object. Than we will call that
method just before calling the DISPLAY method. (We will cover this in next blogpost).
In the code snippet, I have tried to differential three section which can be changed easily. In next blog
posts, I will use this program as the base program and provide the code snippet to add the new
functionality. Like: Section of CODE_ADD_1 can be replaced with the code provided in the future
blog post. You can download this base program code snippet from here ᄃ.

*&---------------------------------------------------------------------*
*& Report ZVINALV01
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZVINALV01.
*
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* Final output table
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,
kunnr TYPE kunnr,
END OF ty_vbak.
*
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV reference
DATA: o_alv TYPE REF TO cl_salv_table.
*
METHODS:
* data selection
get_data,
*
* Generating output
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS. "lcl_report DEFINITION
*
*
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
*
CREATE OBJECT lo_report.
*
lo_report->get_data( ).
*
lo_report->generate_output( ).
*
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* data selection
SELECT vbeln erdat auart kunnr
INTO TABLE t_vbak
FROM vbak
UP TO 100 ROWS.
*
ENDMETHOD. "get_data
*
*.......................................................................
METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.
*
* exception class
DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = t_vbak ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
o_alv->display( ).
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
*
ENDCLASS. "lcl_report IMPLEMENTATION

SALV Table 2 : Adding Default PF STATUS in ALV ᄃ


We have discussed in post SALV Model 1: Normal ALV Table Display ᄃ to generate an ALV
using new the SALV Model. It is a true object oriented way to generate an ALV.
Today, we will see how to add standard PF Status (GUI Status) in this Standard ALV created using the
class CL_SALV_TABLE. The object created with reference to CL_SALV_TABLE contains the
method which can be helpful to add the PF-Status in the ALV.
Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with
the relevent section from this code snippet in the base program. You can find the base program code
snippet in the post SALV Model 1: Normal ALV Table Display ᄃ. Adding code to base program
is like adding the code correction from the OSS Note.
The new UML diagram of our application is like:

Code Snippet
to add Default
PF Status

*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
* In this area we will call the methods which will set the
* different properties to the ALV
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_pf_status.
*
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
*
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).
*
ENDMETHOD. "set_pf_status
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*

SALV Table 3 : Adding Custom PF STATUS in ALV ᄃ


We have seen in the previous posts of this Blog series about the new SALV model to Create ALV:
SALV Model 1: Normal ALV Table Display ᄃ
SALV Model 2: Adding default PF STATUS in ALV ᄃ
Sometimes we need to create our own PF Status (GUI Stauts) with more buttons which can provide
the users more ways to interact with the ALV. To set our custom PF status we need to:
First create a PF status: We need to create a PF status with our own buttons as well as the ALV
standard buttons. If we don’t add the ALV buttons than it would not provide the standard functions of
ALVs. On the other hand it is very tedious to add all the ALV buttons in our PF status manually. So
here is the easy way: We will copy the PF-Status using of any standard SALV_DEMO report using the
transaction SE41: Menu Painter. After Copying the PF-Status we can add our own buttons.
Call Method of SALV to display PF:Now we will call this newly created PF status in our ALV using
the method SET_SCREEN_STATUS of the reference of the class CL_SALV_TABLE.
Here is the code snippet to which provides the ADD-ON code to our Base program. The base program
can be found in the SALV Model 1: Normal ALV Table Display ᄃ.
UML diagram is same as the post SALV Model 2: Adding default PF STATUS in ALV ᄃ.

SALV Table 4 : Setting up Layout ᄃ


Today we will see how easy it is to set up the Layout for the ALV which was created using the
reference of the CL_SALV_TABLE calss.
All these discussions can be found under Tutorials > SALV Table Display ᄃ.
Layouts provides greate flexibility to users for saving their own layouts which contains settings like –
sum, subtotal, filter, sort etc. This provides a great degree of advantages to the ALV reports. We can
add much more in this if we can provide the option from which user can select the Layout and
generate his/her report.
We will request our ALV instance for the instance of the CL_SALV_LAYOUT. This class contains all
the necessary methods which can be helpful for the Layout settings.
Here is the code snippet to which provides the ADD-ON code to our Base program. The base program
can be found in the SALV Model 1: Normal ALV Table Display ᄃ.
UML diagram
for the test
program will
be like:

*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
METHODS:
set_layout
CHANGING
co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
* Setting up the PF-Status
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*
* Setting up the Layout
CALL METHOD set_layout
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
*
METHOD set_pf_status.
*
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
*
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).
*
ENDMETHOD. "set_pf_status
*
METHOD set_layout.
*
DATA: lo_layout TYPE REF TO cl_salv_layout,
lf_variant TYPE slis_vari,
ls_key TYPE salv_s_layout_key.
*
* get layout object
lo_layout = co_alv->get_layout( ).
*
* set Layout save restriction
* 1. Set Layout Key .. Unique key identifies the Differenet ALVs
ls_key-report = sy-repid.
lo_layout->set_key( ls_key ).
* 2. Remove Save layout the restriction.
lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
*
* set initial Layout
lf_variant = 'DEFAULT'.
lo_layout->set_initial_layout( lf_variant ).
*
ENDMETHOD. "set_layout
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*

SALV Table 5 – Add Header (Top of page) & Footer (End of Page)ᄃ
Today we will discuss how to add the Header and Footer using the SALV model. In ALV, header (top-
of-page) and footer (end-of-page) play important role in presentation of the data. Header and footer
are imortant when we need to print the report and use it for later decisions. Assume the report which
has only columns and no information. Does it really define “Information”? – NO. So, let’s get started
on how to create Header and Footer.
Header and Footer both can be created using the class reference
CL_SALV_FORM_LAYOUT_GRID.We will create:
- A reference of the class CL_SALV_FORM_LAYOUT_GRID
- Use methods of this reference to create a Lables & Flow.
- Set this reference into the main ALV object (reference to CL_SALV_TABLE).
Labels are useful to generate the text output in intensified or Bold letters. This can be used to print the
Header, like – Sales Revenue Report. We can also use this kind of lables in the Footer section to
highlight the total amount of all orders – for example. Flow is useful to dispaly the information in the
tabular format. This flow can be used to display the Selection parameters, selected rows etc.
Here is the code snippet to which provides the ADD-ON code to our Base program. The base program
can be found in the SALV Model 1: Normal ALV Table Display ᄃ.
UML diagram for the test program will be like:

Code Snippet
to generate
Header &
Footer

*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
* Default Pf Status
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
* Set Top of page
METHODS:
set_top_of_page
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
* Set End of page
METHODS:
set_end_of_page
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
* Setting up the default PF status
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*
* Calling the top of page method
CALL METHOD me->set_top_of_page
CHANGING
co_alv = o_alv.
*
* Calling the End of Page method
CALL METHOD me->set_end_of_page
CHANGING
co_alv = o_alv.
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_pf_status.
*
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
* Default Functions
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).
*
ENDMETHOD. "set_pf_status
*
METHOD set_top_of_page.
*
DATA: lo_header TYPE REF TO cl_salv_form_layout_grid,
lo_h_label TYPE REF TO cl_salv_form_label,
lo_h_flow TYPE REF TO cl_salv_form_layout_flow.
*
* header object
CREATE OBJECT lo_header.
*
* To create a Lable or Flow we have to specify the target
* row and column number where we need to set up the output
* text.
*
* information in Bold
lo_h_label = lo_header->create_label( row = 1 column = 1 ).
lo_h_label->set_text( 'Header in Bold' ).
*
* information in tabular format
lo_h_flow = lo_header->create_flow( row = 2 column = 1 ).
lo_h_flow->create_text( text = 'This is text of flow' ).
*
lo_h_flow = lo_header->create_flow( row = 3 column = 1 ).
lo_h_flow->create_text( text = 'Number of Records in the output' ).
*
lo_h_flow = lo_header->create_flow( row = 3 column = 2 ).
lo_h_flow->create_text( text = 20 ).
*
* set the top of list using the header for Online.
co_alv->set_top_of_list( lo_header ).
*
* set the top of list using the header for Print.
co_alv->set_top_of_list_print( lo_header ).
*
ENDMETHOD. "set_top_of_page
*
METHOD set_end_of_page.
*
DATA: lo_footer TYPE REF TO cl_salv_form_layout_grid,
lo_f_label TYPE REF TO cl_salv_form_label,
lo_f_flow TYPE REF TO cl_salv_form_layout_flow.
*
* footer object
CREATE OBJECT lo_footer.
*
* information in bold
lo_f_label = lo_footer->create_label( row = 1 column = 1 ).
lo_f_label->set_text( 'Footer .. here it goes' ).
*
* tabular information
lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ).
lo_f_flow->create_text( text = 'This is text of flow in footer' ).
*
lo_f_flow = lo_footer->create_flow( row = 3 column = 1 ).
lo_f_flow->create_text( text = 'Footer number' ).
*
lo_f_flow = lo_footer->create_flow( row = 3 column = 2 ).
lo_f_flow->create_text( text = 1 ).
*
* Online footer
co_alv->set_end_of_list( lo_footer ).
*
* Footer in print
co_alv->set_end_of_list_print( lo_footer ).
*
ENDMETHOD. "set_end_of_page
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
This code will generate output like this:

When you print this
output, it will generate
a spool like:

SALV Table 6 – Dispaly Settings ᄃ


In the series of the SALV Simple Model, we will see how we can set the Display Settings to the entire
ALV. You can find the previous discussions in this blog series at Tutorials > SALV Table Display

To set the display settings, we need to use the reference of the class
CL_SALV_DISPLAY_SETTINGS. We will ask the reference of the Display settings from our ALV
object created using the CL_SALV_TABLE. By using the dispaly settings, we can set the Zebra style,
Title of the ALV etc.
Here is the code snippet to which provides the ADD-ON code to our Base program. The base program
can be found in the SALV Model 1: Normal ALV Table Display ᄃ.
UML diagram
for this
example is
like:

*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
METHODS:
set_display_setting
CHANGING
co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
CALL METHOD set_display_setting
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_display_setting.
*
DATA: lo_display TYPE REF TO cl_salv_display_settings.
*
* get display object
lo_display = o_alv->get_display_settings( ).
*
* set ZEBRA pattern
lo_display->set_striped_pattern( 'X' ).
*
* Title to ALV
lo_display->set_list_header( 'ALV Test for Display Settings' ).
*
ENDMETHOD. "SET_DISPLAY_SETTING
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*


SALV Table 8 – Add & Handle Hotspot ᄃ
In the series of the SALV model table display, today we will see how to add the hotspot and after
adding, how to handle that hotspot. You can find all the previous discussion at Tutorials > SALV
Table Display ᄃ .
Hotspot is useful in most of the ALV reports to drill-down from the main list. For example, in the
Sales order report, it would be great if we provide a drill-down to VA03 (Sales Order display). By this
way users would have better flexibility to look into the respective document information.
To add the Hotspot, we need to get the object reference of the CL_SALV_COLUMNS_TABLE from
the ALV object (reference to CL_SALV_TABLE). This object reference contains the properties related
to ALL the columns. For implementing HOTSPOT, we need to change the specific column properties
(i.e. Sales Order or Customer). For this purpose, we need to have an access of the object reference
CL_SALV_COLUMN_TABLE – which will contain the properties of the speicific column. We will
set the specific Cell type for the HOTSPOT. Here we will use the Casting concept of the Object
Oriented to refer the properties of the CL_SAL_COLUMN_TABLE. Read this for casting: ABAP
OBjects – Narrowing Cast ᄃ & ABAP Objects: Widening Cast ᄃ.
Ater implementing the Hotspot, we need to implement the Event Hanlder to handle the Hotspot. Here,
we will implement the event listner method for the event LINK_CLICK of the class
CL_SALV_EVENTS_TABLE. We need to set the handler of this events to let the system know, where
to go when the event occurs.
Note:Here we have used the class CL_SALV_COLUMNS_TABLE which is different than the class
CL_SALV_COLUMNS discussed as in the previous post SALV Model 7 – Changing Column
settings ᄃ.
Here is the code snippet to which provides the ADD-ON code to our Base program. The base program
can be found in the SALV Model 1: Normal ALV Table Display ᄃ.
UML Diagram
would be like:

Code Snippet
for Hotspot and
event handling
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
* Set the various column properties
METHODS:
set_hotspot_vbeln
CHANGING
co_alv TYPE REF TO cl_salv_table
co_report TYPE REF TO lcl_report.
*
* Event Handler for HOTSPOT event
METHODS:
on_link_click
FOR EVENT link_click OF cl_salv_events_table
IMPORTING
row
column .
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
* Set Up the Hotspot & Event Handler
CALL METHOD set_hotspot_vbeln
CHANGING
co_alv = o_alv
co_report = lo_report.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_hotspot_vbeln.
*
*...HotSpot
DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
lo_col_tab TYPE REF TO cl_salv_column_table.
*
* get Columns object
lo_cols_tab = co_alv->get_columns( ).
*
* Get VBELN column
TRY.
lo_col_tab ?= lo_cols_tab->get_column( 'VBELN' ).
CATCH cx_salv_not_found.
ENDTRY.
*
* Set the HotSpot for VBELN Column
TRY.
CALL METHOD lo_col_tab->set_cell_type
EXPORTING
value = if_salv_c_cell_type=>hotspot.
.
CATCH cx_salv_data_error .
ENDTRY.
*
*...Events
DATA: lo_events TYPE REF TO cl_salv_events_table.
*
* all events
lo_events = o_alv->get_event( ).
*
* event handler
SET HANDLER co_report->on_link_click FOR lo_events.
*
ENDMETHOD. "set_hotspot_vbeln
*
* Handles the UI on the VBELN (HotSpot)
METHOD on_link_click.
*
DATA: la_vbak TYPE ty_vbak.
*
* Get the Sales Order number from the table
READ TABLE lo_report->t_vbak INTO la_vbak INDEX row.
IF la_vbak-vbeln IS NOT INITIAL.
MESSAGE i398(00) WITH 'You have selected' la_vbak-vbeln.
ENDIF.
*
ENDMETHOD. "on_link_click
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*

After event handling
of the Hotspot:

Вам также может понравиться