alv programming
TRANSCRIPT
Using and Programming the ABAP List Viewer (ALV)
06/2004
2
- 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
Contents
3
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.
4
Using the ALVCommonly Used ALV Reporting Features
Sort
Filter
Total a column
Create subtotals
Export to Excel
Export to Word
Change layout
Select layout
Save layout
5
Using the ALVChanging the Report Layout
By pressing the ‘Change Layout’ button, the user has access to the following formatting features
Columns that will not be displayed
Columns will be displayed in this order
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
6
Using the ALVA 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
7
Using the ALVCreating 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.
8
Using the ALVChanging Layouts
• You can switch to different layouts quickly and easily to get totally different views of the data.
Choose layout button
9
Programming the ALVProgramming choices
ALV Function modules
• These are very easy to use• The function modules do most of the work for
you
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 = …
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 with controlsData: 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_displayexporting i_structure_name = …
changing it_outtab = …
10
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.
11
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.
12
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.
13
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.
14
Programming the ALVFrequently 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
15
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.
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.
Some useful fields:
16
Programming the ALVIT_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.
Spos Sort order
Fieldname Field to sort
Up Values: space, ‘X’
Down Values: space, ‘X’
Subtot Values: space, ‘X’
Expa If ‘X’ will show only subtotals
Fields in IT_SORTs_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.
Sample code
17
This parameter is a 1 byte field that controls the users ability to save layouts.
Programming the ALV I_SAVE
Space Changes to layouts may not be saved.
X Global layouts may be save but not user-specific.
U Only user-specific layouts maybe saved
A Global and user-specific layouts may be saved.
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
i_callback_pf_status_set =
'SET_PF_STATUS'
it_fieldcat = t_fieldcat
i_save = g_save
TABLES
t_outtab = t_data.
I_SAVE values
Sample code
18
Programming the ALVThe 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.
19
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
20
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.
21
Programming the ALV Implementing A Custom GUI Status (cont)
g_repid = sy-repid.
call function 'REUSE_ALV_GRID_DISPLAY' exporting i_callback_program = g_repid i_callback_pf_status_set = 'ALV_PF_STATUS' it_fieldcat = t_fieldcat
tables t_outtab = t_data exceptions program_error = 1 others = 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.
•We now add a subroutine to our program that invokes the new GUI status.
•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.
22
Programming the ALV Responding To Drilldown and Custom Functions
g_repid = sy-repid.
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 TABLES t_outtab = t_data EXCEPTIONS program_error = 1 OTHERS = 2.
• 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.
23
Programming the ALV Responding To Drilldown and Custom Functions (cont)
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.
Sumindex If this is > 0 then the user double-clicked a subtotal. The number would indicate the occurrence of the subtotal in the list.
Endsum User double-clicked the total line
Fieldname The name of the field that was clicked on.
Value The value in the field
Refresh Value ‘X’, Space.
‘X’ – causes the ALV grid to be refreshed from the data in the internal table.
Col_stable When refreshing the ALV reposition the list to show the same columns (keep user scrolling)
Row_stable When refreshing the ALV, reposition the list to show the same rows (keep user scrolling)
Exit Exit the ALV
*****************************************************************
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 rs_selfield-value.
call transaction 'FK03'.
when 'AMNT'.
set parameter id 'LIF' field t_data-lifnr.
set parameter id 'BUK' field t_data-bukrs.
set parameter id 'GJR' field t_data-gjahr.
call transaction 'FK10N' and skip first screen.
endcase.
endcase.
endform.
Frequently used fields from structure SLIS_selfield
Sample routine
24
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.This list is in a structure that consists of 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 the name of the subroutine in your 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.
Event name Form name
USER_COMMAND
TOP_OF_PAGE ALV_TOP_OF_PAGE
PF_STATUS_SET
TOP_OF_LIST
END_OF_PAGE
END_OF_LIST
Sample internal table of events
Name of routine in calling program
25
Programming the ALV Using Events for Headers & Footers (cont)
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
Sample code to set up events table
******************************************************
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.
TOP_OF_PAGE event called by ALV
26
Programming the ALV Using Events for Headers & Footers (cont)
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)
List of common events