ALV (第3页)

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

...

Since, it doesn't make too much sense to total the Flight Time, instead we are going to request the maximum of these values, via the IF_SALV_C_AGGREGATION interface that has five predefined attributes:

As a last step, we set the location of the aggregation row to display before the items, via the SET_AGGREGATION_BEFORE_ITEMS() method. That's it! It wasn't too complicated, was it?

...

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

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

...

COMPLETE SOURCE CODE

Get the complete source code:

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

* Program: ZDEMO_ALV07 *

* Request ID: RXXXXXX *

* User ID: ALEXGONCZY *

* Date: 2014.11.05. *

* Description: - *

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

REPORT zdemo_alv07.

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

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

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

layout_settings->set_default( if_salv_c_bool_sap=>true ).

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

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

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

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

* SUBROUTINES - END *

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

SUMMARY

As a result, we got our aggregation row, highlighted with yellow color at the top of the flight schedule items, indicating the maximum of the flight times.

Alright, that was my CL_SALV_TABLE tutorial series. Using these code snippets and source codes, you are able to construct decorated, and feature-rich ALV lists about less than 30 minutes with readable and transparent source code. I know that I haven't showed you everything about CL_SALV_TABLE, but we have touched the most common settings, and based on this knowledge, I know that you can easily enable other settings as well. That's all! Stay tuned, I keep posting!

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

IF_SALV_C_LAYOUT ATTRIBUTES DESCRIPTION
RESTRICT_NONE User can save layouts without restriction
RESTRICT_USER_DEPENDANT User can save layouts for herself/himself
RESTRICT_USER_INDEPENDANT User can save layout for all users
IF_SALV_C_AGGREGATION ATTRIBUTES DESCRIPTION
TOTAL Gives the total of the values
MINIMUM Gives the minimum of the values
MAXIMUM Gives the maximum of the values
AVERAGE Gives the average of the values
NONE No aggregation
返回ABAP参考资料列表