Está en la página 1de 19

ALV In ABAP

ALV for ABAP Freshers


INTRODUCTION SAP provides a set of ALV (ABAP LIST VIEWER) function modules which can be put into use to embellish the output of a report. This set of ALV functions is used to enhance the readability and functionality of any report output. Cases arise in sap when the output of a report contains columns extending more than 255 characters in length. In such cases, this set of ALV functions can help choose selected columns and arrange the different columns from a report output and also save different variants for report display. This is a very efficient tool for dynamically sorting and arranging the columns from a report output. The report output can contain up to 90 columns in the display with the wide array of display options. The following types of display are possible using ALV: 1. 2. 3. 4. List display Grid display Hierarchical display Block display

This document is aimed to introduce the reader to ALV Reports .It gives a brief overview about the various types of ALV displays and the different function modules used .It also provides screenshots of various types of ALV Display and sample code. PRE-REQUISITES: The reader should have a basic knowledge of ABAP Data dictionary ,Language constructs, working with tables ,simple report programming. ALV: What is ALV ALV is an acronym for ABAP List Viewer .It is a standard tool which enhances the readability of a report. Advantages of ALV: 1. Column alignment 2. Sorting 3. Subtotals 4. Downloading to excel or HTML format 5. Report analysis in graphical view 6. We can have our own layout set with our desired fields

ALV In ABAP
7. We can use mailing services 8. We can present the data in what ever the format we want like list, Grid, Blocked, Hierarchical, Tree and so on. 9. Gives a good look and feel. Types of ALV: 1. 2. 3. 4. List display Grid display Hierarchical display Block display

All the different types of ALV displays can be achieved using function modules. All the definitions of internal tables, structures and constants are declared in a type-pool called SLIS. 1. List Display. In list display the output of ALV is in the form of a simple list (single line or several lines).ALV list display can be achieved using the function module Reuse_alv_list_display. The function module outputs an internal table with any structure as a formatted one-line or multiple-line list. The various parameters in this function module: I_INTERFACE_CHECK I_BYPASSING_BUFFER I_BUFFER_ACTIVE I_CALLBACK_PROGRAM I_CALLBACK_PF_STATUS_SET I_CALLBACK_USER_COMMAND I_STRUCTURE_NAME IS_LAYOUT IT_FIELDCAT IT_EXCLUDING IT_SPECIAL_GROUPS IT_SORT IT_FILTER IS_SEL_HIDE I_DEFAULT I_SAVE IS_VARIANT IT_EVENTS IT_EVENT_EXIT IS_PRINT IS_REPREP_ID I_SCREEN_START_COLUMN I_SCREEN_START_LINE I_SCREEN_END_COLUMN I_SCREEN_END_LINE E_EXIT_CAUSED_BY_CALLER ES_EXIT_CAUSED_BY_USER T_OUTTAB

ALV In ABAP
Among the above, let us consider in detail some of the important and commonly used parameters 1. I_CALLBACK_PROGRAM This denotes the program from which the function module is called .The variable passed to this parameter should be of type SY-REPID.

2. IS_LAYOUT-List layout specifications i.e. it is used to modify the appearance of columns in the output. The internal table passed to this parameter should be of type slis_layout_alv.For example we have an internal table t_layout which is imported to the field IS_layout. Declare t_layout as type slis_layout_alv. The following details will be commonly filled in the internal table. a. t_layout-zebra =x. When the field zebra equals to X, it means that the alternate rows in the output will be colored dark and light. b. t_layout-colwidth_opt =X. This makes the output column width to increase or decrease as per the content width.e.g. if the column width is declared as 30 but in the output if the field is only 10 characters long, then the remaining space would be removed. c. window_titlebar-: If t_layout-window_titlebar = then the title of detail display popup window (which we get when double clicking a row) will be details: display. If t_layout-window_titlebar = <title as we like> then that title will be displayed in the popup window. 3. IT_FIELDCAT This is the most vital parameter which has to be imported to the alv display function module. A field catalog is required for every ALV list output. Field catalog contains descriptions of the list output fields (usually a subset of the internal output table fields).The field catalog for the output table is builtup in the caller's coding.Fieldcatalog can be generated either automatically or manually. 3a.Automatic generation of field catalog: This can be achieved using the function module REUSE_ALV_FIELDCATALOG_MERGE.

