TABLE OF CONTENT
Part I. - Simple List
Part II. - Enable Layout Settings
Part III. - Optimize Column Width
Part IV. - Individual Column Settings
Part V. - Set Toolbar
Part VI. - Display Settings
Part VII. - Aggregations
DISPLAY ALV LIST EASILY IN ABAP USING CL_SALV_TABLE PART I. - SIMPLE LIST
10. September 2014 in [1] CL_SALV_TABLE, ABAP Editor, Classical ABAP Developer by abapmentor
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.
Source Code in 7.4 OO ABAP Version
Handling Hotspot Click Event
Adding Custom Button to the Toolbar
Handling Custom Button Clicks [NEW]
DOWNLOAD BONUS CONTENT
OVERVIEW
As an ABAP Developer, we often have to develop ABAP reports that displays some data from the database. These programs usually consists of the following three parts: selection screen, database query, and ALV list. I used to get headache even if I had to implement a simple ALV list, since it's not simple as the 1x1. I mostly used the standard function modules, such as REUSE_ALV_LIST_DISPLAY and the REUSE_ALV_HIERARCHICAL_LIST_DISPLAY. Let's face it, the field catalogs are unnecessary, the settings are over-complicated.
Fortunately, in SAP Netweaver 2004, SAP introduced a new Object Oriented ALV list family class, called CL_SALV. It consists of different ALVs such as table, hierarchy, and tree. In this blog post series, we will focus on the CL_SALV_TABLE class. I will demonstrate you different settings one by one that you can use later to customize your ALV lists.
If we check the class CL_SALV_TABLE in the SE24 transaction, we can find many methods that we can use to customize our ALV list.
In this blog post series, we will cover all the most commonly used settings. We are going to use the CL_SALV_TABLE's instance methods to get an instance of each CL_SALV classes that will help us to customize our ALV.
DEVELOPMENT WORKFLOW
In this blog post series, I will use the standard SAP Flight Model to demonstrate the usage of the CL_SALV_TABLE class, within this model I will use the SPFLI database table that contains a complete flight schedule (if your SPFLI database table is not filled with data, then first let's run the ABAP program, SAPBC_DATA_GENERATOR that will generate sample data for you).
As you know me, I like the transparent and clean code. In order to achieve this, we are going to organize the different responsibilities into small subroutines, like the get_flight_schedule, the initialize_alv, and the display_alv.
So first, we are going to query the actual flight schedule from the database, then initialize an ALV object, and finally display the ALV on the screen.
...
*----------------------------------------------------------------------*
• START-OF-SELECTION event *
*----------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM get_flight_schedule.
PERFORM initialize_alv.
PERFORM display_alv.
...
GET THE LIST OF FLIGHT SCHEDULE
In this subroutine, we are going to query the top 100 rows from the database table, SPFLI and save the received records into an internal table, flight_schedule.
...
*&---------------------------------------------------------------------*
FORM get_flight_schedule.*&---------------------------------------------------------------------*
SELECT * FROM spfli INTO TABLE flight_schedule UP TO 100 ROWS.ENDFORM. " GET_FLIGHT_SCHEDULE...
To avoid any error, we need to declare flight_schedule as an internal table. Now, I am going to declare it in the global declaration area for the sake of the simplicity.
...
*----------------------------------------------------------------------*
• INTERNAL TABLE definitions *
*----------------------------------------------------------------------*
DATA flight_schedule TYPE STANDARD TABLE OF spfli.
...
INITIALIZE THE ALV OBJECT
To use the OO CL_SALV_TABLE class, we need to create an instance of it. We can instantiate it by calling its factory() method (Factory Design Pattern) that requires an ALV parameter (we get back the reference in this parameter), and an internal table filled with business data that we want to display. To avoid any error, SAP recommends to wrap the instantiation into a try-catch block.
...
*&---------------------------------------------------------------------*
FORM initialize_alv.*&---------------------------------------------------------------------*
DATA message TYPE REF TO cx_salv_msg.
TRY.cl_salv_table=>factory(IMPORTING
r_salv_table = alvCHANGING
t_table = flight_schedule ).
CATCH cx_salv_msg INTO message." error handling
ENDTRY.ENDFORM. " INITIALIZE_ALV...
As we declared earlier the internal table, flight_schedule, we need to declare the alv variable also. We are going to define it as reference variable to the class, CL_SALV_TABLE.
...
*----------------------------------------------------------------------*
• REFERENCE definitions *
*----------------------------------------------------------------------*
DATA alv TYPE REF TO cl_salv_table.
...
DISPLAY THE ALV LIST
We finally got over the hump, so from now on our task is very easy. We need to display the ALV list on the screen simply by calling the instance method, display(). It was easy, right? That's what I like in this approach. Effective, transparent, and easy-to-use.
...
*&---------------------------------------------------------------------*
FORM display_alv.*&---------------------------------------------------------------------*
alv->display( ).
ENDFORM. " DISPLAY_ALV...
THE RESULT
After running the complete program, we get the following result. I know, it's really undecorated, but who said it's finished? There is no toolbar, total rows, optimized columns, and so on. That's what we are going to accomplish in the near future.
GET THE COMPLETE SOURCE CODE
• Program: ZDEMO_ALV01 *
• Request ID: RXXXXXX *
• User ID: ALEXGONCZY *
• Date: 2014.09.08. *
• 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 *
...