Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
LaurensDeprost
Contributor
The object model for the SAP List Viewer (ALV) is an object-oriented encapsulation of SAPโ€™s much older ALV tool. Today class CL_SALV_TABLE remains widely used for displaying simple, two-dimensional ALV grids.



Get your ALV up and running in no time using this collection of shorts snippets covering most of the basic requirements.


Note: For simplicityโ€™s sake error handling of SALV errors (subclasses of exception class CX_SALV_ERROR) have been left out. Make sure to implement appropriate exception handling for productive code.




A simple, two-dimensional table in the SAP List Viewer (ALV)



Basics


"Instantiation
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lo_alv)
CHANGING
t_table = mt_data ).

"Do stuff
"...

"Display the ALV Grid
lo_alv->display( ).

 

Column settings


Change header text


DATA(lo_column) = lo_alv->get_columns( )->get_column( 'COLUMNNAME' ).
lo_column->set_long_text( 'LONG_HEADER' ).
lo_column->set_medium_text( 'MEDIUM_HEADER' ).
lo_column->set_short_text( 'SHORT_HEADER' ).

 

Optimize column width


lo_alv->get_columns( )->set_optimize( abap_true ).

 

Sort columns


"Sort a column up
lo_alv->get_sorts( )->add_sort( 'COLUMNNAME' ).

 

Hotspots


About


Hotspots are special cells of an ALV list that can be compared to hyperlinks or buttons on web pages/apps. If a user clicks once onto a hotspot field an event is triggered. For fields that are not defined as hotspots, the user must double-click the field or use a function key to trigger an event.


Register hotspot column


DATA(lo_column_tab) = CAST cl_salv_column_table( 
lo_alv->get_columns( )->get_column( 'COLUMNNAME' ) ).

lo_column_tab->set_cell_type( if_salv_c_cell_type=>hotspot ).


Register hotspot click handler


DATA(lo_events) = lo_alv->get_event( ).
SET HANDLER lcl_application->on_link_click FOR lo_events.


Handler implementation


CLASS lcl_application DEFINITION.
PRIVATE SECTION.
METHODS on_link_click
FOR EVENT link_click OF cl_salv_events_table
IMPORTING
row
column.
"...
ENDCLASS.

CLASS lcl_application IMPLEMENTATION.
METHOD on_link_click.
"Handle hotspot click
ENDMETHOD.
ENDCLASS.


 

Aggregations


Class CL_SALV_AGGREGATIONS can be used to display basic statistical facts for a given column. Options include displaying the total, minimum or maximum, and average value.
"Show total for column 'COLUMNNAME'
DATA(lo_aggregations) = lo_alv->get_aggregations( ).
lo_aggregations->add_aggregation(
columnname = 'COLUMNNAME'
aggregation = if_salv_c_aggregation=>total ).

 

List selections


Set selection mode


DATA(lo_selections) = lo_alv->get_selections( ).
"allow single line selection
lo_selections->set_selection_mode( if_salv_c_selection_mode=>single ).

 

Get selected lines


DATA(lo_selections) = lo_alv->get_selections( ).
DATA(lt_selected_row_indices) = lo_selections->get_selected_rows( ).

IF lines( lt_selected_row_indices ) NE 1.
"Select one row for this function
MESSAGE s022(upp_pp_k) DISPLAY LIKE 'W'.
RETURN.
ENDIF.

READ TABLE lt_selected_row_indices INDEX 1 INTO DATA(lv_selected_row_index).
READ TABLE mt_data
INDEX lv_selected_row_index
ASSIGNING FIELD-SYMBOL(<s_selected_row>).
IF sy-subrc NE 0.
"Select one row for this function
MESSAGE s022(upp_pp_k) DISPLAY LIKE 'W'.
RETURN.
ENDIF.

"Handle user action
"...

 

Variants


Initialize: set layout key


DATA(lo_layout) = lo_alv->get_layout( ).
"set layout key for identifying the particular ALV list
lo_layout->set_key( VALUE #( report = sy-repid ) ).

 

Allow variant saving


"remove restriction on saving layouts
lo_alv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_none ).

 

Set initial variant


"set initial layout
lo_alv->get_layout( )->set_initial_layout( 'VARIANT_NAME' ).

 

Allow default variants


"allow setting layouts as default layouts
lo_alv->get_layout( )->set_default( abap_true ).

 

Example: setting up ALV variants


METHODS set_layout
IMPORTING io_alv TYPE REF TO cl_salv_table
RAISING cx_salv_error.

