188 KiB
Selection Screens and Classic Lists
- Selection Screens and Classic Lists
Introduction
Note
The content of this cheat sheet and the executable examples are only relevant to classic ABAP.
Selection screens and classic lists are among the classic ABAP user interfaces. They are integrated into the ABAP language itself, which means that special ABAP statements are available to create and handle them.
This cheat sheet provides a high-level overview of selection screens and classic lists, focusing on a selection of related statements, supported by executable examples to explore the syntax in action. It includes an excursion into the SAP List Viewer (ALV).
For more detailed information and syntax options, see the topics Selection Screens and Classic Lists in the ABAP Keyword Documentation.
Note
- Although they are considered outdated for application programs, you will still find classic ABAP UIs frequently in classic ABAP.
- Classic ABAP UIs cannot be created in ABAP Cloud.
- This cheat sheet ...
- is not intended to encourage you to start creating classic ABAP UIs for programming new applications.
- does not cover all facets, techniques, and syntax options (there's a rich variety of keywords and additions) in great detail.
- is intended to cover a selection of related syntax that you may encounter in older ABAP code. If you need more information, always consult the ABAP Keyword Documentation.
- Links to the ABAP Keyword Documentation in this cheat sheet refer to the documentation for Standard ABAP (latest version).
Selection Screens
- Are special dynpros in executable programs ("reports"; they're also possible in function groups and module pools, but the focus here is on executable programs).
- Used for data entry in an executable program, i.e. they allow users to ...
- enter parameters (for entering single values).
- provide selection criteria (complex selection options for value lists and value ranges) that can be used to supply an executable program with values to work with.
- Can be created using special ABAP statements in the global declaration part of executable programs
- Are processed by the ABAP runtime framework, which triggers selection screen events (see further down).
- When you create a selection screen and run an ABAP program, the dynpros are generated automatically. These generated dynpros cannot be edited directly.
- Further ways in which selections screens are different from regular dynpros:
- Unlike dynpros, you can create selection screens without using the screen painter tool. The ABAP Editor is the only tool you use to define a selection screen (using ABAP statements).
- They have a more limited set of features, for example, the layout of selection screens is more restricted. However, selection screens offer various types of input fields, which can also be expressed as checkboxes or radio buttons, and which can be grouped and labeled.
- Modification options are limited. For example, you can only use some predefined function codes.
- You cannot define dialog modules in the ABAP program.
- There are two types of selection screens:
- Standalone selection screens
- Defined between the statements
SELECTION-SCREEN BEGIN OF SCREENandSELECTION-SCREEN END OF SCREEN - Can be defined in all programs that can contain dynpros
- Defined between the statements
- Standard selection screens
- Each executable program (and only there) contains a standard selection screen with the dynpro number 1000. Note: If you create a standalone selection screen, you cannot use dynpro number 1000.
- The screen elements on the standard selection screen are defined by all
PARAMETERS,SELECT-OPTIONS, andSELECTION-SCREENstatements that are defined outside of the statements mentioned above for creating standalone selection screens.
- Standalone selection screens
Classic Lists
- Used to output data in a structured and formatted way.
- This output can be ...
- created in the so-called list buffer, a memory area for storing screen lists, i.e. a list that is displayed on a predefined list dynpro (list dynpro that is accessed implicitly when a program is executed; note: this dynpro is not part of the executed program).
- sent to the SAP spool system as a spool list (i.e. a list that is not stored as a screen list but is intended for printing or archiving; the focus of this cheat sheet is on screen lists).
- Typical flow of executable programs that include selection screens and content to be displayed on the list dynpro:
- Programs are executed using the Execute button (F8).
- Selection screens are displayed, users usually make entries for whatever purpose, which are further processed in the program.
- Output result is written to the list buffer and finally displayed (on a list dynpro; note: the list processing is automatically started when running executable programs).
- The list can be further implemented to respond to user interaction, such as clicking a line in the list.
- More modern alternatives for classic lists are available, such as the classes of the SAP List Viewer (ALV), for example
CL_SALV_TABLE.
Note
The program is grouped into so-called event blocks, which can contain implementations for various events (e.g. a selection screen event when a user selects a radio button, or a list event when a user double-clicks a line in the list). These event blocks are introduced by special ABAP statements. When a particular event is triggered, the corresponding event block is called and the appropriate code can be implemented there to react to the user action. See more information here.
ABAP Statements for Selection Screens
Selection screens can be created by using special ABAP statements in the global declaration part of executable programs:
PARAMETERS
- Creates ...
- a program-global variable (similar to
DATAstatements) - an input field on the selection screen automatically, i.e. the content of the program-global variable can be changed via the input field.
- a program-global variable (similar to
- The parameter name can be up to eight characters long.
- Multiple additions are available to define the appearance (e.g.
AS CHECKBOX) or influence the user interface (e.g.OBLIGATORY).
The following table includes code snippets with a selection of available syntax options:
Note
- Various combinations of multiple additions are possible with
PARAMETERSstatement, and some are not. See the ABAP Keyword Documentation for details.- To try out the code examples, you can create a demo executable program, copy and paste the code snippets, and run the programs using F8. They are designed to output content using classic lists.
- The code snippets anticipate topics outlined further down, for example, event blocks and
WRITEstatements.
| Subject/Additions | Notes | Code Snippet |
|---|---|---|
|
Type specification options |
|
|
|
Parameters using ABAP Dicitionary input helps |
|
|
|
Dynamically specifying the type |
|
|
|
Specifying value options |
|
|
|
Specifying screen options |
|
|
|
Assigning function codes to selection parameters |
|
|
|
Assigning a screen element of a selection screen to a modification group |
|
|
|
Replacing the technical name displayed on the screen |
|
|
SELECT-OPTIONS
- Declares selection criteria for a data object
- Unlike the
PARAMETERSstatement, which specifies a single value for a variable, theSELECT-OPTIONSstatement allows you to specify complex criteria, such as an value range or a list of single values, to include or exclude values, that can be evaluated. - The selection criteria are assigned to a selection table:
- Such a table contains four columns -
LOW,HIGH,OPTION,SIGN- for determining range conditions. Each line of such a table represents a condition. - Typically, the content of the selection table can be evaluated in
SELECTstatements using theINoperator in theWHEREclause. - If the selection table is empty, all lines are respected.
- Selection tables have the same layout as ranges table that can be created using the syntax
TYPE RANGE OF. - For historical reasons, the selection table is a table with header line. Therefore, if you want to address the table content (beyond the use in a
SELECT ... WHERE ... IN ...statement), use the syntaxa[].
- Such a table contains four columns -
- Multiple additions are available, and combinations of them are possible.
The following table includes code snippets with a selection of available syntax options:
Note
- Various combinations of multiple additions are possible with
SELECT-OPTIONSstatements.- To try out the code examples, you can create a demo executable program, copy and paste the code snippets, and run the programs using F8. They are designed to output content using classic lists.
- The code snippets anticipate topics outlined further down, for example, event blocks and
WRITEstatements.
| Subject/Addition | Notes | Code Snippet |
|---|---|---|
|
Determining the |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
|
Specifying screen options |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
|
Specifying value options |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
SELECTION-SCREEN
SELECTION-SCREENstatements can be used to create and modify the layout of standalone selection screens.- Note: The standard selection screen for executable programs is created automatically. That is, each executable program contains a standard selection screen with dynpro number 1000. The screen elements there are defined by all
PARAMETERS,SELECT-OPTIONS, andSELECTION-SCREENstatements that are not specified within theSELECTION-SCREEN BEGIN OF SCREEN ... SELECTION-SCREEN END OF SCREEN ...block. - Selection screens can be created as regular dynpros or as subscreen dynpros.
- Multiple syntax variants with
SELECTION-SCREENare available. Most of them do not create selection screens, but are used to change the layout of selection screens, create additional screen elements, and so on. - Example: The
PARAMETERSandSELECT-OPTIONSstatements create input fields on an individual line. Using the variants of theSELECTION-SCREENstatement, you can arrange the layout differently. - However, the selection screen cannot be generated if there are conflicts with existing screen elements.
- See the effect with the following snippet. Run a program with the code snippet and comment in the third line. The program terminates. The text to be created (in the same line as the first text) interferes with the first text. However, the fourth line would be possible because there is no overlap.
PARAMETERS pa TYPE string. SELECTION-SCREEN COMMENT /5(10) t1. "SELECTION-SCREEN COMMENT 10(20) t2. "SELECTION-SCREEN COMMENT 20(20) t3.
Creating Selection Screens
| Subject/Statement | Notes | Code Snippet |
|---|---|---|
|
Creating a selection screen as regular dynpro |
|
|
|
Creating a selection screen as subscreen dynpro |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
Adapting the Selection Screen Layout
| Subject/Statement | Notes | Code Snippet |
|---|---|---|
|
Adding blank lines |
|
|
|
Creating horizontal lines |
|
|
|
Creating an output field/comment on the selection screen |
|
|
|
Specifying multiple elements in one line and their position in the line |
|
|
|
Creating pushbuttons |
|
|
|
Defining a block |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
|
Defining tabstrips |
|
See the example for |
|
Adding pushbuttons in the application toolbar |
|
The code snippet, copyable to a demo program and executable using F8, implements the following:
|
Using Elements from Other Selection Screens
- You can include screen elements created in other selection screens.
- Statements include the
INCLUDEaddition. These are possible:SELECTION-SCREEN INCLUDE PARAMETERSSELECTION-SCREEN INCLUDE SELECT-OPTIONSSELECTION-SCREEN INCLUDE COMMENTSELECTION-SCREEN INCLUDE PUSHBUTTONSELECTION-SCREEN INCLUDE BLOCKS
- Find more information in the subtopics here.
The following code snippet, copyable to a demo program and executable using F8, implements the following:
- Various screen elements are created. Many of them - all possible syntax options are reflected - are reused and included in another selection screen (9000).
- The selection screen with number 9000 is called if a checkbox is selected. This selection screen then opens in a modal dialog box.
PROGRAM.
TABLES sscrfields.
SELECTION-SCREEN COMMENT /1(30) txt1.
SELECTION-SCREEN SKIP.
PARAMETERS param1 TYPE c LENGTH 10.
SELECTION-SCREEN SKIP.
DATA num TYPE c LENGTH 10.
SELECT-OPTIONS sel FOR num.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT /1(30) txt2.
SELECTION-SCREEN PUSHBUTTON /1(15) btn USER-COMMAND cmd.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN COMMENT /1(70) txt3.
PARAMETERS toincl AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF BLOCK blck,
COMMENT /1(30) txt4,
ULINE.
PARAMETERS param2 TYPE c LENGTH 10.
SELECTION-SCREEN END OF BLOCK blck.
SELECTION-SCREEN: BEGIN OF SCREEN 9000 AS WINDOW,
INCLUDE PARAMETERS param1,
INCLUDE SELECT-OPTIONS sel,
INCLUDE COMMENT /1(10) txt2,
INCLUDE PUSHBUTTON /1(15) btn,
INCLUDE BLOCKS blck,
END OF SCREEN 9000.
INITIALIZATION.
txt1 = 'Standard Selection Screen'.
txt2 = 'Pushbutton:'.
txt3 = 'Select the checkbox to demonstrate SELECTION-SCREEN ... INCLUDE'.
txt4 = 'Some text'.
btn = 'Button'.
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'CMD'.
MESSAGE |Hello { sy-uname }. You clicked the pushbutton| TYPE 'I'.
ENDCASE.
START-OF-SELECTION.
IF toincl = 'X'.
CALL SELECTION-SCREEN 9000 STARTING AT 10 10.
WRITE / `The checkbox was selected, and the modal dialog box opened.`.
WRITE / `This modal dialog box contains multiple included elements.`.
ELSE.
WRITE / `The checkbox was not selected, so the modal dialog box was not opened.`.
WRITE / `This modal dialog box contains multiple included elements.`.
ENDIF.
SKIP.
WRITE / |param1: "{ param1 }"|.
WRITE / |param2: "{ param2 }"|.
WRITE / `Selection table content:`.
LOOP AT sel ASSIGNING FIELD-SYMBOL(<st>).
WRITE / |low: { <st>-low }, high: { <st>-high }, option: { <st>-option }, sign: { <st>-sign }|.
ENDLOOP.
Calling Selection Screens
- There are several ways of how to call selection screens (see here), among them
CALL SELECTION-SCREENstatements. - These statements call selection screens by specifying the dynpro number.
- Any selection screen of the main program (and only from there; including 1000) can be called.
CALL SELECTION-SCREEN 9876.
"STARTING AT/ENDING AT additions for creating a modal dialog box
"The numbers determine how it should be displayed.
"STARTING AT: upper left corner (column and line); ENDING AT: bottom right
"corner (if not specified, it is set automatically)
CALL SELECTION-SCREEN 9345 STARTING AT 10 10.
CALL SELECTION-SCREEN 9345 STARTING AT a b ENDING AT c d.
"You can also predefine selection criteria for a selection screen using
"the addition USING SELECTION-SET.
Excursion: SUBMIT Statements
SUBMITstatements call executable programs and, therefore, selection screens.- In general, every executable program is started implicitly with
SUBMIT. - Selection screens can be considered a parameter interface if the program containing them is executed using
SUBMIT. - Note that when using
SUBMIT, an authorization check for the authorization group is performed using theS_PROGRAMauthorization object.
The following table includes a selection of additions and code snippets. Two programs use the demo names zdemo_abap_report for the calling program and zdemo_abap_report_submit for the called program.
| Subject/Addition | Notes | Code Snippet |
|---|---|---|
|
Calling a program, ending the current program |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
|
Calling a program, interrupting the current program and return |
|
See above. |
|
Dynamically calling a program |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
|
Specifying a particular selection screen to be called and display it or not |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
|
Passing values to a called selection screen |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
|
Handling the list of the called program |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
|
Running a program in the background |
|
The following code snippets, copyable to demo programs and executable using F8 (execute program
|
ABAP Statements for Classic Lists
Creating Lists with WRITE Statements
- Lists consist of consecutive list lines that are filled one after the other using the statement
WRITE. - This way, you can output mostly flat data objects and strings/xstrings, types converted to a character-like value (i.e. no internal table content or structures directly).
- Each time a data object is output, an output length is defined, either implicitly or explicitly (see here).
- The following snippets show a selection of additions to
WRITEstatements, covering positioning, creating special list elements, and formatting options.
| Subject/Addition | Notes | Code Snippet |
|---|---|---|
|
Writing content |
|
|
|
Positioning options (1) |
|
|
|
Positioning options (2) |
|
|
|
Adding quickinfo |
Creates a tooltip for the output |
|
|
Adding list elements such as checkboxes, icons, symbols, and frames |
|
|
|
Formatting options |
The example only shows a selection. Check the documentation for all additions. |
Formatting Output Using FORMAT Statements
FORMATstatements can be used to apply formattings to output statements.- The formats are valid until reverted by a new
FORMATstatement, using the additionsOFForRESET. - Note that the additions for
FORMATare also available forWRITEstatements and can be applied individually. See the ABAP Keyword Documentation for all details.
| Subject/Addition | Notes | Code Snippet |
|---|---|---|
|
Coloring output |
As shown above, multiple colors are available. Instead of |
|
|
Specifying a hotspot |
|
|
|
Specifying an input field |
Enables user input that can be evaluated during a list event. |
|
|
Creating frames |
Defines whether the - and | characters are converted to line elements, producing continuous lines. |
|
|
Resetting formats |
This addition sets all formatting settings for which the corresponding addition is not specified in the same |
In the following example, all settings are reset without specifying concrete additions (e.g. |
List Creation-Related ABAP Keywords and Additions
| Subject/Keyword | Notes | Code Snippet |
|---|---|---|
|
Creating a horizontal line |
Additions are available such as for position and length. |
|
|
Specifying if blank lines created using |
|
|
|
Setting the list cursor explicitly in another line |
You can create blank lines, but also set to the cursor to a specified line. |
|
|
Setting the list cursor to the first position of the next line |
Additions are available to affect the scrolling behavior |
|
|
Storing the content of a flat variable together with the current list line |
|
|
|
Creating a page break |
|
|
|
Positioning the list cursor in the first position of the first line of a logical unit |
|
The following example demonstrates |
Processing Lists in List Buffers
- ABAP statements are available that allow lists to be processed that have already been saved as screen lists in the list buffer.
- The following selection covers
READ [CURRENT] LINEandMODIFY [CURRENT] LINE.
| Subject/Keyword | Notes | Code Snippet |
|---|---|---|
|
Reading content of lines |
|
|
|
Overwriting line content and format |
|
|
Note
- Relevant sy components in the context of lists
- You can interact with a list by double-clicking a line (or pressing F2; the default
PICKfunction code raises theAT LINE-SELECTIONevent). Other function codes usually trigger theAT USER-COMMANDevent. See the event blocks below. In your ABAP program, you can react to the user action by implementing the individual event blocks. Regarding the function code, you can use thesy-ucommsystem field for evaluation (unlike dynpros, there's noOK_CODEfield to be filled).
Event Blocks
- Event blocks are introduced by an event keyword.
- Ended by the next processing block (there's no closing statement).
- Should not contain separate declarations, i.e. you should put all data object declarations in the global declaration part (except for
AT SELECTION-SCREEN ...) - It is recommended that you use ABAP Objects and methods consistently for data encapsulation
- Should not be specified more than once in a program
- Called depending on the user action and the program state (even if not explicitly specified)
Note
The main purpose of the examples is to visualize the calling of the events. Internal tables are filled during the events to log the event name and the time stamp when it is called. The tables are then output.
| Event block | Notes | Code Snippet |
|---|---|---|
|
Program constructor |
|
See below |
|
Reporting events related to executable programs in particular |
|
|
|
Selection screen events |
Event blocks:
|
|
|
List events |
|
The following example is an excerpt of the executable example contained in the repository. Find more information in the comments there. The example is just intended to explore and visualize various list events. |
Excursion: SAP List Viewer (ALV)
- Provides an object-oriented API for displaying and formatting lists
- Allows you to specify layout settings and functionality
- Classes such as
CL_SALV_TABLEencapsulate the use of the ALV grid control and simplify the integration into ABAP programs. Note: The older classCL_GUI_ALV_GRIDshould no longer be used directly for new developments. - Other classes are available to handle hierarchical lists and tree structures. The focus of the code snippets is on simple, non-nested tables and the use of the
CL_SALV_TABLEclass.
Getting Started
- To get started quickly, you only need to ...
- instantiate an ALV table object using the
factorymethod. - define the display type for the ALV output. By default, full-screen display is enabled.
- display the ALV output using the
displaymethod.
- instantiate an ALV table object using the
Note
- When working with ALV, make sure that you implement appropriate error handling.
- The
factorymethod also has optional exporting parameters. You can use the optionallist_displayparameter to specify whether you want to display the ALV output as classic list. It is set to false by default. There are also exporting parameters to display the ALV output in containers (e.g. see the dynpro cheat sheet example). The exporting parameters are not relevant in this example. Here, the ALV output is displayed on the entire screen (however, it is also possible to display the ALV output in a dialog box).
The following code snippet shows a simple example to get you started quickly with the necessary method calls. It includes a demo internal table with content.
TYPES: BEGIN OF demo_struct,
col1 TYPE string,
col2 TYPE i,
col3 TYPE c LENGTH 1,
END OF demo_struct.
DATA itab TYPE TABLE OF demo_struct WITH EMPTY KEY.
itab = VALUE #( ( col1 = `abc` col2 = 1 col3 = 'X' )
( col1 = `def` col2 = 2 col3 = 'Y' )
( col1 = `ghi` col2 = 3 col3 = 'Z' ) ).
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(alv)
CHANGING t_table = itab ).
alv->display( ).
CATCH cx_salv_msg INTO DATA(err).
MESSAGE err->get_text( ) TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
Optional Layout Settings and Functionality
- The following commented code snippet covers a selection of the many setting and functionality options.
- For simplicity of the snippet, the code only uses the root exception class. Make sure that you implement appropriate error handling and exception classes.
- You can copy and paste the code into your own test program to explore the ALV output and the effect of the method calls.
Note
Check the comments for the custom functions. If there are errors in your test program, replace the relevant code section and enable the generic ALV functions.
TYPES: BEGIN OF demo_struct,
col1 TYPE string,
col2 TYPE i,
col3 TYPE string,
col4 TYPE icon_d,
col5 TYPE string,
END OF demo_struct.
DATA itab TYPE TABLE OF demo_struct WITH EMPTY KEY.
DATA alv TYPE REF TO cl_salv_table.
DATA cnt4evt TYPE i VALUE 6.
"Local class to handle events
CLASS lcl_events DEFINITION.
PUBLIC SECTION.
CLASS-METHODS single_click
FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
CLASS-METHODS double_click
FOR EVENT double_click OF cl_salv_events_table
IMPORTING row column.
CLASS-METHODS func_click
FOR EVENT added_function OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS.
CLASS lcl_events IMPLEMENTATION.
METHOD single_click.
"Both single and double click events trigger messages.
READ TABLE itab INDEX row REFERENCE INTO DATA(sc_ref).
IF sy-subrc = 0.
ASSIGN sc_ref->(column) TO FIELD-SYMBOL(<fs_sc>).
MESSAGE `Single click event. ` &&
|Row: { row } { COND #( WHEN column IS NOT INITIAL THEN `Column: ` && column ) } | &&
|{ COND #( WHEN <fs_sc> IS ASSIGNED THEN `Value: ` && <fs_sc> ) }| TYPE 'I'.
ELSE.
MESSAGE `Single click event` TYPE 'I'.
ENDIF.
ENDMETHOD.
METHOD double_click.
READ TABLE itab INDEX row REFERENCE INTO DATA(dc_ref).
IF sy-subrc = 0.
ASSIGN dc_ref->(column) TO FIELD-SYMBOL(<fs_dc>).
MESSAGE `Double click event. ` &&
|Row: { row } { COND #( WHEN column IS NOT INITIAL THEN `Column: ` && column ) } | &&
|{ COND #( WHEN <fs_dc> IS ASSIGNED THEN `Value: ` && <fs_dc> ) }| TYPE 'I'.
ELSE.
MESSAGE `Double click event` TYPE 'I'.
ENDIF.
ENDMETHOD.
METHOD func_click.
"Handling custom functions
CASE e_salv_function.
WHEN 'DATA'.
MESSAGE `Custom function DATA. Do something ...` TYPE 'I'.
WHEN 'TEST'.
IF cnt4evt = 6.
MESSAGE `A table row will be added, demonstrating the 'refresh' ` &&
`method that rebuilds the output table.` TYPE 'I'.
ENDIF.
cnt4evt += 1.
itab = VALUE #( BASE itab ( col1 = `fffff` col2 = cnt4evt
col3 = `ttttt` col4 = icon_green_light ) ).
"Rebuilding the output table
alv->refresh( ).
WHEN 'QUIT'.
MESSAGE `Custom function QUIT. Do something ...` TYPE 'I'.
WHEN OTHERS.
MESSAGE `Some other function` TYPE 'I'.
ENDCASE.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"Populating internal table that is output
itab = VALUE #( ( col1 = `aaaaa` col2 = 1 col3 = `zzzzz` col4 = icon_green_light )
( col1 = `bbbbb` col2 = 2 col3 = 'yyyyy' col4 = icon_green_light )
( col1 = `ccccc` col2 = 3 col3 = 'xxxxx' col4 = icon_green_light ) ).
TRY.
"----------- Instantiating an ALV table object -----------
cl_salv_table=>factory( IMPORTING r_salv_table = alv
CHANGING t_table = itab ).
"----------- Creating a new instance with new data -----------
"Notes on the set_data method:
"- All objects that were referred to previously are removed.
"- Not to be used in event handlers.
"In the example, new lines are added to the existing internal table.
itab = VALUE #( BASE itab ( col1 = `ddddd` col2 = 4
col3 = `wwwww` col4 = icon_green_light )
( col1 = `ddddd` col2 = 5
col3 = `vvvvv` col4 = icon_green_light )
( col1 = `eeeee` col2 = 6
col3 = `uuuuu` col4 = icon_green_light ) ).
alv->set_data( CHANGING t_table = itab ).
"----------- Layout-related settings -----------
"Changing the list header title
CAST cl_salv_display_settings(
alv->get_display_settings( ) )->set_list_header( 'Demo Title' ).
"There are several methods to retrieve column-specific information.
"The following examples show a selection. You can check the variable
"content in the debugger.
DATA(col1obj) = alv->get_columns( )->get_column( 'COL1' ).
DATA(col1alignment) = col1obj->get_alignment( ).
DATA(col1type) = col1obj->get_ddic_inttype( ).
DATA(col1colname) = col1obj->get_columnname( ).
DATA(is_col1_visible) = col1obj->is_visible( ).
"Settings for the column header
"This example uses the get method, which returns all column objects of
"the output table. The table is processed in a loop.
"Settings covered:
"- Specifying the column header titles (long, medium, and short
" column header)
"- Specifying a tooltip for the column header
LOOP AT alv->get_columns( )->get( ) REFERENCE INTO DATA(colref).
"Specifying the column header titles
"The example sets the texts based on the columname value.
"Note: The column width is optimized further down. You may want to
"manually adjust the width in the ALV output to see the column header
"name change.
colref->r_column->set_long_text( |{ colref->columnname }| ).
colref->r_column->set_medium_text( |{ colref->columnname+2(3) }| ).
colref->r_column->set_short_text( |{ colref->columnname+3(3) }| ).
"Specifying a tooltip for the column headers
colref->r_column->set_tooltip(
|Demo tooltip { colref->columnname+3(3) }| ).
ENDLOOP.
"Displaying/Hiding the column headers
"In the example, the value is set to abap_true. Set it to abap_false
"to hide the column header.
"Note: In this and the following examples, the settings are
"intentionally done using separate casts for each example. You can also
"create object reference variables, such as for 'col1obj' to avoid having
"to specify the casts explicitly each time.
CAST cl_salv_columns_list(
alv->get_columns( ) )->set_headers_visible( abap_true ).
"Setting key columns
"Note: The key columns have a default color setting. There, you can only
"change the color in individual cells.
CAST cl_salv_column_table(
alv->get_columns( )->get_column( 'COL1' ) )->set_key( abap_true ).
"Setting column color
"Note: The executable example includes examples of coloring entire rows and
"specific cells.
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL2' )
)->set_color( VALUE lvc_s_colo( col = col_positive ) ).
"Hiding columns
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL5' )
)->set_visible( abap_false ).
"Setting text alignment
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL2' )
)->set_alignment( if_salv_c_alignment=>left ).
"Optimizing column width, i.e. automatically adjusting the column width
"to display values completely (can also be done for individual columns)
alv->get_columns( )->set_optimize( abap_true ).
"Setting output width explicitly (for an individual column)
"Note: Just to demonstrate the method call. This setting has no effect in
"the example due to the column width optimization above, and the column is
"hidden anyway.
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL5' )
)->set_output_length( '100' ).
"Setting table rows to a striped pattern
alv->get_display_settings( )->set_striped_pattern( abap_true ).
"Displaying/hiding horizontal and vertical grid lines (here, they're hidden)
alv->get_display_settings( )->set_horizontal_lines( abap_false ).
alv->get_display_settings( )->set_vertical_lines( abap_false ).
"With the following method call, the ALV output is displayed in a dialog box.
"Demo coordinates are provided. You can comment it in to see the effect.
* alv->set_screen_popup( start_column = 10
* end_column = 100
* start_line = 4
* end_line = 15 ).
"------ Adding functionality/user interaction options to the ALV output ------
"Specifying the selection type
"The following specification allows you to select multiple rows/columns (a column
"is added on the left for selections). Check the other options in the
"if_salv_c_selection_mode interface.
alv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).
"Setting the sorting
"In the example, a particular column is sorted in descending order.
alv->get_sorts( )->add_sort( 'COL1' )->set_sequence( if_salv_c_sort=>sort_down ).
"Applying a filter
"The example is implemented to exclude one entry from the demo table.
alv->get_filters( )->add_filter( 'COL2' )->add_selopt( sign = 'E'
option = 'EQ'
low = '6' ).
"Making a calculation/aggregation
"In this example, an aggregation is added for a specific column.
"In this case, the average value is calculated. It is displayed in a row added
"at the bottom of the table.
alv->get_aggregations( )->add_aggregation(
columnname = 'COL2'
aggregation = if_salv_c_aggregation=>average ).
"Generic and custom ALV functions
"Note:
"- By default, ALV functions (such as sorting or filtering) are not available
" to users. You must explicitly enable them.
"- Depending on whether the display is full-screen (as it is here) or in a
" container, restrictions apply. In the first case, you can use your own GUI status
" to integrate custom functions.
"This (self-contained, ready-to-use) code snippet is intended so that you can simply
"copy & paste the code into a test program. For this purpose, and to be able to
"demonstrate custom functions in this full-screen ALV example using a GUI status, the
"example uses a demo GUI status that is included in another sample program (in a
"subpackage of SALV). For your own GUI status, you can check the SALV_TABLE_STANDARD
"status contained in the SALV_METADATA_STATUS function group, which you can copy and
"use as a template, for example.
"*********************************** NOTE ***********************************
"- The GUI status used here is just reused to have a copyable and self-contained example.
"- The GUI status of the sample program (specified for the report parameter below)
" contains generic and additional functions. For the additional functions, a simple
" implementation is included in this snippet. The GUI status should include the
" functions TEST, DATA, QUIT.
"- The implementations in the event handler class here do not match the implementations
" there, the button texts do not make sense for the implementations here, and so on.
"- So even though it (the button names, icons, implementations, etc.) does not make much
" sense for this snippet, you should get an idea about custom functions and be able to
" explore the event handling by clicking on the buttons.
"- The TEST function demonstrates adding new rows to the table and the 'refresh' method.
"- If this particular program, the GUI status and/or the custom functions are not available
" in your system, or if you insert the code into your own test program, run it, and
" encounter problems setting the status, remove the TRY control structure that contains
" the set_screen_status method call, and comment in the code below to use the standard
" ALV functions. In this case, the custom functions cannot be checked out with this
" snippet.
"- Check out the executable example of the cheat sheet that includes a GUI status.
TRY.
alv->set_screen_status(
pfstatus = 'SALV_TABLE_STANDARD'
report = 'SALV_TEST_REFRESH'
set_functions = alv->c_functions_all ).
"In this GUI status, the custom functions TEST, DATA, and QUIT are specified. You can
"check the results of the following method calls in the debugger.
DATA(getfunc) = alv->get_functions( ).
"Checking the activation status
DATA(is_enabled) = getfunc->is_enabled( 'TEST' ).
"Checking the visibility
DATA(is_visible) = getfunc->is_visible( 'TEST' ).
"Registering an event handler for the custom functions
"The added_function event is raised. In the implementation of the event handler method,
"you can then implement your code based on the value of the e_salv_function parameter
"(which contains the specified function name), for example, using a CASE statement.
"The implementation in this code snippet is different from the original program.
SET HANDLER lcl_events=>func_click FOR alv->get_event( ).
CATCH cx_salv_object_not_found.
MESSAGE `GUI status error. Instead, use the set_default method, for example.`
TYPE 'I'.
ENDTRY.
"If you remove the TRY control structure above, you can comment in the following
"code to use the generic ALV functions. Use the set_all method for all generic
"functions. You can also enable generic ALV functions individually. Check the
"set_* methods.
"alv->get_functions( )->set_default( abap_true ).
"Hotspot/single click functionality
"You can define the content of a cell as a clickable area. You can do this by
"specifying the cell type hotspot. When a user clicks the cell content,
"the link_click event is raised.
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL1' )
)->set_cell_type( if_salv_c_cell_type=>hotspot ).
"You can set icon columns and make the icon cells clickable areas. To do this,
"use the set_icon method. As above, the cell type of the column is set to hotspot.
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL4' )
)->set_icon( if_salv_c_bool_sap=>true ).
CAST cl_salv_column_table( alv->get_columns( )->get_column( 'COL4' )
)->set_cell_type( if_salv_c_cell_type=>hotspot ).
"An event handler is registered for the clickable areas (the link_click event
"is raised). The get_event method returns the required event object. In the
"example, a message is displayed.
SET HANDLER lcl_events=>single_click FOR alv->get_event( ).
"Double click functionality
"Registering an event handler for the double click event. In the example, a message
"is displayed. Double-click cells in columns for which the cell type is not set to hotspot.
SET HANDLER lcl_events=>double_click FOR alv->get_event( ).
"Adding tooltips to the icon column cells
alv->get_functional_settings( )->get_tooltips( )->add_tooltip(
type = cl_salv_tooltip=>c_type_icon
value = CONV lvc_value( icon_green_light )
tooltip = `This is a tooltip for an icon` ).
"----------- Displaying the ALV output -----------
alv->display( ).
CATCH cx_root INTO DATA(error).
"For simplicity, this example uses the root exception class.
"Always make sure that you use appropriate exception classes. Check the F2
"information for the methods in ADT.
MESSAGE error->get_text( ) TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
More Information
- ABAP Keyword Documentation:
- ALV:
- SAP List Viewer (ALV) on the SAP Help Portal
- Find more demonstration examples in package
SALVand its subpackages.
Executable Examples
After the import of the repository, proceed as follows:
- Find the program in ADT using the search by choosing
CTRL + SHIFT + A. - Enter
ZDEMO_ABAP_SELSCR_LISTS_INTROand open the program. This program serves as an entry point for all selection screen and list examples. Program names of the individual examples:ZDEMO_ABAP_SELSCR_PARAMETERS: DemonstratesPARAMETERSstatementsZDEMO_ABAP_SELSCR_SELECT_OPT: DemonstratesSELECT-OPTIONSstatementsZDEMO_ABAP_SELSCR_STANDALONE: Demonstrates the creation (SELECTION-SCREENstatements) and calling of standalone selection screensZDEMO_ABAP_SELSCR_STMTS_VAR: Demonstrates variants of theSELECTION-SCREENstatements that do not create selection screensZDEMO_ABAP_LISTS: Demonstrates various ABAP statements to create and handle classic listsZDEMO_ABAP_EVENT_BLOCKS: Demonstrates event blocksZDEMO_ABAP_ALV: Demonstrates the SAP List Viewer (ALV)
- Run the program by choosing
F8.
Note
- The examples in the main branch of the ABAP cheat sheet repository are designed to be imported into the SAP BTP ABAP Environment. For Standard ABAP, you can find examples (such as
ZDEMO_ABAP_SELSCR_LISTS_INTRO) in the other branches of the repository.- The executable examples ...
- do not claim to include meaningful selection screens and lists.
- are not intended to be role models for proper selection screen and list design.
- are not intended to solve concrete programming tasks. You should always work out your own solution for each individual case.
- are only intended to demonstrate a selection of keywords and visualize related syntax in action on a high level.
- include comments in the program code.
- The steps to import and run the code are outlined here.
- Disclaimer