ALV (第2页)

As you have completed the necessary code refactoring as well, let's move on and work on our current task, perform individual setting on certain columns. As you could see, we display all of the columns from the SPFLI table in our ALV list without any restriction. Now, I want to teach you how you can hide the unnecessary columns in your ALV list. As an example, let's hide the column, MANDT since it's a technical fields, the user doesn't interested in the content of this fields.

It's going to be very simple: first of all let's get the reference of the MANDT column by calling the method, GET_COLUMN() on the columns object (columns contains all of the columns from the table, SPFLI), and then let's call its method, named SET_VISIBLE() with false. At last, let's wrap these method calls into a TRY-CATCH block to handle exceptions (now we have to handle the case when doesn't exist any field in the structure with the name, MANDT).

...

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

FORM hide_client_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'MANDT' ).

column->set_visible( if_salv_c_bool_sap=>false ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " HIDE_CLIENT_COLUMN

...

Above, we used a reference variable, named column that we have not declared yet. Let's place its declaration in the global area for the sake of the simplicity with the type CL_SALV_COLUMN.

...

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

* REFERENCE definitions *

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

DATA alv TYPE REF TO cl_salv_table.

DATA columns TYPE REF TO cl_salv_columns_table.

DATA column TYPE REF TO cl_salv_column.

...

SET TEXT FOR COLUMNS

Beside the field's visibility, I want to show you another setting that you can use to reset column's texts (that is specified in the ABAP Dictionary) easily by coding.

Here, we are going to do the same thing. First let's get the reference of the COUNTRYFR field by calling the GET_COLUMN() method, and save it in the variable column. Then call the different text setting method according to the lengths: short, medium, long with the different texts that you want to set. At last, handle the CX_SALV_NOT_FOUND exception, in the case when there is no field with the name COUNTRYFR.

...

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

FORM set_departure_country_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'COUNTRYFR' ).

column->set_short_text( 'D. Country' ).

column->set_medium_text( 'Dep. Country' ).

column->set_long_text( 'Departure Country' ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " SET_DEPARTURE_COUNTRY_COLUMN

...

COMPLETE SOURCE CODE

Get the complete source code:

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

* Program: ZDEMO_ALV04 *

* Request ID: RXXXXXX *

* User ID: ALEXGONCZY *

* Date: 2014.10.01. *

* Description: - *

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

REPORT zdemo_alv04.

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

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

DATA columns TYPE REF TO cl_salv_columns_table.

DATA column TYPE REF TO cl_salv_column.

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

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

columns = alv->get_columns( ).

PERFORM enable_layout_settings.

PERFORM optimize_column_width.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

" ...

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

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

columns->set_optimize( ).

ENDFORM. "OPTIMIZE_COLUMN_WIDTH

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

FORM hide_client_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'MANDT' ).

column->set_visible( if_salv_c_bool_sap=>false ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " HIDE_CLIENT_COLUMN

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

FORM set_departure_country_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'COUNTRYFR' ).

column->set_short_text( 'D. Country' ).

column->set_medium_text( 'Dep. Country' ).

column->set_long_text( 'Departure Country' ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " SET_DEPARTURE_COUNTRY_COLUMN

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

* SUBROUTINES - END *

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

SUMMARY

Finally, that's what we worked on the last few minutes: column Mandt is hidden, the column Departure Country's texts are renamed.

I think, we are on the good way :) You are getting close to master the CL_SALV_TABLE class step by step. First, we implemented a simple list, then we enabled the layout settings, after that we optimized the column width, and now we hid the MANDT field, and set the texts for the COUNTRYFR field.

Stay tuned, I'm working on the next post!

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 V. - SET TOOLBAR

13. 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 previous post, we successfully performed some individual column settings, like hiding unnecessary fields, and changing the text of some fields. So now, we have a good-looking ALV list, but we cannot say that our ALV is strong in functionality.

So today, I going to share with you that how we can add additional functionality to our toolbar on the top of the list, beside the existing layout settings. To bring it to life, today we are going to use the class, CL_SALV_FUNCTIONS_LIST that is responsible for customizing the toolbar. Using this class, we are able to create custom functions and add it to the toolbar, or we can use standard ones, like sorting, filtering, and so on. In this demo, I am going to show you, how to enable the standard toolbar.

SET TOOLBAR

As always, we are going to create a new subroutine for our next setting, called SET_TOOLBAR, and place its perform call in the usual INITIALIZE_ALV subroutine.