ALV In ABAP
The important parameters for this function module are: I_PROGRAM_NAME-Program from which the function module is called and which contains the exit routines. The program should always be a Report, Function group, Module pool or Form routine pool (not an Include). I_INTERNAL_TABNAME-Name of the output internal table which will be displayed. I_BYPASSING_BUFFER-When the program is run for the first time, the fields present in the output table and their positions are stored in a buffer .So buffer is taken as a reference for successive executions. This parameter should hold the value X to bypass the buffer. I_BUFFER_ACTIVE-For the same reason mentioned above, this parameter should hold the value (space) 3b.Manually populating the field catalog: Sometimes there is a need to fill in the field catalog manually under on of the following conditions 1. The internal table to be output does not have the same structure as a Data Dictionary structure which is referred to in the internal table declaration using LIKE or INCLUDE STRUCTURE. 2. All fields in this structure need not be output. Some of the commonly used details while populating a field catalog manually is: Positioning 1. Row_pos (row position) Value set: 0, 1 - 3 Only relevant if the list output is to be multi-line (two or three lines) by default. A multi-line list can also be defined by the user interactively if the default list is one-line. The parameter specifies the relative output line of the column in a multi-line list. 2. Col_pos (column position) Value set: 0, 1 - 60 Only relevant when the default relative column positions differ from the field catalog field sequence. The parameter specifies the relative column position of the field in the list output. The column order can be changed interactively by the user. If this parameter is initial for all field catalog entries, columns appear in the field catalog field sequence.

ALV In ABAP
Identification 1. Fieldname (field name) Value set: internal output table field name (required parameter).Name of the internal output table field which is described by this field catalog entry. NOTE: the fieldname must be given in UPPER CASE. Link to currency unit 1. Cfieldname (currency unit field name) Value set: SPACE, output table field name only relevant for amount columns with associated unit. Name of the internal output table field containing the currency unit associated with the amount field FIELDCAT-FIELDNAME. The field in FIELDCAT-CFIELDNAME must have its own field catalog entry. Link to measurement unit 1. Qfieldname (measurement unit field name) Value set: SPACE, output table field name Only relevant for quantity columns with unit link. Name of the internal output table field containing the measurement unit associated with the quantity field FIELDCAT-FIELDNAME. The field in FIELDCAT-QFIELDNAME must have its own field catalog entry. Column output options 1. Outputlen (column width) Value set: 0 (initial), n For fields with a Data Dictionary link this parameter can be left initial. For fields without a Data Dictionary link (program field) the parameter must be given the value of the desired field list output length (column width). Initial = column width is the output length of the referred Data Dictionary field (domain). n = column width is n characters. 2. Key (key column) Value set: SPACE, 'X' 'X' = kex field (key field output in color) Key fields can not be interactively hidden. 3. Emphasize (highlight columns in color) 'X' = column is colored with the default column highlight color. 'Cxyz' = column is colored with a coded color: C: Color (coding must begin with C) X: color number (can range from 0 to 7) 0-background color 1-Blue 2-Gray 3-Yellow 4-Blue/Gray 5-Green 6-Red

ALV In ABAP
7-Orange y: bold(0=off, 1=on) z: inverse(o=off, 1=on) Example if we give fieldcat-emphasize = C510 Then green color will be displayed with intensified on. Format column contents 1. Just (justification) Value set: SPACE, 'R', 'L', 'C' Only relevant for fields of data type CHAR or NUMC ' ' = default justification for this data type 'R' = right-justified output 'L' = left-justified output 'C' = centered output NOTE: The justification of the column header always follows the justification of the columns. Independent justification of the column header is not possible.

