Alv Programming

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
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

Create Export to Select


Filter subtotals Word layout

Total a Export to Change Save


Sort column Excel layout
layout

4
Using the ALV
Changing the Report Layout
By pressing the ‘Change Layout’ button, the user has access to the following formatting
features

Columns
Columns that will
will be not be
displayed displayed
in this
order

The next screen


displayed allows you
to enter the filtering
criteria
The report will be
sorted by vendor
name (asc) then
year (desc) and
subtotaled by
vendor name

5
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

6
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 1. Press the
Save layout
a letter. button
• 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
2. Fill in name,
user specific default layout will desc, user
supercede a global default specific/global
and default or
layout. 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

8
Programming the ALV
Programming choices

ALV Function modules ALV Control


• These are very easy to use • Give you more flexibility (multiple controls per screen, drag &
drop)
• The function modules do most of the work for • You need some understanding of dialog programming and
you objects

Code that you may see using the function modules Code that you may see with controls
Data: g_custom_container type ref to
CALL FUNCTION cl_gui_custom_container,
'REUSE_ALV_FIELDCATALOG_MERGE' Grid type ref to cl_gui_alv_grid
EXPORTING
i_structure_name = … Create object g_custom_container
CHANGING Exporting
ct_fieldcat = … Container_name = ‘CCCONTAINER’.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' Create object grid


EXPORTING Exporting
it_fieldcat = … i_parent = g_custom_container.
TABLES
t_outtab = … 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 = t_fieldcat
TABLES
t_outtab = t_data
EXCEPTIONS
program_error = 1
OTHERS = 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 Column number in the llist


Fieldname The field name in your internal table (if specifying manually use all capital
letters)
Outputlen Column width
No_out Space – Field is shown when report initially displays
‘X’ – Do not show field initially. User can show field in ‘Change Layout’.
Tech Space – user is allowed to show and hide this field
‘X’ – the field is invisible to the user
Do_sum Create totals for this column

No_sum Do not allow totals/subtotals for this column. Used for numeric fields like
year, document number, etc that you would not want totaled.
No_zero Space – show zeros
‘X’ – suppress zeros
Seltext_l Long field label

Seltext_m Medium field label

Seltext_s 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 When set to ‘‘X’ will automatically set the column width to fit the data in that
column
Zebra When set to ‘X’ will create a stripped pattern among the rows
Confirmation_prompt 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.
Numc_sum 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 Sample code


Spos Sort order s_sort-fieldname = 'NAME1'.
s_sort-up = 'X'.
Fieldname Field to sort
s_sort-subtot = 'X'.
Up Values: space, ‘X’ append s_sort to t_sort.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
Down Values: space, ‘X’
EXPORTING
Subtot Values: space, ‘X’ it_fieldcat = t_fieldcat
Expa If ‘X’ will show only subtotals 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 Sample code
Data: g_save(1) type c.
to save layouts.
If sy-uname in poweruser_group.
I_SAVE values g_save = ‘A’.
Else.
Space Changes to layouts may g_save = ‘U’.
not be saved. Endif.

X Global layouts may be CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


save but not user- EXPORTING
specific. i_callback_program = g_repid
i_callback_pf_status_set =
U Only user-specific 'SET_PF_STATUS'
layouts maybe saved it_fieldcat = t_fieldcat
i_save = g_save
A Global and user-specific TABLES
layouts may be saved. t_outtab = t_data.

17
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 1. Press

STANDARD ALV
the copy
status
button

GUI status into your 2. Fill in this


screen and

program. press the copy


button

• This is done in the 3. On the


screen following

menu painter
this also press
the copy button

(SE41)

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 g_repid = sy-repid.

subroutine to our call function 'REUSE_ALV_GRID_DISPLAY'


program that invokes the exporting
new GUI status. i_callback_program =
i_callback_pf_status_set =
g_repid
'ALV_PF_STATUS'
•To have the ALV it_fieldcat
tables
= t_fieldcat

invoke this routine, set t_outtab = t_data


i_callback_program to exceptions
your program name and program_error
others
=
=
1
2.
i_callback_pf_status_set .
to the name of the new .
subroutine. .

•The subroutine name *****************************************************


form alv_pf_status using rt_extab type slis_t_extab.
can be any valid name *****************************************************
although make sure it is
all capitalized in the call <you may want to manipulate rt_extab here>
set pf-status 'STANDARD‘ excluding rt_extab.
function statement.
endform.

21
Programming the ALV
Responding To Drilldown and Custom
Functions

• To add drilldown functionality, g_repid = sy-repid.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


set i_callback_program to EXPORTING
i_callback_program = g_repid
your program name and i_callback_pf_status_set =
i_callback_user_command =
'ALV_PF_STATUS'
'ALV_UCOMM'
i_callback_user_command to it_fieldcat
TABLES
= t_fieldcat