...

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

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.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

PERFORM set_toolbar.

" ...

" PERFORM setting_n.

...

The SET_TOOLBAR subroutine is going to be very easy, we need only 3-4 lines of code to implement it. First, we are going to use the GET_FUNCTIONS() method of the CL_SALV_TABLE class that returns back a reference to its CL_SALV_FUNCTIONS_LIST object. Then, we are going to save this reference into a variable, called functions with the type CL_SALV_FUNCTIONS_LIST. At last, we call the method SET_ALL() on the previously initialized functions reference variable that is responsible to set all of the standard ALV functions in our toolbar.

...

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

FORM set_toolbar.

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

DATA functions TYPE REF TO cl_salv_functions_list.

functions = alv->get_functions( ).

functions->set_all( ).

ENDFORM. " SET_TOOLBAR

...

COMPLETE SOURCE CODE

Get the complete source code:

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

* Program: ZDEMO_ALV05 *

* Request ID: RXXXXXX *

* User ID: ALEXGONCZY *

* Date: 2014.10.13. *

* Description: - *

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

REPORT zdemo_alv05.

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

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

DATA columns TYPE REF TO cl_salv_columns_table.

DATA column TYPE REF TO cl_salv_column.

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

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

columns = alv->get_columns( ).

PERFORM enable_layout_settings.

PERFORM optimize_column_width.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

PERFORM set_toolbar.

" ...

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

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

columns->set_optimize( ).

ENDFORM. "OPTIMIZE_COLUMN_WIDTH

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

FORM hide_client_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'MANDT' ).

column->set_visible( if_salv_c_bool_sap=>false ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " HIDE_CLIENT_COLUMN

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

FORM set_departure_country_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'COUNTRYFR' ).

column->set_short_text( 'D. Country' ).

column->set_medium_text( 'Dep. Country' ).

column->set_long_text( 'Departure Country' ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " SET_DEPARTURE_COUNTRY_COLUMN

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

FORM set_toolbar.

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

DATA functions TYPE REF TO cl_salv_functions_list.

functions = alv->get_functions( ).

functions->set_all( ).

ENDFORM. " SET_TOOLBAR

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

* SUBROUTINES - END *

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

SUMMARY

At last, let's run our program and check the results: a new toolbar appears on the top of the list. That's it! :)

Stay tuned, I'm working on the next post!

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 VI. - DISPLAY SETTINGS

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

I think, we are getting closer to have a nice, feature-rich ALV list week by week. In the previous post, I showed you that how to enable the standard toolbar on the top of our ALV list.

Today, we are going to work on the appearance, namely I'm going to show you that how set the header of the ALV list, and how to set the rows striped. Alright, we have the requirements, but how to satisfy them? Easy, and you already know it, we have another subclass of the CL_SALV_TABLE, called CL_SALV_DISPLAY_SETTINGS that is responsible for the appearance of our ALV list.

DISPLAY SETTINGS

As always, we are going to place the call of our new subroutine, called DISPLAY_SETTINGS in the usual INITIALIZE_ALV subroutine.

...

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

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.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

PERFORM set_toolbar.

PERFORM display_settings.

" ...

" PERFORM setting_n.

...

The DISPLAY_SETTINGS subroutine is responsible for two things: set the pattern of the rows to striped, and set the header of the ALV list. You can guess. We are going to use the GET_DISPLAY_SETTINGS() method of the CL_SALV_TABLE class that returns back a reference to its CL_SALV_DISPLAY_SETTINGS object. Then, we are going to save this reference into a variable, called display_settings with the type CL_SALV_DISPLAY_SETTINGS.

Next, we call the method SET_STRIPED_PATTERN() on the display_settings reference variable with the parameter true, and as a result, we get the rows with striped pattern.

At last, we call the method SET_LIST_HEADER() on the display_settings reference variable that is responsible to set the header of the ALV list. Cool & Easy, isn't it?

...

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

FORM display_settings.

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

DATA display_settings TYPE REF TO cl_salv_display_settings.

display_settings = alv->get_display_settings( ).

display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).

display_settings->set_list_header( 'Flight Schedule' ).

ENDFORM. " DISPLAY_SETTINGS

...

COMPLETE SOURCE CODE

Get the complete source code:

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

* Program: ZDEMO_ALV06 *

* Request ID: RXXXXXX *

* User ID: ALEXGONCZY *