Texts The following text parameters should be specified for program fields without a Data Dictionary reference. The texts are taken from the Data Dictionary for fields with a Data Dictionary reference. If this is not desired, the text parameters can also be specified. The Data Dictionary texts are then ignored. If the user changes the column width interactively, the column header text with the appropriate length is always used. The interactive function 'Optimize column width' takes account of both the field contents and the column headers if all field contents are shorter than the shortest column header; the column width depends on the column header. The 'long field label' is also used in display variant definition, sort, etc. Popups. F1 HELP: Value set: SPACE, Data Dictionary data element name F1 help can be provided for a program field without a Data Dictionary reference, or F1 help which differs from the Data Dictionary help can be provided for a field with a Data Dictionary reference, using this parameter. seltext_l (long field label) seltext_m (medium field label) seltext_s (short field label) reptext_ddic (header)

ALV In ABAP
When F1 help is called for this field, the documentation of the specified data element is displayed. If the FIELDCAT-ROLLNAME is initial for fields with a Data Dictionary reference, the documentation of the data element of the referred Data Dictionary field is output. 4. IT_EVENTS-This internal table tells the ALV, which events are processed by the user CALLBACK. The table of possible events per list type can be initialized using the module REUSE_ALV_EVENTS_GET. The function module contains the following parameters: 1. I_LIST_TYPE (type SLIS_LIST_TYPE) (Export Parameter) Pass any one of the following values 0 = simple list REUSE_ALV_LIST_DISPLAY 1 = hierarchical-sequential list REUSE_ALV_HIERSEQ_LIST_DISPLAY 2 = simple block list REUSE_ALV_BLOCK_LIST_APPEND 3 = hierarchical-sequential block list REUSE_ALV_BLOCK_LIST_HS_APPEND 2. ET_EVENTS (type SLIS_T_EVENT) (Import Parameter) This internal table is returned by the FM with the list of all possible CALLBACK events for the particular ALV display. Pass this modified internal table to the display function module through the corresponding parameter (IT_EVENTS explained earlier). This function module returns an internal table having two fields NAME and FORM. The NAME field will have the list of all the events that can be handled in ALV. The FORM field will be blank when the FM returns the internal table. We have to fill the name of the form in which we are going to handle a particular event in the FORM field corresponding to the EVENT in the internal table returned by the FM. Some of the most commonly used events in ALV are : Top of page Top of list End of page End of list Set PF Status User Command 1) TOP OF PAGE: The event gets triggered whenever the first output statement like write, uline, skip etc. is encountered. The formatted simple header output at Top of Page can be output using the function module REUSE_ALV_COMMENTARY_WRITE. The function module has the following import parameters: 1. IT_ LIST_COMMENTARY-This field is of type SLIS_T_LISTHEADER.We need to declare an internal table of type SLIS_T_LISTHEADER and pass it to

ALV In ABAP
IT_LIST_COMMENTRY.The internal table contains three fields namely TYPE, KEY and INFO. TYP: Possible values are Header, Selection and Action. Header: It is the list header, denoted by H.Only the contents of INFO are output, KEY is ignored. Usually only one line. Selection: It displays important header information, denoted by S.It usually consist of several entries. Action: Describes the action info. Denoted by A. direct output via table. Only the contents of INFO are output, KEY is ignored. KEY: String with keyword character output in combination with TYP =S. INFO: Information output in header, formatted by type.

2) TOP OF LIST: This event gets triggered only once at the start of ALV display, but top of page will be triggered for every new page in ALV.Top of List comes out only when the ALV Report is printed. Top of Page gets triggered when the ALV Report is displayed. Top of List can never been seen in an ALV Report it comes only with the print out. 3) END-OF-PAGE: The END-OF-PAGE event is executed whenever processing reaches that area when formatting a list page or if the RESERVE statement detects that there is insufficient space remaining on the current page. You specify the size of the END-OF-PAGE area of list pages in the LINECOUNT parameter of the REPORT statement (e.g. REPORT TEST LINE-COUNT 65(3)). If you do not define a size, the END-OF-PAGE area contains no lines and the event END-OF-PAGE is never executed. If the standard setting LINE-COUNT 0 applies (i.e. no restriction on the number of lines per page), the event END-OF-PAGE is not processed, since no automatic new page follows. If you explicitly specify a new page with NEWPAGE, END-OF-PAGE is ignored. 4)END OF LIST : This event gets triggered only once at the end of ALV display, but end of page will be triggered at the end of every page in ALV.End of List comes out only when the ALV Report is printed. End of Page gets triggered when the ALV Report is displayed. End of List can never been seen in an ALV Report it comes only with the print out. 5) PF-STATUS: Sets a GUI (Graphical User Interface) status pfstat which can be up to 20 characters long. There are many of these statuses in the GUI of a program. Each one describes which functions are available and how you can select these via menus and menu bars or by pressing function keys or pushbuttons. The current status is stored in the system field SY-PFKEY.

