Scroll Top

Using The SALV OO Class – User Managed Layouts

Anthony 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 [email protected].

Introduction

In keeping in line with developing a feature-rich ALV grid for our users, in this final post we are going to add the LAYOUT functionality that will allow the changes made thus far by a user to be saved, and used again when the user logs on and runs our report. This is very much like how a variant is used for input; a Layout applies to how the output is formatted, sorted, filtered and displayed.

Adding User Managed Layouts to The ALV

Before we discuss how to add this functionality to our existing ALV code, lets look at what a user has the ability to do with our code at present. Below is a screen shot of the current SAP ALV Grid report, notice the RED highlighting around the cubed box. This is the “Change ALV Layout Button”.

Change ALV Layout

Lets go ahead and click this button. The below windowed dialog will open.

ALV Layout Dialog

Notice that the Current Layout field is grayed out and cannot be edited. But, if we look across the tabs, we see we have a variety of ways to customize our UI experience with this ALV grid. In the DISPLAYED COLUMNS Tab, we can choose what columns to display. In the SORT ORDER Tab, we can choose what columns to sort on, whether ascending or descending, and whether to calculate sub-totals. In the FILTER Tab, we can choose to filter the results on certain fields and their contents. The VIEW Tab allows us to choose In-Line EXCEL or the SAP List Viewer. Finally in the DISPLAY Tab, we can choose to show grid-lines, optimize column width…etc.

So let’s go ahead and make some changes… I will remove the column MANDT. I will add an ascending sort by AIRLINE and remove Subtotals. Since I only care about American Airlines, I will create a filter for just that airline. Finally I will optimize the width. After I have clicked through the Tabs and applied all of these settings, I finally exit the dialog and now my ALV grid looks like this.

Sorted_Filtered ALV

OK, this is exactly how I want to see the report the next time I execute it. But if I back out and try again, the report will default back to the original format. What happened to my layout? It would be nice to be able to quickly apply it again, without having to run through all the tabs. In fact, I may have a need for different layout versions, each a little different, that I want to choose from. How can I give a user functionality so they can not just change a layout, but save the changes as well? What about allowing a user to choose what layout they would like to superimpose onto the grid data? Read On…

Adding Layout Sets to an ALV with the SALV Classes

As always I will first show you the separate lines of code we need to add, the the entire program. let’s begin!

First we need to create the Object Reference Variable.

ALV Layout Class

Next, we need to receive the object GR_LAYOUT using the GET_LAYOUT method of the GR_TABLE object. This gives us a key structure typed to SALV_S_LAYOUT_KEY. We will set and save this key in a minute.

ALV Layout reference method

OK, so now we need to set and save this key so I can choose it the next time I want to use it. I need a working variable, I will call it LV_KEY and type it to the structure SALV_S_LAYOUT_KEY.

ALV declare layout key

Then I need to assign this REPORT (SY-REPID) to the KEY Structure Variable REPORT. Once I have done this, I simply call the method SET_KEY of the GR_LAYOUT object with the parameter LV_KEY and set the save restriction using the SET_SAVE_RESTRICTION method. I am using a constant from the interface IF_SALV_C_LAYOUT. The value is “3” and the alias is RESTRICT_NONE. This restriction has to do with whether this layout will be applicable to just my USER-ID or available for all users. More on this a little later….

ALV set and save layout key

That’s it! Now we have added the ability to SAVE and Choose a saved LAYOUT. Now when we run the program, we can see two new buttons to the right of the Change Layout button on the Toolbar. Choose Layout and Save Layout. (see below).

ALV with Layout Functions

Lets take these buttons out for a spin… Again, using the Change Layout button, I will remove the column MANDT. I will add an ascending sort by AIRLINE and remove Subtotals. Since I only care about American Airlines, I will create a filter for just that airline. Finally I will optimize the width. After I have clicked through the Tabs and applied all of these settings, I finally exit the dialog and now my ALV grid looks like this.

ALV cusomized Layout

Now using the SAVE LAYOUT Button ALV Save Layout Button I can save this customization. When I click this button, I am shown a windowed dialog asking for the layout name, description, and whether I would like this to be user specific. You have the option, if the report is shared and you want a group of users to be able to access this customization, to uncheck this box.

I will fill out the details and hit the green check. This will save the report. (see below)

ALV Save Layout Dialog

Now let’s remove all our customization’s by exiting and executing the report. use /NSE38. Your screen should look like the one below.

ALV with Layout Functions

Now let’s Click on the Choose Layout Button ALV Choose Layout Button.  You should see a windowed dialog like the one below. There is are saved layout!

ALV Choose Layout Dialog

Now go ahead and click on it and you will see the layout is instantly applied. (see below)

ALV cusomized Layout

In Summary

In this final post in the series, we learned how to add code that allows a user the ability to CHANGE, SAVE, and CHOOSE  a LAYOUT , 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: gr_layout    TYPE REF TO cl_salv_layout.

 ************************************************************************
 **   Data Declarations                                                **
 ************************************************************************
 DATA: it_spfli TYPE TABLE OF spfli.
 DATA: lv_key   TYPE salv_s_layout_key.

 ************************************************************************
 **   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_layout =  gr_table->get_layout( ).

   lv_key-report = sy-repid.
   gr_layout->set_key( lv_key ).
   gr_layout->set_save_restriction( cl_salv_layout=>restrict_none ).

   gr_table->display( ).
ITP logo

If you enjoyed this blog, Using The SALV OO Class – User Managed Layouts, 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

Related Posts

Pin It on Pinterest

Share This

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