0% found this document useful (0 votes)
45 views

Laurix Abap Salv Cheatsheet

The document provides code snippets for getting started with the ALV (Advanced List Viewer) grid in ABAP. It includes basics for initializing and displaying an ALV grid, setting column properties, handling toolbar and custom functions, registering hotspots, adding sorting and selections, using variants and aggregations, and coloring list data and rows/cells.

Uploaded by

Orsis Lynx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Laurix Abap Salv Cheatsheet

The document provides code snippets for getting started with the ALV (Advanced List Viewer) grid in ABAP. It includes basics for initializing and displaying an ALV grid, setting column properties, handling toolbar and custom functions, registering hotspots, adding sorting and selections, using variants and aggregations, and coloring list data and rows/cells.

Uploaded by

Orsis Lynx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

LAURIX

CONSULTING
SALV: ALV List Quickstart
Snippets to get your ALV Grid
up and running in no time �
laurix.com/post/abap/alv/salv-quickstart

Basics Handling toolbar functions


cl_salv_table=>factory( "Enable default ALV toolbar functions
IMPORTING lo_alv->get_functions( )->set_default(
r_salv_table = DATA(lo_alv) abap_true ).
CHANGING
t_table = mt_data ). Custom functions require a custom GUI status. Copy status
"Do stuff… SALV_STANDARD of program SALV_DEMO_* to get up and running.
lo_alv->display( ).
"Register a custom GUI status for an ALV
lo_alv->set_screen_status(
Column settings pfstatus = 'SALV_STANDARD'
"Change header text report = sy-repid
DATA(lo_cols) = lo_alv->get_columns( ). set_functions = lo_alv->c_functions_all ).
DATA(lo_col) = lo_cols->get_column( 'COLNAME' ).
lo_col->set_long_text( 'LONG_HEADER' ). "Handler method for custom functions
lo_col->set_medium_text( 'MEDIUM_HEADER' ). METHODS on_user_command
lo_col->set_short_text( 'SHORT_HEADER' ). FOR EVENT added_function OF cl_salv_events
IMPORTING !e_salv_function.
"Optimize column width
DATA(lo_cols) = lo_alv->get_columns( ). METHOD on_user_command.
lo_cols->set_optimize( abap_true ). CASE e_salv_function.
WHEN 'MYFUNCTION'.
"Sort a column up "Handle custom function
lo_alv->get_sorts( )->add_sort( 'COLUMNNAME' ). ENDCASE.
ENDMETHOD.

Hotspots
"Register column Coloring list data
DATA(lo_column_tab) = CAST cl_salv_column_table( "Colored cell/row
lo_alv->get_columns( )->get_column( "Add color column to data structure
'COLUMNNAME') ). TYPES:
lo_column_tab->set_cell_type( BEGIN OF ty_s_data,
if_salv_c_cell_type=>hotspot ). carrid TYPE s_carr_id,
seatsocc TYPE s_seatsocc,
"Register click handler t_color TYPE lvc_t_scol, "Color column
DATA(lo_events) = lo_alv->get_event( ). END OF ty_s_data.
SET HANDLER lcl_application->on_link_click
FOR lo_events. DATA mt_data TYPE TABLE OF ty_s_data.

"Handler implementation "Color a particular cell based on a condition


CLASS lcl_application DEFINITION. LOOP AT mt_data
PRIVATE SECTION. ASSIGNING FIELD-SYMBOL(<s_data>)
METHODS on_link_click WHERE seatsocc > 300.
FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column. APPEND VALUE #(
ENDCLASS. fname = 'SEATSOCC'
CLASS lcl_application IMPLEMENTATION. color-col = col_negative
METHOD on_link_click. color-int = 0 "'Intensified' off
"Handle hotspot click… color-inv = 0 "'Inverse' off
ENDMETHOD. ) TO <s_data>-t_color.
ENDCLASS. ENDLOOP.

Line selection mode "Color an entire row based on a condition


lo_alv->get_selections( )->set_selection_mode( LOOP AT mt_data ASSIGNING <s_data>
if_salv_c_selection_mode=>single ). WHERE seatsocc < 100.
"Don't specify a field name to color whole row

Variants APPEND VALUE #( color = col_positive )


TO <s_data>-t_color.
DATA(lo_layout) = lo_alv->get_layout( ). ENDLOOP.
"Set layout key to identify the particular ALV
lo_layout->set_key( "Register the color column
VALUE #( report = sy-repid ) ). lo_alv->get_columns( )->set_color_column(
"Remove restriction on saving layouts 'T_COLOR' ).
lo_layout->set_save_restriction(
if_salv_c_layout=>restrict_none ). "Colored column
"Set initial layout DATA(lo_col_tab) = CAST cl_salv_column_table(
lo_layout set_initial_layout( 'VARIANT_NAME' ). lo_alv->get_columns( )->get_column(
"Allow setting layouts as default layouts 'COLUMNNAME' ) ).
lo_layout->set_default( abap_true ).
"It's impossible to change color for key fields,
Aggregations "so unregister as key field when needed
lo_col_tab->set_key( abap_false ).
"Show total for column 'COLUMNNAME'
lo_col_tab->set_color(
DATA(lo_aggrs) = lo_alv->get_aggregations( ).
VALUE #( col = col_positive ) ).
lo_aggrs->add_aggregation(
columnname = 'COLUMNNAME'
aggregation = if_salv_c_aggregation=>total ).

www.laurix.com

You might also like