ALV In ABAP
A status remains valid for the duration of a transaction or until you set a new status. 6) USER-COMMAND: It describes what should happen when the user performs certain action like clicking of a button etc.The user command would be stored in sy-ucomm. 2. GRID DISPLAY: Difference between ALV List and ALV Grid Display: 1. For ALV Grid display the function module REUSE_ALV_GRID_DISPLAY is used. The function module has all the parameters as discussed in the REUSE_ALV_LIST_DISPLAY. In addition the following parameters are present I_CALLBACK_TOP_OF_PAGE I_CALLBACK_HTML_TOP_OF_PAGE I_CALLBACK_HTML_END_OF_LIST I_BACKGROUND_ID I_GRID_TITLE I_GRID_SETTINGS IT_ALV_GRAPHICS IT_HYPERLINK IT_ADD_FIELDCAT IT_EXCEPT_QINFO I_HTML_HEIGHT_TOP I_HTML_HEIGHT_END 2. ALV List is done with function module; Grid can be done with function module and also using OO concepts. 3. ALV List is display only but Grid can be made editable for entry purpose. 4. ALV List can be displayed hierarchically but grid cannot be displayed hierarchically. 5. In ALV Grid we have resizable column option but in ALV list this option is not present. 6. In grid we can display the headings and top-of-page by using parameters in function module. In list we need to use events to display the headings and top- of-page. The parameter I_CALLBACK_TOP_OF_PAGE in the function module REUSE_ALV_GRID_DISPLAY can be used for top of page in ALV Grid. 7. In ALV list display it is not possible to insert logos in top of page but in grid display logos can be inserted by calling the function module REUSE_ALV_COMMENTRY_WRITE.

ALV In ABAP
I_LOGO We can upload logos in the ALV top of page using this parameter. STEPS TO INCLUDE LOGO: Logo should be uploaded into application server using transaction 'OAER'. 1. Go to Transaction OAER, 2. Give Class Name as PICTURES 3. Class type as OT 4. Object Key as the name of the Object u want to specify 5. Upon execution you would be prompted to give the file path details. Just upload which ever logo u want to display 6. Now you can use the same name in your ALV FM. 7. ALV List is faster and better in case of huge volumes of data. REFRESHING THE ALV OUTPUT: After the display of the ALV output, when the output needs to be refreshed then we can use selfield-refresh = 'X', where selfield is of type slis_selfield. What are the criteria to choose between the grid and list display. ->Grid cannot handle high volumes. -> Functions like sort, scrolling down consumes a lot of resources / time if the volume of data to be displayed is high. ->There is no clear cut definition that if so much data then go for list or vise versa but the developer has to take a call based on his experience. If not sure, then list is the better option. 3. Hierarchical Display: Hierarchical display are used to display data that are related. Like sales order and item details. Here sales order details can be the header data whereas them items in the sales order can be the item data. NOTE: Hierarchical display can only be used with List display. The function module used for Hierarchical display is REUSE_ALV_HIERSEQ_LIST_DISPLAY. This module outputs two internal tables as a formatted hierarchical-sequential list. The following parameters are present in the function module I_INTERFACE_CHECK I_CALLBACK_PROGRAM I_CALLBACK_PF_STATUS_SET I_CALLBACK_USER_COMMAND IS_LAYOUT IT_FIELDCAT

10

