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
Today, we are going to move forward, and continue decorating our ALV list. In my previous post, I showed you how to enable the layout settings for users in the ALV, although it's a nice feature, but as you could see our ALV still looks like so crude. So let's do something! What do you think about optimizing the column width? To customize the columns of the ALV, we have to use the class, CL_SALV_COLUMNS_TABLE by getting an instance of it with the method of the CL_SALV_TABLE, called GET_COLUMNS().

Place of ALV Settings
As always, first we are going to place the call of our next setting (OPTIMIZE_COLUMN_WIDTH) in the subroutine, called INITIALIZE_ALV. As we move forward, we are going to place the call of these setting subroutines one by one under each other.
...
*&---------------------------------------------------------------------*
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 optimize_column_width.
" PERFORM setting_3.
" ...
" PERFORM setting_n.
CATCH cx_salv_msg INTO message.
" error handling
ENDTRY.
ENDFORM. " INITIALIZE_ALV
...
Optimize Column Width
As I mentioned above, to customize the columns of an ALV, we need an instance of the class, CL_SALV_COLUMNS_TABLE that we are going to get by calling the CL_SALV_TABLE method, called GET_COLUMNS(), and save the received instance reference in a reference variable, named COLUMNS (it refers to the class, CL_SALV_COLUMNS_TABLE ).
...
*&---------------------------------------------------------------------*
FORM optimize_column_width.
*&---------------------------------------------------------------------*
DATA columns TYPE REF TO cl_salv_columns_table.
columns = alv->get_columns( ).
columns->set_optimize( ).
ENDFORM. "OPTIMIZE_COLUMN_WIDTH
...
As we get the required instance, there is nothing left, than calling its method, named SET_OPTIMIZE(). That's obvious, isn't it? We want to optimize the width of the columns, so that we call the method, SET_OPTIMIZE(). It has an optional importing bool parameter, named VALUE that is true by default.

Let's check that code! Since the importing parameter of the SET_OPTIMIZE() is true by default, we are going to call it simply without any parameter.
...
*&---------------------------------------------------------------------*
FORM optimize_column_width.
*&---------------------------------------------------------------------*
DATA columns TYPE REF TO cl_salv_columns_table.
columns = alv->get_columns( ).
columns->set_optimize( ).
ENDFORM. "OPTIMIZE_COLUMN_WIDTH
...
The Result
Before optimizing the columns width.

After optimizing the columns width. It looks better, right?

Complete Source Code
Get the complete source code:
************************************************************************
* Program: ZDEMO_ALV03 *
* Request ID: RXXXXXX *
* User ID: ALEXGONCZY *
* Date: 2014.09.22. *
* Description: - *
************************************************************************
REPORT zdemo_alv03.
************************************************************************
* 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 optimize_column_width.
" 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
*&---------------------------------------------------------------------*
FORM optimize_column_width.
*&---------------------------------------------------------------------*
DATA columns TYPE REF TO cl_salv_columns_table.
columns = alv->get_columns( ).
columns->set_optimize( ).
ENDFORM. "OPTIMIZE_COLUMN_WIDTH
************************************************************************
* SUBROUTINES - END *
************************************************************************
Summary
As a summary I want to show you a snapshot about the instance members of the CL_SALV_TABLE class that hopefully clarifies the architecture of this class. On the picture below, you can see many familiar class, such as CL_SALV_LAYOUT, CL_SALV_COLUMNS, and so on. The CL_SALV_TABLE stores one-one instance of these classes, and you can reach them by calling their GETTER methods, like GET_LAYOUT, GET_COLUMNS, and so on. As you get the references of these instances, you can set their properties to perform the required settings.

Let's check our current example:
- we have called the GET_COLUMNS method of the CL_SALV_TABLE class that returned back an instance reference of the CL_SALV_COLUMNS_TABLE class (we stored this reference in the COLUMNS variable)
- then we used this instance and called its method, SET_OPTIMIZE()
That's the trick of the CL_SALV_TABLE class. I hope you liked it! Next, we are going to deal with the columns again, but we are going to customize them individually.