* Date: 2014.10.22. *

* Description: - *

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

REPORT zdemo_alv06.

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

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

DATA columns TYPE REF TO cl_salv_columns_table.

DATA column TYPE REF TO cl_salv_column.

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

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

columns = alv->get_columns( ).

PERFORM enable_layout_settings.

PERFORM optimize_column_width.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

PERFORM set_toolbar.

PERFORM display_settings.

" ...

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

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

columns->set_optimize( ).

ENDFORM. "OPTIMIZE_COLUMN_WIDTH

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

FORM hide_client_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'MANDT' ).

column->set_visible( if_salv_c_bool_sap=>false ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " HIDE_CLIENT_COLUMN

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

FORM set_departure_country_column.

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

DATA not_found TYPE REF TO cx_salv_not_found.

TRY.

column = columns->get_column( 'COUNTRYFR' ).

column->set_short_text( 'D. Country' ).

column->set_medium_text( 'Dep. Country' ).

column->set_long_text( 'Departure Country' ).

CATCH cx_salv_not_found INTO not_found.

" error handling

ENDTRY.

ENDFORM. " SET_DEPARTURE_COUNTRY_COLUMN

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

FORM set_toolbar.

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

DATA functions TYPE REF TO cl_salv_functions_list.

functions = alv->get_functions( ).

functions->set_all( ).

ENDFORM. " SET_TOOLBAR

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

FORM display_settings.

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

DATA display_settings TYPE REF TO cl_salv_display_settings.

display_settings = alv->get_display_settings( ).

display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).

display_settings->set_list_header( 'Flight Schedule' ).

ENDFORM. " DISPLAY_SETTINGS

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

* SUBROUTINES - END *

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

SUMMARY

After running our program we get the following results: the title says 'Flight Schedule', and the rows are striped. That's it! :) I hope you liked it!

Stay tuned, I'm working on the next post!

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 VII. - AGGREGATIONS

4. November 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 successfully set a title for our ALV and set the rows striped. Now, we have arrived to the final post of the series, where I'm going to show you another useful feature of the CL_SALV_TABLE class, called aggregations. Using aggregations, we are able to display the minimum, maximum, average or the total of a key figure column.

In order to set an aggregation on our ALV, we need to ask the CL_SALV_TABLE class via its GET_AGGREGATION method to get an object reference of the CL_SALV_AGGREGATIONS.

That's enough for now, let's jump into the middle, and implement our last setting!

AGGREGATIONS

As always, we are going to place the call of our new subroutine, called SET_AGGREGATIONS in the usual INITIALIZE_ALV subroutine.

...

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

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.

PERFORM hide_client_column.

PERFORM set_departure_country_column.

PERFORM set_toolbar.

PERFORM display_settings.

PERFORM set_aggregations.

CATCH cx_salv_msg INTO message.

" error handling

ENDTRY.

ENDFORM. " INITIALIZE_ALV

...

In the implementation of the SET_AGGREGATION subroutine, we are going to get an object reference of the CL_SALV_AGGREGATIONS, using the GET_AGGREGATION() method of our ALV object (CL_SALV_TABLE object), and save the returning object reference into a reference variable, called AGGREGATIONS that has the type CL_SALV_AGGREGATIONS.

...

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

FORM set_aggregations.

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

DATA aggregations TYPE REF TO cl_salv_aggregations.

aggregations = alv->get_aggregations( ).

TRY.

aggregations->add_aggregation(

columnname = 'FLTIME'

aggregation = if_salv_c_aggregation=>maximum

).

CATCH cx_salv_data_error.

CATCH cx_salv_not_found.

CATCH cx_salv_existing.

ENDTRY.

aggregations->set_aggregation_before_items( ).

ENDFORM. " SET_AGGREGATIONS

...

In the next few lines, we are going to construct a TRY-CATCH block that is responsible to catch the following three exceptions: CX_SALV_DATA_ERROR, CX_SALV_NO_FOUND and CX_SALV_EXISTING.

In the core of the TRY-CATCH block, we call the ADD_AGGREGATION() method of the previously initialized reference variable, called AGGREGATION. The method requires two importing parameters, a column name that we want to aggregate, and an aggregation type.

Although the SPFLI table is not the best for demonstrating the aggregations, but there are some columns that we can use, like the FLTIME, namely Flight Time.

...

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

FORM set_aggregations.

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

返回ABAP参考资料列表