ALV In ABAP
IT_EXCLUDING IT_SPECIAL_GROUPS IT_SORT IT_FILTER IS_SEL_HIDE I_SCREEN_START_COLUMN I_SCREEN_START_LINE I_SCREEN_END_COLUMN I_SCREEN_END_LINE I_DEFAULT I_SAVE IS_VARIANT IT_EVENTS IT_EVENT_EXIT I_TABNAME_HEADER I_TABNAME_ITEM I_STRUCTURE_NAME_HEADER I_STRUCTURE_NAME_ITEM IS_KEYINFO IS_PRINT IS_REPREP_ID I_BYPASSING_BUFFER I_BUFFER_ACTIVE E_EXIT_CAUSED_BY_CALLER ES_EXIT_CAUSED_BY_USER T_OUTTAB_HEADER T_OUTTAB_ITEM Among these the parameters not discussed in list display are: 1. I_TABNAME_HEADER This a mandatory parameter to be passed into the function module. It is the name of the internal table in the program containing the output data of the highest hierarchy level. 2. I_TABNAME_ITEM-This is also a mandatory parameter. It is the name of the internal table in the program containing the output data of the lowest hierarchy level. 3. I_STRUCTURE_NAME_HEADER-It is the internal output table (header) structure name. 4. I_STRUCTURE_NAME_ITEM- It is the internal output table (item) structure name. 5. IS_KEYINFO It represents the Header/position. Table field links. It is a mandatory parameter to be passed into the function module. This structure contains the header and item table field names which link the two tables (shared key). Enter the foreign key field names in the fields KEYINFO-HEADERxx and KEYINFO-ITEMxx. The foreign key field names of the header and item tables are usually identical.

11

ALV In ABAP
The item table has other key fields as well as the header table foreign key. These other key fields should also be named in this structure. The corresponding header table field (HEADERxx) must be SPACE. Naming the other key fields guarantees a stable item table sort sequence. 4. BLOCK DISPLAY: Blocked ALV is used if we have more than one report in the output. (i.e.) if there are multiple internal tables with data to be displayed as single block then we go for block ALV.This looks like a simple report but this report has the function of sorting and filtering only. We need to use three function modules for Block display. 1. REUSE_ALV_BLOCK_LIST_INIT 2. REUSE_ALV_BLOCK_LIST_APPEND 3. REUSE_ALV_BLOCK_LIST_DISPLAY 1. REUSE_ALV_BLOCK_LIST_INIT: This function module is used to initialize the block ALV .It contains the following parameters: 1. I_CALLBACK_PROGRAM 2. I_CALLBACK_PF_STATUS_SET 3. I_CALLBACK_USER_COMMAND 4. IT_EXCLUDING 2. REUSE_ALV_BLOCK_LIST_APPEND: This function module is used to append each ALV output required. It contains the following parameters: 1. IS_LAYOUT 2. IT_FIELDCAT 3. I_TABNAME 4. IT_EVENTS 5. IT_SORT 6. I_TEXT If we want to display the contents of five internal tables in the output then we should call REUSE_ALV_BLOCK_LIST_APPEND five times. 3. REUSE_ALV_BLOCK_LIST_DISPLAY: This is used to display all the ALVs in the block and has the following function modules: 1. I_INTERFACE_CHECK 2. IS_PRINT 3. I_SCREEN_START_COLUMN 4. I_SCREEN_START_LINE 5. I_SCREEN_END_COLUMN 6. I_SCREEN_END_LINE ALV Variants: ALV provides the user with the flexibility of selecting the displaying only the desired column from the output. The variants are used to set default properties Of ALV output.

12

ALV In ABAP
The parameter i_save in the function module REUSE_ALV_LIST_DISPLAY and REUSE_ALV_GRID_DISPLAY specifies the mode in which the display variant can be saved.

The parameter i_save can have the values A,U, X and SPACE. If the value A is passed to this parameter then user can save the output variant user specifically or as standard variant. If the value passed is U then the layout should only be user specific. If X is passed standard save only is possible. If the value passed is SPACE then no save is done. The common function modules related to ALV Variants are 1.Reuse_alv_variant_default_get:- Used to fetch the default ALV variant. 2. Reuse_alv_variant_f4:- This displays the variant selection dialog box. 3. Reuse_alv_variant_existence:- Used to check if the selected variant is valid or not.

On clicking the layout icon in the output, the list of all fields in the display is shown in the new popup screen. Then we can hide the desired fields by selecting that field and clicking on the hide selected fields button as shown in the screenshot above.

13

ALV In ABAP

Then goto Settings->layout->save to save the layout.