the name of the routine to t_outtab


EXCEPTIONS
= t_data

program_error = 1
execute for drilldown and OTHERS = 2.

custom functions.
• The subroutine name can be
any valid name although
make sure it is all capitals in
the call function statement.

22
Programming the ALV
Responding To Drilldown and Custom
Functions (cont)
Sample routine
*****************************************************************
Frequently used fields from structure SLIS_selfield
form alv_ucomm using r_ucomm like sy-ucomm
rs_selfield type slis_selfield.
Tabindex The index of the detail line in the ALV that
*****************************************************************
the user double clicked. This should
case r_ucomm.
correspond to the index in the data table.
when '&IC1'. "--- pick, doubleclick Sumindex If this is > 0 then the user double-clicked a
*--- user clicked on a subtotal line. subtotal. The number would indicate the
if rs_selfield-sumindex ne 0 and rs_selfield-tabindex = 0. occurrence of the subtotal in the list.
exit.
endif. Endsum User double-clicked the total line

read table t_data index rs_selfield-tabindex.


Fieldname The name of the field that was clicked on.
Value The value in the field
case rs_selfield-fieldname.
when 'LIFNR' or 'NAME1'. Refresh Value ‘X’, Space.
set parameter id 'LIF' field rs_selfield-value. ‘X’ – causes the ALV grid to be refreshed
call transaction 'FK03'. from the data in the internal table.
when 'AMNT'.
set parameter id 'LIF' field t_data-lifnr. Col_stable When refreshing the ALV reposition the list
set parameter id 'BUK' field t_data-bukrs. to show the same columns (keep user
set parameter id 'GJR' field t_data-gjahr. scrolling)
call transaction 'FK10N' and skip first screen.
endcase.
Row_stable When refreshing the ALV, reposition the list
to show the same rows (keep user scrolling)
endcase.
endform. Exit Exit the ALV

23
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 Sample internal table of events
REUSE_ALV_EVENTS_GET to Event name Form name
return an internal table of all events
available in the ALV. USER_COMMAND
This list is in a structure that consists of TOP_OF_PAGE ALV_TOP_OF_PAGE
2 fields; an event name and a form name PF_STATUS_SET
Find the event in the table that you want TOP_OF_LIST
to invoke and update the form name with
the name of the subroutine in your END_OF_PAGE
program.
END_OF_LIST
*** You cannot use write statements to
produce headers and footers. You must
use function module
REUSE_ALV_COMMENTARY_WRITE. Name of routine in
Please see documentation for details. calling program

24
Programming the ALV
Using Events for Headers & Footers (cont)
Sample code to set up events table TOP_OF_PAGE event called by ALV
Data: s_events type slis_alv_event, ******************************************************
t_events type slis_t_event. form top_of_page.
******************************************************
call function 'REUSE_ALV_EVENTS_GET' refresh t_commentary.
exporting s_commentary-typ = 'H'.
i_list_type = 0 s_commentary-key = 'H Key'.
importing s_commentary-info = 'This is TOP_OF_PAGE'.
et_events = t_events. append s_commentary to t_commentary.
s_commentary-typ = 'S'.
*--- top of page
s_commentary-key = 'Vendor number'.
read table t_events into s_events
s_commentary-info = 'All'.
with key name = slis_ev_top_of_page.
append s_commentary to t_commentary.
if sy-subrc = 0.
s_commentary-typ = 'S'.
move ‘ALV_TOP_OF_PAGE' to s_events-form.
s_commentary-key = 'Vendor name'.
modify t_events from s_events index sy-tabix.
s_commentary-info = 'All'.
endif.
append s_commentary to t_commentary.

call function 'REUSE_ALV_GRID_DISPLAY' s_commentary-typ = 'A'.


exporting s_commentary-key = 'A Key'.
i_callback_program = g_repid s_commentary-info = 'This is the third type of
heading'.
i_callback_pf_status_set = 'ALV_PF_STATUS'
append s_commentary to t_commentary.
i_callback_user_command = 'ALV_UCOMM'
it_fieldcat = t_fieldcat call function 'REUSE_ALV_COMMENTARY_WRITE'
is_layout = s_layout exporting
it_events = t_events it_list_commentary = t_commentary
tables i_logo = 'ACCENTURE'.
t_outtab = t_data endform.
25
Programming the ALV
Using Events for Headers & Footers (cont)

List of common events

User_command It is easier to specify this event with the i_callback_user_command parameter


instead of in the it_events parameter.
Pf_status_set It is easier to specify this event with the i_callback_pf_status_set parameter
instead of in the it_events parameter.
Top_of_page Displays a header at the beginning of each page (seems to display online as
well as when printed)
End_of_list Display a footer at the end of the list (seems to display online as well as when
printed)
Top_of_list Display a header at the beginning of the list (seems to show only when
printed)
End_of_page 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