Using The SALV OO Class – Updating Grid Columns, Sorting, and Aggregation

97be63abb66b42f281dff9197b6b35da_WglrP9Anthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 17 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

Introduction

In keeping in line with developing a feature-rich ALV grid for our users, in this post we are going to work further on the appearance and functionality of the ALV. Specifically adding a custom title to column of the grid, sorting the grid, and aggregating totals.

Adding a custom title to a column of an ALV with the SALV Classes

The column title we see displayed on the grid now is defaulted in from the DATA ELEMENT definition of the field the column is referencing.

So in our SAP ALV Grid example, our grid is for the SPFLI (flight schedule table). Lets look at the grid again and choose a column, first to understand how the title of the column is being generated, and then how we could impact or change that title.

Lets look at the “Dest.” column. First notice even though its currently titled as “Dest.” if I hover my mouse to the hard right edge of the column and drag the boundary to the right, you will see the title change from “Dest.” to “Arrival City”.

ALV Column title

After dragging the right edge …..

ALV Column new title

How is this possible? Well lets go back and look at table SPFLI in SAP using Tcode SE11. The arrival city is the field CITYTO and data element S_TO_CITY.

ALV CITYTO

Double click this Data Element. The screen that appears will have a Tab “Field Label”. lets navigate to this Tab and discuss what we see. Ok, it appears to have Field label text that will be used when SAP considers it needs “SHORT (Max length of 10)”, MEDIUM (Max length of 17), LONG (Max length of 20) and HEADING (Max length of 20). Please note SHORT Field label text is “Dest.” and MEDIUM field text is “Arrival City”.

ALV field label text

And so we have our answer. The title of the column is tied to the Data Element definition of the field in the table structure the ALV grid is displaying. This is why when the grid was first displayed, and the column length was 10 bytes or less, we saw a column title of “Dest.” But after we slide the right edge over to the right and exceeded 10 bytes, the title dynamically changed to “Arrival City”.

Now that we know how and where the column title is defaulted from, lets create a CUSTOM title for this column in our code.

First we need to create the Object Reference Variables.

ALV Object Ref Variables

Next, we need to receive the object GR_COLUMNS using the GET_COLUMNS method of the GR_TABLE object. This will pass you the object for all columns of the ALV grid. We do this using an assign ABAP format with our new refernece object variable GR_COLUMNS as the receiving variable..

ALV Columns

Because we are interested in ONLY one column in this example, the CITYTO column, we call the method GET_COLUMN from the GR_COLUMNS object. In this example, we are accessing the CITYTO column. We have to widen the cast (?=) in order to operate on the column. Here is a good article from the ABAP SCN area to help get your head around casting.

ALV Column Widen Cast

Now we can set all of our SHORT, MEDIUM, and LONG Labels with our custom text in order to override the defaults.

ALV label setting

Now when we run the program, we can see the column heading contains our new custom text!

ALV with Custom Column

 

Adding Sorting to an ALV with the SALV Classes

Let keep enriching the functionality of this little grid. Let’s say we wanted to sort a specific column for our user so when the grid would pop up, it would be sorted for them. Since we are already using the CITYTO field, lets sort on that!

First we need to add the Object Reference variable…

ALV Sorting Object Ref

Now we can receive the object using the GET_SORTS method of the GR_TABLE object. Then we just add the sort for the field CITYTO, by calling the ADD_SORT method of the GR_SORTS object.

Alv Sorting

Now when we run the program again, the grid is sorted by the destination city. This is confirmed by the white space you see for rows where the CITYTO field is the same.

ALV grid Sorted

Adding Aggregation to an ALV with the SALV Classes

Finally we will close this month’s blog by adding subtotaling functionality to teh grid. We have the grid already sored by the CITYTO field, so lets use that as our subtotaling break. The only real numeric column in the grid we can total by is DISTANCE. While the output won’t make much business sense, it will demonstrate the concept.

So lets add the Object Reference Variable…

ALV Totaling Object Ref

We need to modify the call to ADD_SORT to set the SUBTOTAL = ABAP_TRUE. This informs the grid we intend to subtotal at the CITYTO field.

ALV CITYTO Subtotal

Now we can receive the object using the GET_AGGREGATIONS method of the GR_TABLE object, and add the aggregation by calling the ADD_AGGREGATION method of the GR_AGG object and specifying the column we want to aggregate.

ALV cityto Aggregation

Run the program again and your output should look like the below screen shot. Notice how it aggregates each unit of measure (Miles vs. Kilometers) separately.

ALV Aggregate

 

In Summary

We learned how to add a custom heading to an ALV grid column. We also learned how to SORT & Aggregate on a column of the grid, making full use of the ABAP OO SALV Model.

Below is the full code for you to COPY & PASTE.

*&---------------------------------------------------------------------*
*& Report ZSALV_DEMO01
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zalvom_demo1.
************************************************************************
** Global References for Classes **
************************************************************************
DATA: gr_table     TYPE REF TO cl_salv_table.
DATA: gr_functions TYPE REF TO cl_salv_functions.
DATA: gr_display   TYPE REF TO cl_salv_display_settings.
DATA: gr_columns   TYPE REF TO cl_salv_columns_table.
DATA: gr_column    TYPE REF TO cl_salv_column_table.
DATA: gr_sorts     TYPE REF TO cl_salv_sorts.
DATA: gr_agg       TYPE REF TO cl_salv_aggregations.
************************************************************************
** Data Declarations **
************************************************************************
DATA: it_spfli TYPE TABLE OF spfli.
************************************************************************
** Processing Blocks **
************************************************************************
START-OF-SELECTION.
SELECT * INTO TABLE it_spfli FROM spfli.
cl_salv_table=>factory( IMPORTING r_salv_table = gr_table CHANGING t_table = it_spfli ).
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
gr_display = gr_table->get_display_settings( ).
gr_display->set_list_header( 'This is our custom heading' ).
gr_display->set_striped_pattern( abap_true ).
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'CITYTO' ).
gr_column->set_long_text( 'long text example' ).
gr_column->set_medium_text( 'med text example' ).
gr_column->set_short_text( 'short text' ).
gr_sorts = gr_table->get_sorts( ).
gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ).
gr_agg = gr_table->get_aggregations( ).
gr_agg->add_aggregation( 'DISTANCE' ).
gr_table->display( ).
ITP logo

If you enjoyed this blog, Using The SALV OO Class – Updating Grid Columns, Sorting, and Aggregation, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!