Sample ALV Outputs: ALV List Display:

ALV Grid Display:

14

ALV In ABAP

ALV Hierarchial Display:

ALV Block Display

15

ALV In ABAP

SAMPLE CODE :

Sam source code ple

Introduction to Object Oriented ALV Instead of using standard function modules SAP also provides Classes for using ALV. By using Object Oriented ALV, we can have two grid displays in one screen whereas by using the classical ALV function modules two blocks of ALV output can be obtained only as two lists. In the object oriented ALV we have wrapper class to implement the object oriented functionality. The first step in object oriented ALV is to create a container. There are five kinds of ALV Containers. a. SAP Custom Container The SAP Custom Container allows you to display controls in an area defined on a normal screen using the Screen Painter. Class: CL_GUI_CUSTOM_CONTAINER b. SAP Dialog Box Container The SAP Dialog Box container allows you to display controls in a modal dialog box or full screen. Class: CL_GUI_DIALOGBOX_CONTAINER c. SAP Docking Container The SAP Docking Container allows you to attach a control to any of the four edges of a screen as a resizable screen area. You can also detach it so that it becomes an independent modal dialog box. Class: CL_GUI_DOCKING_CONTAINER

16

ALV In ABAP
d. SAP Splitter Container The SAP Splitter Container allows you to display more than one control in a given area by dividing it into cells. Class: CL_GUI_SPLITTER_CONTAINER e. SAP Easy Splitter Container The SAP Easy Splitter Container allows you to divide an area into two cells with a control in each. The cells are separated by a moveable splitter bar. To create a custom container, we need a custom container area on the screen. Create a custom control in the screen layout.

* This will create a container CREATE OBJECT ob_custom EXPORTING container name = CONTAINER. STEP 2: SPLIT The Container. If we want more than one ALV displays we need to split the container. * This spit the container OB_CUSTOM into two CREATE OBJECT ob_split1 EXPORTING

17

ALV In ABAP
parent = ob_custom orientation = cl_gui_easy_splitter_container=>orientation_vertical SASH_POSITION = 70. This will split the container vertically. The sash_position says that the container will be split in the ratio specified. STEP 3: * Now Put a grid in each container CREATE OBJECT ob_grid1 EXPORTING i_parent = ob_split1->top_left_container. CREATE OBJECT ob_grid2 EXPORTING i_parent = ob_split1->bottom_right_container. STEP 4: Make the list to be displayed, by calling the method set_table_for_first_display. CALL METHOD ob_grid1->set_table_for_first_display EXPORTING is layout = t_layout CHANGING it_outtab = T_MISC_OUTPUT1 IT_FIELDCATALOG = t_fieldcat. CALL METHOD ob_grid2->set_table_for_first_display EXPORTING is layout = t_layout1 CHANGING it_outtab = T_EDID4_N IT_FIELDCATALOG = t_fieldcat1. The internal table for field catalog is of type LVC_T_FCAT. The commonly used fields of this type are: FIELDNAME: You use this field to assign a field name of your output table to a row of the field catalog. All settings that you make in this row refer to the corresponding column of the output table. REPTEXT: Relevant only to fields with reference to the Data Dictionary. For such fields, the ALV Grid Control copies the field label for the header of the corresponding data element into this field. OUTPUTLEN: Determines the column width of the field:

18

ALV In ABAP
If the field has a reference to the Data Dictionary, you can leave the field set to its initial value. In this case, the ALV adopts the output length of the relevant domain. For fields without reference to the DDIC, you must specify the desired field output length. The field catalog can be filled manually as discussed in the ALV using function modules. It can be populated semi automatically using LVC_FIELDCATALOG_MERGE. The internal table used for layouts should be of type LVC_S_LAYO. The most commonly used fields in this type are: ZEBRA: If this field is set, the list shows a striped pattern in the print preview and when it is printed. CWIDTH_OPT: If this field is set, the ALV Grid Control optimizes the column width. You can then see the column header and the contents of the cells of this column. GRID_TITLE: Title between grid control and toolbar. Sample output of two grid displays in one screen using Object Oriented ALV:

Reference: www.sdn.sap.com

19

También podría gustarte