You are on page 1of 27

Using and Programming the ABAP List Viewer (ALV)

06/2004

Contents
- What is the ALV - Using the ALV - Programming the ALV
The Field Catalog The Is_Layout structure (grid characteristics) Starting the ALV with sorting and subtotals Controlling the users ability to create layouts Implementing a custom GUI Status Responding to drilldown and custom functions Creating headers and footers using events
2

What is the ALV


The ALV (Abap List Viewer) is a flexible tool for displaying lists. It provides common reporting options like sorting, subtotals and filters with no added programming. Allows the definition of multiple report layouts that can be maintained by the user. Downloads directly into Excel and Word.
3

Using the ALV


Commonly Used ALV Reporting Features

Filter Total a column

Create subtotals Export to Excel

Export to Word

Select layout Save layout

Sort

Change layout

Using the ALV


Changing the Report Layout
By pressing the Change Layout button, the user has access to the following formatting features

Columns will be displayed in this order

Columns that will not be displayed

The report will be sorted by vendor name (asc) then year (desc) and subtotaled by vendor name

The next screen displayed allows you to enter the filtering criteria

Using the ALV


A Formatted Report
The selected fields are listed in the order specified The report is sorted by Vendor name (asc) and Year (desc) There are subtotals by Vendor name The report is filtered to only show state 06

Using the ALV


Creating A New Layout
Your formatted report can be saved in a Layout. Layouts can be user specific or global. When user specific is checked, the layout name must start with a letter. When global (user specific is not checked), the layout name must start with a /. Checking the default setting will cause that layout be loaded when the list first displays. A user specific default layout will supercede a global default layout.

1. Press the Save layout button

2. Fill in name, desc, user specific/global and default or not.


7

Using the ALV


Changing Layouts
You can switch to different layouts quickly and easily to get totally different views of the data.
Choose layout button

Programming the ALV


Programming choices
ALV Function modules
These are very easy to use The function modules do most of the work for you

ALV Control
Give you more flexibility (multiple controls per screen, drag & drop) You need some understanding of dialog programming and objects

Code that you may see using the function modules CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING i_structure_name = CHANGING ct_fieldcat = CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING it_fieldcat = TABLES t_outtab =

Code that you may see with controls Data: g_custom_container type ref to cl_gui_custom_container, Grid type ref to cl_gui_alv_grid Create object g_custom_container Exporting Container_name = CCCONTAINER. Create object grid Exporting i_parent = g_custom_container. Call method grid->set_table_for_first_display exporting i_structure_name = changing it_outtab =
9

Programming the ALV


A simple ALV program
************************************************************************ START-OF-SELECTION. ************************************************************************ PERFORM load_data_table. PERFORM build_field_catalog. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING

it_fieldcat
TABLES t_outtab EXCEPTIONS program_error OTHERS

= t_fieldcat
= t_data = 1 = 2.

IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.

10

Programming the ALV


The Field Catalog
The field catalog defines the fields within your data table that you want displayed in the ALV. The field catalog is an internal table of TYPE slis_fieldcat_alv

The TYPEs used in the ALV function modules are defined in TYPE-POOL SLIS
Example code to define the field catalog:
TYPE-POOLS: slis.
DATA: s_fieldcat TYPE slis_fieldcat_alv, t_fieldcat TYPE slis_t_fieldcat_alv.

11

Programming the ALV


Building the Field Catalog
Method 1 Manually in your program
***************************************************** Form Build_field_catalog. ***************************************************** s_fieldcat-col_pos = 1. s_fieldcat-fieldname = 'LIFNR'. s_fieldcat-key = 'X'. s_fieldcat-do_sum = ' '. s_fieldcat-no_out = ' '. s_fieldcat-datatype = 'CHAR'. s_fieldcat-outputlen = 10. s_fieldcat-seltext_l = 'Vendor number'. s_fieldcat-seltext_m = 'Vendor num'. s_fieldcat-seltext_s = 'Vndr num'. APPEND s_fieldcat TO t_fieldcat. s_fieldcat-col_pos = 2. s_fieldcat-fieldname = NAME1'. . . . Endform.

12

Programming the ALV


Building the Field Catalog (cont)
Method 2 Using a structure in the Data Dictionary
***************************************************** Form Build_field_catalog. ***************************************************** CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING i_structure_name = 'ZPKALV1' CHANGING ct_fieldcat = t_fieldcat. *--- do not display street, city, zipcode or account *--- group when report initial displays LOOP AT t_fieldcat INTO s_fieldcat. IF s_fieldcat-fieldname = 'ORT01' OR s_fieldcat-fieldname = 'PSTLZ' OR s_fieldcat-fieldname = 'STRAS' OR s_fieldcat-fieldname = 'KTOKK'. s_fieldcat-no_out = 'X'. MODIFY t_fieldcat FROM s_fieldcat INDEX sy-tabix. ENDIF. ENDLOOP. Endform.