METHOD set_layout.
DATA(lo_layout) = io_alv->get_layout( ).
"set layout key for identifying the particular ALV list
lo_layout->set_key( VALUE #( report = sy-repid ) ).
"remove restriction on saving layouts
lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
"allow setting layouts as default layouts
lo_layout->set_default( abap_true ).
ENDMETHOD.

 

Handling ALV toolbar functions


Default functions


"Enable default ALV toolbar functions
lo_alv->get_functions( )->set_default( abap_true ).

 

Custom functions



Custom functions with SALV classes require a custom GUI status



Creating a custom GUI status


Instead of manually creating a GUI status we can copy the status SALV_STANDARD of demo program SALV_DEMO_TABLE_FUNCTIONS to quickly get things up and running.



Copying the GUI status of program SALV_DEMO_TABLE_FUNCTIONS


 

This status includes all standard ALV functions as well as a demo custom function.


GUI Status SALV_STANDARD


 

The demo functionโ€™s attributes can be adjusted in the Menu Painter (via transaction SE80 or transaction SE41).


Attributes of the function โ€˜My functionโ€™ of demo GUI status SALV_STANDARD


 

Register an ALV GUI status


CONSTANTS gui_status_name TYPE sypfkey VALUE 'SALV_STANDARD'.

"Register a custom GUI status for an ALV
lo_alv->set_screen_status(
pfstatus = gui_status_name
report = sy-repid
set_functions = lo_alv->c_functions_all ).

 

Handler method for custom functions


"Handler method for custom functions
METHODS on_user_command
FOR EVENT added_function OF cl_salv_events
IMPORTING !e_salv_function .

METHOD on_user_command.
CASE e_salv_function.
WHEN 'MYFUNCTION'.
"Handle custom function
"...
ENDCASE.
ENDMETHOD.

"Register handler for custom function
SET HANDLER on_user_command FOR lo_alv->get_event( ).

 

 

Coloring list data


Colors can help draw attention to outliers in the output data. For example, colors can bring negative balances or successfully handled records to the end userโ€™s attention. We can colorize three different levels in the list output: cells, rows, and columns.


Possible ALV colors, output of SAP Demo program DEMO_LIST_FORMAT_COLOR_1


 

Colored cell/row


Add color column to ALV data structure, type lvc_t_scol


TYPES: 
BEGIN OF ty_s_data,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
seatsocc TYPE s_seatsocc,
t_color TYPE lvc_t_scol, "Color column
END OF ty_s_data.

DATA mt_data TYPE TABLE OF ty_s_data.

 

Color a particular cell


"Color a particular cell based on a condition
LOOP AT mt_data ASSIGNING FIELD-SYMBOL(<s_data>) WHERE seatsocc > 300.

APPEND VALUE #(
fname = 'SEATSOCC'
color-col = col_negative
color-int = 0 "'Intensified' off
color-inv = 0 "'Inverse' off
) TO <s_data>-t_color.
ENDLOOP.

 

Color an entire row


"Color an entire row based on a condition
LOOP AT mt_data ASSIGNING <s_data> WHERE seatsocc < 100.
"Don't specify a field name in order to apply a color to a whole row
APPEND VALUE #( color-col = col_positive ) TO <s_data>-t_color.
ENDLOOP.

 

Register the color column


"Register the color column before displaying the ALV
lo_alv->get_columns( )->set_color_column( 'T_COLOR' ).

 

Colored column




DATA(lo_column_tab) = CAST cl_salv_column_table( 
lo_alv->get_columns( )->get_column( 'COLUMNNAME' ) ).

"It's not possible to change the column color of key fields,
"so unregister the column as key field when needed
lo_column_tab->set_key( abap_false ).

lo_column_tab->set_color( VALUE #( col = col_positive ) ).

 


Conclusion


Hopefully, this post can be a useful reference when developing simple ALVs.
If so, you can click here for a PDF cheat sheet with these code snippets.

Do you have tips for approaching certain ALV requirements or can you think of any other common snippets that are missing? Let's discuss in the comments below ๐Ÿ‘‡



9 Comments
marcioch
Explorer
Great Work! Is helpfully a lot!

 

Att,

Marcio
LaurensDeprost
Contributor
Thanks for the feedback, Marcio
Mehh
Participant
Nice blog, very handy guide.
LaurensDeprost
Contributor
0 Kudos
Thanks Mehmet
ethantew12
Participant

Nice summary with useful tips. Thank you!

I added cl_abap_structdescr to read the local structure and rename it with method get_column.

Code as follow :


lo_structdescr ?= cl_abapstructdescr=>describe_by_name( 'STRUCTURE NAME' ).

DATA( lt_components ) = lo_structdescr->get_components( ).


Now the lt_components hold the table with the field name, which will be useful for me to loop and manage the text that I want to change to display on SALV.

 

shais
Participant
0 Kudos
It might be better to use method GET_INCLUDED_VIEW, which handles also included structures (INCLUDE TYPE).
LaurensDeprost
Contributor
0 Kudos
Thanks ethantew. Great tip ๐Ÿ‘
rina_spector3
Explorer
0 Kudos
Hello very helpful and nice

My contribution To add  fixed columns,

Use  method  cl_salv_column_table  set_key  to mark required columns

Together with  set_key_fixation(abap_true)  in class cl_salv_columns_table
0 Kudos
Hello, itโ€™s very good, summarized overview. Many thanks.

I do have a question. On the first Screenshot above show the Total and the Subtotal Buttons with a little Drop-Down-Arrow.

Thatโ€™s what I wanted to achieve. Especially on the Sub-Total there is the entry for "Drill Down Total Level" and thatโ€™s what I need. But with the SALV the Drop-Downs are not there, just a simple Button.

Do you know how to get them?

Many thanks in Advance.
Labels in this area