Professional Documents
Culture Documents
Problems with ABAP output and why the ALV Grid Control can help
In this Blog series I will dive deeply into the ALV Grid Control and elaborate on its advantages in ABAP list
processing. Lets begin by looking at some problems with Classical list processing
Within an SAP system there is a lot of data that needs to be reviewed, maintained, and interpreted. You know what
Im talking about inventory data, controlling data, accounting data, employee addresses, and so on and so forth.
The bigger a company, the greater the amount of data that needs to be output for review and analysis.
Within the context of certain applications, the terms output and list can be synonymous. When I took my very first
ABAP class at SAP Education, I learned that the term list denotes an output page that has been created using ABAP
statements like WRITE, POSITION, or FORMAT, and text lines are buffered in a list buffer before a system program
called the ABAP list processor steps in to perform various run-time activities. Lists created in this way are often
referred to as classical lists (everything in SAP that is okd is lovingly termed Classical) and represent the standard
output method of data.
If you have dealt with list formatting issues and/or the use of events for interactive lists, you will find the features and
functions introduced by the new ALV Grid Controlwill make your life a lot simpler.
Granted, the effort to create a classical list for just simple output of data is minimal. But amass a few lists across a
few applications, each created by a different developer, and things get a bit more complicated. The ABAP list
processor lacks standard baseline list handling functions like sorting, filtering, and calculations, so these types of
functions have to be added by hand for each new list. Responsibility for list layout and interface design is also left to
the developer.
This is why you find different schemes for list handling and layout in different applications. Lack of standard, baseline
list processing functions means that developers devise their own methods for common list handling activities such as
headings, sorting, filtering, rendering subtotals, and the like. The result is that end users who work with more than
one application may have to deal with different ways to access these simple list functions based on the developer
who coded the solution.
The ALV Grid Control solves this problem. Its user interface provides a set of generic functions (e.g., sorting, filtering)
for handling tabular data. It also confers the many benefits of controls technology to users, enabling more operations
by mouse, and interaction with other controls, such as drag-anddrop. Developers simply plug the ALV Grid Control
into their applications and the tool takes care of the rest. You do not need to do any further programming to offer
users these functions. How you plug the control into an application is the first thing I will show you in this months
blog. So lets get started!
ALV Grid Control Baseline Functionality
Take a look at the screen shot below; it shows a tab-strip with an ALV Grid Control and its integrated toolbar, which,
thanks to the controls technology, enables mouse-oriented access to a variety of list handling functions.
Columns can be selected and dragged to a different column position. Users can invoke functions that refer to a
selected column (like hide, search, or filter) directly via a context menu. (The selection of columns and rows complies
with the Microsoft standard, which means that users can do things like use the CTRL key to select rows or columns
that are not adjacent.)
Where the ALV Grid Control is placed within a SAP GUI screen is up to you. You can, for example, dock a control to
an arbitrary side of a dynpro. The ABAP Workbench uses this technique in combination with an ALV Grid Control to
dynamically blend in additional information e.g., a global worklist that contains repository objects, or syntax error
messages belonging to the currently shown source code.
Lets take a closer look at the toolbar. See the screen shot below for an overview of the generic functions of the
toolbar.
The user can sort the list by one or more columns, filter entries (e.g., display only data of one day), and calculate
totals and subtotals. Users can determine column order and column length to retrieve needed information in a more
convenient fashion. Developers can allow these settings to be saved in a display variant that can be saved as the
default setting, such that the users preference for settings is loaded before the list is displayed. Some of the
functions made available in the toolbar are there to provide better visualization of lists that either have a lot of
columns or a lot of rows. The Detail function, for example, allows users who are working with a list that has a lot of
columns to view values of a line in a separate pop-up window. For lists that are very long, a user can calculate
subtotals, then collapse all subtotal levels by clicking on the little buttons of the total line. Particular subtotal lines can
be collapsed or expanded, too. Users will also find functions, like the ABC analysis tool, to facilitate the analysis of
data. To print a list, the ALV Grid Control calls the SAP List Viewer. Alternatively, users can export a list to MS Word or
MS Excel. For those who usually work with Excel, the ALV Grid Control offers a function to switch between Grid and
Excel inline display.
Now that you have seen the standard list handling functions that become available to users via the ALV Grid Control,
lets talk about integrating this control with your applications.
So once you have generated an instance of the ALV Grid Control, integrated it into a screen, and selected the data
that is to be displayed, this one simple call
is all that you need to provide a list with a set of generic functions.
Generating an instance of the control and integrating it into a screen is not specific to the ALV Grid Control. You do
this the same way you would any control.
What about selecting the data that is to be displayed and passed to the instance together with a description of the
fields? You need to understand that displaying data via the ALV Grid Control requires, at a minimum, that you provide
the following two types of information: the output table and the field catalog.
The output table is an internal table that contains the data that is to be displayed. In most cases, a structure of the
output table already resides in the ABAP Dictionary. When you pass the structure name to the ALV Grid Control, the
field catalog for the description of fields is generated automatically. As you will soon see in upcoming blogs, the field
catalog can be created in a number of ways. You will also see that some of the fields in this catalog must be
specified. The bottom line is that a structure description of the data that is to be displayed must be declared to the
ALV Grid Control either through the field catalog or the corresponding ABAP Dictionary structure.
Lets finish this blog with a code example. I will assume you will create screen 100 and use a custom container to hold
the ALV Grid Control. Here is the code, I will follow with an explanation:
PROGRAM SIMPLE_ALV_CALL.
DATA: OK_CODE LIKE SY-UCOMM,
G_GRID TYPE REF TO CL_GUI_ALV_GRID,
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
GT_SFLIGHT TYPE TABLE OF SFLIGHT. output table
CALL SCREEN 100.
*************************************************************
MODULE PAI INPUT.
CASE OK_CODE.
WHEN EXIT.
LEAVE PROGRAM.
ENDCASE.
CLEAR OK_CODE.
ENDMODULE.
MODULE PBO OUTPUT.
SET PF-STATUS MAIN100.
* create controls
IF G_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = MYCONTAINER.
CREATE OBJECT G_GRID
EXPORTING I_PARENT = G_CUSTOM_CONTAINER.
*
* ALV specific part starts here
*
* data selection
SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT.
* display output table in ALV instance
CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = SFLIGHT
CHANGING IT_OUTTAB = GT_SFLIGHT.
ENDIF.
ENDMODULE.
We start by calling SCREEn 100. This calls modules PBO and PAI within its flow logic. At PBO time, we define a
status MAIN100 to leave the program with function code EXIT (this is nothing but classic ABAP dialog programming).
We then create an instance of the Custom Container Control and an instance of the
ALV Grid Control. Note that the latter is connected to the first by parameterI_PARENT and that the container control
is placed on an area that has been defined in the Screen Painter with the name MYCONTAINER. Next we pass ALV
the target data that is to be displayed in a list of the flight model
One last point worth noting is that data selection is well separated from the call to display the data. If this is also the
case for old reports that uses the ABAP list processor at your company, it can be easily converted to a report based
on the ALV Grid Control. The more the data selection is mingled with commands to display the data, the greater the
amount of effort required to convert the old report.
Thats it! Now that the control has been integrated on the screen, you will likely want to configure it to do other cool
things. The key to configuring the ALV Grid Control for your particular application is the structures that are passed by
the application to an ALV Grid instance before or during list display. For some simple extensions of your ALV Grid
instance, you only need to set the right parameter and pass the table or structure by using
method set_table_for_first_display. But this is what we will cover next month
When we ended the last blog on the ALV Grid Control I promised that we would cover some simple extensions in
this blog. So lets begin..
The key to configuring the ALV Grid Control for your particular application is the structures that are passed by the
application to an ALV Grid instance before or during list display. For some simple extensions of your ALV Grid
instance, you only need to set the right parameter and pass the table or structure by using
methodset_table_for_first_display.
Once the ALV Grid control has been integrated on the screen, you will likely want to configure it to do things like:
- Calculate totals initially
- Hide columns, the toolbar, the grid title, or the column headers
- Add, replace, or hide functions from the toolbar
- React to a double-click on a row to show a detail list
- Interpret values of a column as icons
Like it was stated above, The key to configuring the ALV Grid Control for your particular application is the structures
that are passed by the application to an ALV Grid Control instance. Below is a list of the structures that can be
passed.
Of the seven structure/tables listed above, the field catalog is the most important one for the ALV Grid Control.
Lets take a look at a specific example. Lets say we want to provide a title to our ALV Grid Control. We also would like
to hide the search capability from the standard toolbar so the user doesnt have that ability.
OK lets grab a code snippet from the previous Blog A Guide to the New ALV Grid Control Part 1. Look at the
bold font lines below.
data: gt_exclude type ui_functions.
data: gs_exclude type ui_func.
data: gs_layout type lvc_s_layo.
gs_exclude = cl_gui_alv_grid=>mc_fc_find.
append gs_exclude to gt_exclude.
gs_layout-grid_title = 'My Grid Title'.
CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = 'SFLIGHT'
IS_LAYOUT = GS_LAYOUT
IT_TOOLBAR_EXCLUDING = GT_EXCLUDE
CHANGING IT_OUTTAB = GT_SFLIGHT.
The structure gs_exclude of type ui_func is used in the internal table gt_exclude of type ui_functions. The g is for
Global and s and t are for structure and table respectively. The search function is referenced by the class attribute
MC_FC_FIND of class CL_GUI_ALV_GRID. You can hide other standard functions or menu buttons as well by
adding other attributes with prefix MC_FC_ or MC_MB_, respectively.
We added the title to gs_layout-grid_title.
Finally to get these changes to manifest in our ALV Grid Control, we added IS_LAYOUT = GS_LAYOUT and
IT_TOOLBAR_EXCLUDING = GT_EXCLUDE to the call statement for the method
SET_TABLE_FOR_FIRST_DISPLAY
Setting Properties Using the Field Catalog in the ALV Grid Control
As mentioned earlier, the ALV determines the output format of values using the field catalog. If you choose to build
the field catalog manually, you need to know which fields are mandatory.
Listed below are the required fields.
Fortunately, in most cases you can generate the field catalog (semi-)automatically, as I will show you, so you dont
need to fill in these fields.
Generating the Field Catalog for the ALV Grid Control
You have several options for building a field catalog:
1)Automatically through an ABAP Dictionary structure (as in our example above) the ALV Grid Control then uses
this structure to generate the field catalog on its own
2)Manually in your ABAP program (by filling at least the mandatory fields for each entry of our output table)
3)Semi-automatically by combining the first two procedures
In the case of the first option, you use parameter I_STRUCTURE_NAME, and in the latter cases IT_FIELDCATALOG
of method set_table_for_first_display.
Since the last option is the most commonly used, it will be the focus of our discussion. When generating the field
catalog semi-automatically, you combine structure information of the ABAP Dictionary with your own structure
information.
This allows you to modify or add structure descriptions of new fields to the automatically generated field catalog,
which may be helpful for the following scenarios:
- You want to display an ABAP Dictionary table without displaying all possible columns initially (using field NO_OUT,
another field of the field catalog).
- You want to display additional columns containing icons or other information.
To generate a field catalog semi-automatically all you need to do is call function module
LVC_FIELDCATALOG_MERGE and pass the ABAP Dictionary structure of the output table and the internal table for
the field catalog.
The function module generates the field catalog and fills the internal table accordingly.
Please remember to read the rows you want to change, and adapt the fields accordingly. If your output table contains
more fields than are stored in the ABAP Dictionary, you must append one row for each new field to the field catalog.
Again, an example will help clear this up. The code snippet below deals again with a list of the good old flight model.
In the code, column PLANETYPE is hidden and an additional column is used to display icons.
TYPE-POOLS: icon.
[...]
data: begin of outtab occurs 0.
include structure sflight.
10
When we ended the last blog on the ALV Grid Control I promised that we would cover adding Application Specific
functions like push buttons or context menu that provides different functions to the ALV Grid Control. I also said we
would learn how we can use Interactive Reporting with the ALV Grid Control. So lets get started!!
You can add the following GUI elements to an ALV Grid instance of an SAP ALV Class:
11
12
13
14
1. Define an event handler method for event BEFORE_USER_COMMAND. This event is triggered after the user has
selected a function. This means that the ALV Grid Control passes control to the application before the function is
executed. Using event parameter E_UCOMM, you can trap and restrict the function code to just the function you want
to modify.
2. Implement your own code for the function within the event handler method. In this context, you can even call
methods of the ALV Grid Control if you need too.
3. Reset the function code to ensure that the standard function is no longer executed. You can use code similar to the
below:
CALL METHOD <instance of the ALV control>->set_user_command
exporting I_UCOMM = SPACE.
Lets end this blog series with a look at how you can incorporate the interactive reporting method we all have fond
memories of from our classical reporting days into the ALV grid control.
Interactive Reporting and the ALV Grid Control
So far our discussions have dealt with functionality provided using classical interface elements, such as push buttons,
toolbar menus, and the context menu. There are other interaction facilities can be integrated into an ALV Grid
instance. Take a look at the chart below
As far as the method to integrate these facilities is concerned, the double-click and the hyperlink facility obviously
stand out a little bit. To respond to a double-click, you just have to implement an event handler method for event
double_click. Hyperlinks do not need any event handling, because the corresponding action is always the same
open a Web browser to show the destination of a link. The links have to be defined in a table of type LVC_T_HYPE,
which is passed to the ALV Grid instance using method set_table_for_first_display.
The way to designate a facility to all cells of a single column is controlled by the relevant field in the field catalog. In
the case of hotspots and push buttons, you must set this field to X or attribute cl_gui_alv_grid=>mc_style_button,
15
respectively. In the case of hyperlinks and drag-and-drop behaviors, a handle must be provided instead. By using this
handle, the ALV Grid Control relates the cells of that column to a defined hyperlink or drag-and-drop behavior.
But what if your requirement is for just some cells of a column to be depicted as push buttons e.g., only those
where a condition of the other values of the same row is met? For this purpose, the ALV Grid Control offers a concept
that might be complicated at first glance, but as it can be applied for other facilities too, you will surely encounter it
more than once when you work frequently with this tool.
The idea is to extend the output table by a field that shall not be visible to the user, but which holds additional
information for specific columns of the current row in the table. So lets take a look at the code below
DATA: BEGIN OF GT_OUTTAB OCCURS 0.
INCLUDE STRUCTURE <Your ABAP Dictionary structure>.
DATA: CT TYPE LVC_T_STYL. "Table to refer to cells of a row
DATA: END OF GT_OUTTAB.
In the specific case of push buttons, the additional field has the type of a table with line type LVC_S_STYL, the socalled cell table. This structure has two fields style and fieldname to communicate the row or rows which
columns should be displayed as push buttons, follow these steps
1. To assign this property to all columns, just append one line to the cell table where the field style is set to the button
attribute
cl_gui_alv_grid=>mc_style_button.
2. To assign the property to designated columns, append one line in the cell table for each column. Set the field style
to the button attribute and determine the column using field fieldname.
Lastly, the ALV Grid Control has to be notified of the meaning of the new field in the output table. The field stylefname
of the layout structure is used for this purpose (in our example, you must assign CT to this field). As a consequence,
the ALV Grid Control marks this field in the field catalog as a technical field that is not displayed.
The diagram below may better explain. It shows an output table where cells b1 and c1 as well as the entire second
row are displayed as push buttons through the cell table (to keep the diagram simple, I have assumed that the
constant CL_GUI_ALV_GRID=>MC_STYLE_BUTTON has a value 1).
16
The ALV Grid Control supplies a large set of generic list functions. These standard baseline functions obviate the
need for developers to implement the same code over and over again. As a result, the ALV Grid Control is used
throughout the ECC system and list handling is presented in a uniform way to the user. Moreover, the ALV Grid
Control offers developers great opportunities to easily adapt this technology to support application-specific
requirements.
17