Get FREE Access to the CL_SALV_TABLE Bonus Content
Updated on 17.11.2015
In this post series, I show you how to display a simple list with a flight schedule. I also show you the most common ALV settings that you might need. However, I don't show you how to handle events in CL_SALV_TABLE, and how to add custom buttons at the top of your ALV. But, I have good news for you! I re-wrote the complete source code in OO ABAP way with the help of the 7.4 language elements.
What is covered in the bonus content:
- Source Code in 7.4 OO ABAP Version
- Handling Hotspot Click Event
- Adding Custom Button to the Toolbar
- Handling Custom Button Clicks [NEW]
Overview
In the previous post, we already prepared the main ABAP program that responsible to display a flight schedule from database table, SPFLI in an ALV list using CL_SALV_TABLE class. Today, my goal is to show you how to enable ALV layout settings for the users at the top of the ALV list.

To provide this functionality, let me introduce you our first CL_SALV setting class, called CL_SALV_LAYOUT that we can use via the CL_SALV_TABLE's instance method, named GET_LAYOUT(). This method will give us an instance of the CL_SALV_LAYOUT class that we can use to enable the layout settings feature for the users.

Development Workflow
The development workflow is going to be really simple, namely we are going to organize the different settings (mentioned above) into small, clearly named subroutines, then place the calls of these routines one by one in the existing INITIALIZE_ALV subroutine under the FACTORY() method call. I like this approach, because it ensures a high transparency in our code without using unnecessary "dead comments".
Place of ALV Settings
As I mentioned before, we are going to put our ALV settings into the subroutine, INITIALIZE_ALV in the following order: first we call the FACTORY() method of the CL_SALV_TABLE to get an instance of it, then call the subroutines that responsible for different settings.
...
*&---------------------------------------------------------------------*
FORM initialize_alv.
*&---------------------------------------------------------------------*
DATA message TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = alv
CHANGING
t_table = flight_schedule ).
PERFORM enable_layout_settings.
" PERFORM setting_2.
" PERFORM setting_3.
" ...
" PERFORM setting_n.
CATCH cx_salv_msg INTO message.
" error handling
ENDTRY.
ENDFORM. " INITIALIZE_ALV
...
Get Layout Settings
We introduced a new subroutine above, named ENABLE_LAYOUT_SETTINGS. Let's implement it! As I mentioned above, to set the layout settings feature for an ALV list, first we should get an instance of the CL_SALV_LAYOUT class.
We are going to get and save an instance of the CL_SALV_LAYOUT in the variable, called LAYOUT_SETTINGS by calling the instance method of the CL_SALV_TABLE class, called GET_LAYOUT().
...
*&---------------------------------------------------------------------*
FORM enable_layout_settings.
*&---------------------------------------------------------------------*
DATA layout_settings TYPE REF TO cl_salv_layout.
DATA layout_key TYPE salv_s_layout_key.
layout_settings = alv->get_layout( ).
layout_key-report = sy-repid.
layout_settings->set_key( layout_key ).
layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM. "enable_layout_settings
...
Set the Layout Key and the Saving Restriction
As we get a CL_SALV_LAYOUT instance, there is nothing left then using its instance methods to set the expected behavior. We usually set a unique key to make the different ALV layout settings unique between each other, and set a restriction for saving the layouts.

To set a unique key we use the SET_KEY() method of the CL_SALV_LAYOUT class that requires a SALV_S_LAYOUT_KEY structure as an import parameter.

So, we define a LAYOUT_KEY variable with the type SALV_S_LAYOUT_KEY, then set its REPORT field using the SY-REPID system field (contains the technical name of the program: ZDEMO_ALV02), and at last we call the SET_KEY() method with this LAYOUT_KEY variable.
...
*&---------------------------------------------------------------------*
FORM enable_layout_settings.
*&---------------------------------------------------------------------*
DATA layout_settings TYPE REF TO cl_salv_layout.
DATA layout_key TYPE salv_s_layout_key.
layout_settings = alv->get_layout( ).
layout_key-report = sy-repid.
layout_settings->set_key( layout_key ).
layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM. "enable_layout_settings
...
If we check the SET_SAVE_RESTRICTION() method, we find that it requires a parameter with the type SALV_DE_LAYOUT_RESTRICTION. To provide a value with this type, we are going to use the IF_SALV_C_LAYOUT interface that contains three different attributes:

The attributes mean the followings:
IF_SALV_C_LAYOUT attributes
|
Description |
RESTRICT_NONE |
User can save layouts without restriction |
RESTRICT_USER_DEPENDANT |
User can save layouts for herself/himself |
RESTRICT_USER_INDEPENDANT |
User can save layout for all users |
As you get familiar with the members of the IF_SALV_C_LAYOUT, there is nothing left then calling the SET_SAVE_RESTRICTION() method with one of the above mentioned attribute (in my case I use the RESTRICT_NONE).
...
*&---------------------------------------------------------------------*
FORM enable_layout_settings.
*&---------------------------------------------------------------------*
DATA layout_settings TYPE REF TO cl_salv_layout.
DATA layout_key TYPE salv_s_layout_key.
layout_settings = alv->get_layout( ).
layout_key-report = sy-repid.
layout_settings->set_key( layout_key ).
layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM. "enable_layout_settings
...
The Result
That's it! After running the program, we get the following result:

Complete Source Code
Get the complete source code:
************************************************************************
* Program: ZDEMO_ALV02 *
* Request ID: RXXXXXX *
* User ID: ALEXGONCZY *
* Date: 2014.09.16. *
* Description: - *
************************************************************************
REPORT zdemo_alv01.
************************************************************************
* GLOBAL DATA DEFINITIONS *
************************************************************************
*----------------------------------------------------------------------*
* INCLUDE - definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* CONSTANT - definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* TYPE - definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* DDIC - TABLE / STRUCTURE / VIEW definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* STRUCTURE definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* RANGE definitions *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* REFERENCE definitions *
*----------------------------------------------------------------------*
DATA alv TYPE REF TO cl_salv_table.
*----------------------------------------------------------------------*
* INTERNAL TABLE definitions *
*----------------------------------------------------------------------*
DATA flight_schedule TYPE STANDARD TABLE OF spfli.
*----------------------------------------------------------------------*
* OTHER GLOBAL DATA definitions *
*----------------------------------------------------------------------*
************************************************************************
* GLOBAL DATA DEFINITIONS - END *
************************************************************************
************************************************************************
* SELECTION SCREENS *
************************************************************************
************************************************************************
* SELECTION SCREENS - END *
************************************************************************
************************************************************************
* EVENTS *
************************************************************************
*----------------------------------------------------------------------*
* INITIALIZATON event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST FOR event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* START-OF-SELECTION event *
*----------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM get_flight_schedule.
PERFORM initialize_alv.
PERFORM display_alv.
*----------------------------------------------------------------------*
* END-OF-SELECTION event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* AT LINE-SELECTION event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* AT USER-COMMAND event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* TOP-OF-PAGE event *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* END-OF-PAGE event *
*----------------------------------------------------------------------*
************************************************************************
* EVENTS - END *
************************************************************************
************************************************************************
* SUBROUTINES *
************************************************************************
*&---------------------------------------------------------------------*
FORM get_flight_schedule.
*&---------------------------------------------------------------------*
SELECT * FROM spfli INTO TABLE flight_schedule UP TO 100 ROWS.
ENDFORM. " GET_FLIGHT_SCHEDULE
*&---------------------------------------------------------------------*
FORM initialize_alv.
*&---------------------------------------------------------------------*
DATA message TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = alv
CHANGING
t_table = flight_schedule ).
PERFORM enable_layout_settings.
" PERFORM setting_2.
" PERFORM setting_3.
" ...
" PERFORM setting_n.
CATCH cx_salv_msg INTO message.
" error handling
ENDTRY.
ENDFORM. " INITIALIZE_ALV
*&---------------------------------------------------------------------*
FORM display_alv.
*&---------------------------------------------------------------------*
alv->display( ).
ENDFORM. " DISPLAY_ALV
*&---------------------------------------------------------------------*
FORM enable_layout_settings.
*&---------------------------------------------------------------------*
DATA layout_settings TYPE REF TO cl_salv_layout.
DATA layout_key TYPE salv_s_layout_key.
layout_settings = alv->get_layout( ).
layout_key-report = sy-repid.
layout_settings->set_key( layout_key ).
layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM. "enable_layout_settings
************************************************************************
* SUBROUTINES - END *
************************************************************************