Using The SALV OO Class – Changing how the ALV Grid is Displayed

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.

Recap from last Month

In the previous post Using The SALV OO Class – Adding Functions to the ALV ToolBar, we added functions to the standard toolbar delivered with the ALV Grid functionality (see below).

ALV with Tool Bar

In keeping in line with developing a feature-rich ALV grid for our users, in this post we are going to work on the appearance of the ALV. Specifically adding a custom title, and making the ALV more readable.

Adding a custom title to the ALV with the SALV Classes

I can hear you now… your like… wait we have a title! It’s “Demo for using SALV Class to Create ALV Output”.  Well, yes you are correct. But where did that title come from? It’s the title from the program ZSALV_DEMO02. You can see this if you use the menu path Goto->attributes in se38. The ABAP runtime processor will use this if no custom title is present. It will do the same in the classic ABAP List View as well.

SE38 Attributes

 

We can, thankfully, easily add our own title to the ALV quite easily. We first need to declare a new object reference variable GR_DISPAY.

SALV GR_DISPLAY

Next, we can receive the object using the GET_DISPLAY_SETTINGS method of the GR_TABLE object.

SALV Get_Display_Settings

 

Finally all we need to do is  use the SET_LIST_HEADER method of the GR_DISPAY object to initialize our custom heading.

SALV SET_LIST_HEADER

Now when we execute the program, we get output that looks like this (see below)

ALV with Custom Header

 

Displaying the ALV grid with ZEBRA pattern.

In the Classic (old, very old) days of programming we used to print our reports out on something called Green Bar paper. We did this, in theory, to allow the consumer of the report to have better visibility to each of the lines on the report. Each line is printed on white, then green, etc…etc…  Even so we still made mistakes reading the report. What’s that saying — To err is human – and to blame it on a computer is even more so.

The paper looked like the screen shot below.

SALV Green bar paper

Well SAP hit the America’s back in the early 90’s and was originally a mainframe system, so guess what we can do with our ALV grids? You guessed it, we can create a striped pattern for our ALV grid. maybe not green & white, but blue and white. This has been affectionately dubbed the ZEBRA pattern. Here is how to do it!

In the previous example, we declared an object reference called GR_DISPLAY typed to the SALV Class cl_salv_display_settings. We can use this again to simply call the method SET_STRIPED_PATTERN of the object GR_DISPLAY. If we check the SET_STRIPED_PATTERN() method, we find that it requires a parameter with the type SAP_BOOL.  We have seen this before, this is either ABAP_TRUE or ABAP_FALSE. So lets add the code.

SALV Striped Method Call

Now run the program. Wait it looks the same! Remember we are in the ALV GRID format. If we want to see the stripes, we can easily use the print preview from the menu path List->Print Preview (shown below).

SALV List Print Preview

 

So here is what the SALV ABAP OO List preview looked like BEFORE  our code was added.

SALV Printed Regular

And after….

SALV Printed Striped

In Summary

We learned how to add a custom heading to our ALV Grid and how to use the somewhat outdated, but still useful, zebra pattern all using the delivered SALV Classes and ABAP OO ALV.

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

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 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_table->display( ).
ITP logo

If you enjoyed this blog, Using The SALV OO Class – Changing how the ALV Grid is Displayed, 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!