ALV (第1页)

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.

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]

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 = alv

CHANGING

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

Here is 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 *

************************************************************************

************************************************************************

* 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 ).

CATCH cx_salv_msg INTO message.

" error handling

ENDTRY.

ENDFORM. " INITIALIZE_ALV

*&---------------------------------------------------------------------*

FORM display_alv.

*&---------------------------------------------------------------------*

alv->display( ).

ENDFORM. " DISPLAY_ALV

************************************************************************

* SUBROUTINES - END *

************************************************************************

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 II. - ENABLE LAYOUT SETTINGS

17. 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.

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]

DOWNLOAD BONUS CONTENT

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:

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 *

************************************************************************

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 III. - OPTIMIZE COLUMN WIDTH

24. 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.

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]

DOWNLOAD BONUS CONTENT

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.

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 IV. - INDIVIDUAL COLUMN SETTINGS

6. October 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.

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]

DOWNLOAD BONUS CONTENT

OVERVIEW

In the latest post, I introduced you the CL_SALV_COLUMNS_TABLE class that is responsible for customizing the columns collectively. Today, I want to show you another class, called CL_SALV_COLUMN. This class is responsible for customizing a chosen column individually. To perform some customization on a given column, we have to get an instance reference of the CL_SALV_COLUMN class by calling the method GET_COLUMN() on the CL_SALV_COLUMNS_TABLE class (we already have an instance reference of it).

CODE REFACTORING

When I worked on the previous post - I'm going to be honest with you - I did not plan to show you the CL_SALV_COLUMN class, so I had to rethink my code, before we jump into the middle of coding.

First, I extracted the definition of the reference variable, called columns (CL_SALV_COLUMNS_TABLE) from the OPTIMIZE_COLUMN_WIDTH subroutine into the global definition area for the sake of the simplicity, since we are going to reuse this instance later several times.

...

*----------------------------------------------------------------------*

* REFERENCE definitions *

*----------------------------------------------------------------------*

DATA alv TYPE REF TO cl_salv_table.

DATA columns TYPE REF TO cl_salv_columns_table.

...

Then, I moved the initialization of the columns reference variable from the OPTIMIZE_COLUMN_WIDTH subroutine into the INITIALIZE_ALV subroutine, since we want to initialize only once, and reuse it several times, so I think it belongs to this place.

...

*&---------------------------------------------------------------------*

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 ).

columns = alv->get_columns( ).

PERFORM enable_layout_settings.

PERFORM optimize_column_width.

...

At last, this is the result of the OPTIMIZE_COLUMN_WIDTH subroutine. It's short, compact, and transparent! That's what I like.

...

*&---------------------------------------------------------------------*

FORM optimize_column_width.

*&---------------------------------------------------------------------*

columns->set_optimize( ).

ENDFORM. "OPTIMIZE_COLUMN_WIDTH

...

HIDE UNNECESSARY COLUMNS

返回ABAP参考资料列表