13

Programming the ALV


Frequently Used Catalog Fields
Col_pos Fieldname Outputlen No_out Tech Do_sum No_sum No_zero Seltext_l Seltext_m Seltext_s Column number in the llist The field name in your internal table (if specifying manually use all capital letters) Column width Space Field is shown when report initially displays X Do not show field initially. User can show field in Change Layout. Space user is allowed to show and hide this field X the field is invisible to the user Create totals for this column Do not allow totals/subtotals for this column. Used for numeric fields like year, document number, etc that you would not want totaled. Space show zeros X suppress zeros Long field label Medium field label Short field label
14

Programming the ALV


The IS_LAYOUT Structure
The IS_LAYOUT structure defines characteristics of the ALV grid itself. The structure is defined to your program as follows:
TYPE-POOLS: slis. Data: S_LAYOUT type SLIS_LAYOUT_ALV.

Some useful fields:


colwidth_optimize Zebra Confirmation_prompt Numc_sum When set to X will automatically set the column width to fit the data in that column When set to X will create a stripped pattern among the rows When set to X, if F03(Back), F12(Cancel) or F15(Exit) is pressed the user is asked whether they want to exit the report before it actually does it. When set to X will create a total for all numeric fields. You then specify which numeric fields should not be totaled with the fieldcat-no_sum flag.

Edit

Will allow the user to change the values within the ALV and those chages are reflected in the internal table in the calling program.

15

Programming the ALV


IT_SORT
IT_SORT is an internal table that is used to set the sorting and subtotaling of the list. The internal table is defined to your program as follows:
TYPE-POOLS: slis.
DATA: s_sort TYPE slis_sortinfo_alv, t_sort TYPE slis_t_sortinfo_alv.

Fields in IT_SORT
Spos Fieldname Up Down Subtot Expa Sort order Field to sort Values: space, X Values: space, X Values: space, X If X will show only subtotals

Sample code
s_sort-fieldname = 'NAME1'. s_sort-up = 'X'. s_sort-subtot = 'X'. append s_sort to t_sort. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING it_fieldcat = t_fieldcat IT_SORT = t_sort TABLES T_OUTTAB = t_data.
16

Programming the ALV


I_SAVE
This parameter is a 1 byte field that controls the users ability to save layouts.
I_SAVE values Space X Changes to layouts may not be saved. Global layouts may be save but not userspecific. Only user-specific layouts maybe saved Global and user-specific layouts may be saved.

Sample code
Data: g_save(1) type c.

If sy-uname in poweruser_group.
g_save = A. Else. g_save = U. Endif. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = g_repid 'SET_PF_STATUS' it_fieldcat i_save TABLES t_outtab = t_data.
17

U A

i_callback_pf_status_set = = t_fieldcat = g_save

Programming the ALV


The Default GUI Status
A GUI status defines the menus, toolbar buttons and function keys that a program will have available to it.
The complete GUI status in the REUSE_ALV_GRID_DISPLAY function module is shown below.

If you are not going to support all of these functions or need to add custom functions, you need to create and implement your own GUI status.

18

Programming the ALV


Implementing A Custom GUI Status
To implement a custom GUI status you should place a copy of the STANDARD ALV GUI status into your program. This is done in the menu painter (SE41)

1. Press the copy status button 2. Fill in this screen and press the copy button

3. On the screen following this also press the copy button

19

Programming the ALV


Implementing A Custom GUI Status (cont)
Once the STANDARD status has been copied, you must activate it in your program.

You may also want to add your own custom functions to it.

20

Programming the ALV


Implementing A Custom GUI Status (cont)
We now add a subroutine to our program that invokes the new GUI status.
g_repid = sy-repid. call function 'REUSE_ALV_GRID_DISPLAY' exporting i_callback_program = i_callback_pf_status_set = it_fieldcat = tables t_outtab = exceptions program_error = others = . . .

To have the ALV invoke this routine, set i_callback_program to your program name and i_callback_pf_status_set to the name of the new subroutine.
The subroutine name can be any valid name although make sure it is all capitalized in the call function statement.

g_repid 'ALV_PF_STATUS' t_fieldcat t_data 1 2.

***************************************************** form alv_pf_status using rt_extab type slis_t_extab. ***************************************************** <you may want to manipulate rt_extab here> set pf-status 'STANDARD excluding rt_extab. endform.

21

Programming the ALV


Responding To Drilldown and Custom Functions
To add drilldown functionality, set i_callback_program to your program name and i_callback_user_command to the name of the routine to execute for drilldown and custom functions. The subroutine name can be any valid name although make sure it is all capitals in the call function statement.
g_repid = sy-repid. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = i_callback_pf_status_set = i_callback_user_command = it_fieldcat = TABLES t_outtab = EXCEPTIONS program_error = OTHERS =

g_repid 'ALV_PF_STATUS' 'ALV_UCOMM' t_fieldcat t_data 1 2.

22

Programming the ALV


Responding To Drilldown and Custom Functions (cont)
Sample routine
***************************************************************** form alv_ucomm using r_ucomm like sy-ucomm rs_selfield type slis_selfield. ***************************************************************** case r_ucomm. when '&IC1'. "--- pick, doubleclick *--- user clicked on a subtotal line. if rs_selfield-sumindex ne 0 and rs_selfield-tabindex = 0. exit. endif. read table t_data index rs_selfield-tabindex. case rs_selfield-fieldname. when 'LIFNR' or 'NAME1'. set parameter id 'LIF' field call transaction 'FK03'. when 'AMNT'. set parameter id 'LIF' field set parameter id 'BUK' field set parameter id 'GJR' field call transaction 'FK10N' and endcase. endcase. endform.

Frequently used fields from structure SLIS_selfield


Tabindex The index of the detail line in the ALV that the user double clicked. This should correspond to the index in the data table.
If this is > 0 then the user double-clicked a subtotal. The number would indicate the occurrence of the subtotal in the list. User double-clicked the total line The name of the field that was clicked on. The value in the field Value X, Space. X causes the ALV grid to be refreshed from the data in the internal table. When refreshing the ALV reposition the list to show the same columns (keep user scrolling) When refreshing the ALV, reposition the list to show the same rows (keep user scrolling) Exit the ALV 23

Sumindex

Endsum Fieldname Value Refresh

rs_selfield-value.

t_data-lifnr. t_data-bukrs. t_data-gjahr. skip first screen.

Col_stable

Row_stable

Exit

Programming the ALV


Using Events for Headers & Footers
Your use events by associating a subroutine in your program with an ALV event.

Use function module REUSE_ALV_EVENTS_GET to return an internal table of all events available in the ALV.

Sample internal table of events


Event name Form name

USER_COMMAND
ALV_TOP_OF_PAGE PF_STATUS_SET

This list is in a structure that consists of TOP_OF_PAGE 2 fields; an event name and a form name

Find the event in the table that you want to invoke and update the form name with TOP_OF_LIST the name of the subroutine in your END_OF_PAGE program. *** You cannot use write statements to produce headers and footers. You must use function module REUSE_ALV_COMMENTARY_WRITE. Please see documentation for details.
END_OF_LIST

Name of routine in calling program


24

Programming the ALV


Using Events for Headers & Footers (cont)
Sample code to set up events table
Data: s_events type slis_alv_event, t_events type slis_t_event. call function 'REUSE_ALV_EVENTS_GET' exporting i_list_type = 0 importing et_events = t_events. *--- top of page read table t_events into s_events with key name = slis_ev_top_of_page. if sy-subrc = 0. move ALV_TOP_OF_PAGE' to s_events-form. modify t_events from s_events index sy-tabix. endif. call function 'REUSE_ALV_GRID_DISPLAY' exporting i_callback_program = g_repid i_callback_pf_status_set = 'ALV_PF_STATUS' i_callback_user_command = 'ALV_UCOMM' it_fieldcat = t_fieldcat is_layout = s_layout it_events = t_events tables t_outtab = t_data

TOP_OF_PAGE event called by ALV


****************************************************** form top_of_page. ****************************************************** refresh t_commentary. s_commentary-typ = 'H'. s_commentary-key = 'H Key'. s_commentary-info = 'This is TOP_OF_PAGE'. append s_commentary to t_commentary. s_commentary-typ = 'S'. s_commentary-key = 'Vendor number'. s_commentary-info = 'All'. append s_commentary to t_commentary. s_commentary-typ = 'S'. s_commentary-key = 'Vendor name'. s_commentary-info = 'All'. append s_commentary to t_commentary. s_commentary-typ = 'A'. s_commentary-key = 'A Key'. s_commentary-info = 'This is the third type of heading'. append s_commentary to t_commentary. call function 'REUSE_ALV_COMMENTARY_WRITE' exporting it_list_commentary = t_commentary i_logo = 'ACCENTURE'. endform.

25

Programming the ALV


Using Events for Headers & Footers (cont)
List of common events
User_command Pf_status_set Top_of_page End_of_list Top_of_list End_of_page It is easier to specify this event with the i_callback_user_command parameter instead of in the it_events parameter. It is easier to specify this event with the i_callback_pf_status_set parameter instead of in the it_events parameter. Displays a header at the beginning of each page (seems to display online as well as when printed) Display a footer at the end of the list (seems to display online as well as when printed) Display a header at the beginning of the list (seems to show only when printed) Displays a footer at the end of the list when printed (seems to show only when printed)

26

Programming the ALV


Questions

Contact information:

Paul.Kleiman@Accenture.com AIM: PaulKleiman9

27

You might also like