ABAP ALV with CL_SALV_TABLE: Modern Table Display

Category
ABAP-Statements
Published
Author
Johannes

CL_SALV_TABLE is the modern object-oriented API for ALV (ABAP List Viewer). It replaces the older function modules (REUSE_ALV_*) and offers a clean, typed interface for table displays.

Basic Structure

DATA: lo_alv TYPE REF TO cl_salv_table.
TRY.
" Create ALV
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_data
).
" Configuration...
" Display
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_error).
WRITE: / lx_error->get_text( ).
ENDTRY.

Examples

1. Simplest ALV Display

REPORT z_alv_simple.
TYPES: BEGIN OF ty_customer,
id TYPE i,
name TYPE string,
city TYPE string,
END OF ty_customer.
DATA: lt_customers TYPE TABLE OF ty_customer,
lo_alv TYPE REF TO cl_salv_table.
" Test data
lt_customers = VALUE #(
( id = 1 name = 'Miller Inc.' city = 'Berlin' )
( id = 2 name = 'Smith Corp' city = 'Munich' )
( id = 3 name = 'Weber Ltd' city = 'Hamburg' )
).
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_customers
).
lo_alv->display( ).
CATCH cx_salv_msg.
ENDTRY.

2. Configure Columns

DATA: lo_columns TYPE REF TO cl_salv_columns_table,
lo_column TYPE REF TO cl_salv_column.
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_customers
).
" Get columns object
lo_columns = lo_alv->get_columns( ).
" Optimize column width
lo_columns->set_optimize( abap_true ).
" Configure individual column
lo_column = lo_columns->get_column( 'ID' ).
lo_column->set_short_text( 'ID' ).
lo_column->set_medium_text( 'Cust ID' ).
lo_column->set_long_text( 'Customer Number' ).
lo_column->set_output_length( 10 ).
lo_column = lo_columns->get_column( 'NAME' ).
lo_column->set_long_text( 'Customer Name' ).
lo_column = lo_columns->get_column( 'CITY' ).
lo_column->set_long_text( 'City' ).
lo_alv->display( ).
CATCH cx_salv_not_found cx_salv_msg.
ENDTRY.

3. Hide Column

TRY.
lo_column = lo_columns->get_column( 'INTERNAL_FIELD' ).
lo_column->set_visible( abap_false ).
CATCH cx_salv_not_found.
ENDTRY.

4. Enable Toolbar

DATA: lo_functions TYPE REF TO cl_salv_functions_list.
" Enable all standard functions
lo_functions = lo_alv->get_functions( ).
lo_functions->set_all( abap_true ).
" Or individual functions
lo_functions->set_export_xml( abap_true ).
lo_functions->set_export_spreadsheet( abap_true ).
lo_functions->set_print( abap_true ).
lo_functions->set_find( abap_true ).
lo_functions->set_aggregation( abap_true ).
lo_functions->set_filter( abap_true ).
lo_functions->set_sort_asc( abap_true ).
lo_functions->set_sort_desc( abap_true ).

5. Set Sorting

DATA: lo_sorts TYPE REF TO cl_salv_sorts.
lo_sorts = lo_alv->get_sorts( ).
TRY.
" Sort by city (ascending)
lo_sorts->add_sort(
columnname = 'CITY'
position = 1
sequence = if_salv_c_sort=>sort_up
).
" Second sort by name
lo_sorts->add_sort(
columnname = 'NAME'
position = 2
sequence = if_salv_c_sort=>sort_up
).
CATCH cx_salv_not_found cx_salv_existing cx_salv_data_error.
ENDTRY.

6. Set Filter

DATA: lo_filters TYPE REF TO cl_salv_filters.
lo_filters = lo_alv->get_filters( ).
TRY.
" Filter by city
lo_filters->add_filter(
columnname = 'CITY'
sign = 'I'
option = 'EQ'
low = 'Berlin'
).
CATCH cx_salv_not_found cx_salv_existing cx_salv_data_error.
ENDTRY.

7. Aggregation (Totals)

DATA: lo_aggregations TYPE REF TO cl_salv_aggregations.
lo_aggregations = lo_alv->get_aggregations( ).
TRY.
" Sum for amount column
lo_aggregations->add_aggregation(
columnname = 'AMOUNT'
aggregation = if_salv_c_aggregation=>total
).
" Average
lo_aggregations->add_aggregation(
columnname = 'QUANTITY'
aggregation = if_salv_c_aggregation=>average
).
CATCH cx_salv_not_found cx_salv_existing cx_salv_data_error.
ENDTRY.

8. Zebra Stripes and Layout

DATA: lo_display TYPE REF TO cl_salv_display_settings.
lo_display = lo_alv->get_display_settings( ).
" Zebra pattern (alternating row colors)
lo_display->set_striped_pattern( abap_true ).
" Set title
lo_display->set_list_header( 'Customer List' ).
" Horizontal lines
lo_display->set_horizontal_lines( abap_true ).
" Vertical lines
lo_display->set_vertical_lines( abap_true ).

9. Handle Events (Double-Click)

" Event handler class
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_double_click
FOR EVENT double_click OF cl_salv_events_table
IMPORTING row column.
METHODS: on_link_click
FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION.
METHOD on_double_click.
" Read data
READ TABLE lt_customers INTO DATA(ls_cust) INDEX row.
IF sy-subrc = 0.
MESSAGE |Double-click on { ls_cust-name }| TYPE 'I'.
ENDIF.
ENDMETHOD.
METHOD on_link_click.
MESSAGE |Link clicked: Row { row }, Column { column }| TYPE 'I'.
ENDMETHOD.
ENDCLASS.
" Main program
DATA: lo_events TYPE REF TO cl_salv_events_table,
lo_handler TYPE REF TO lcl_event_handler.
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_customers
).
" Register event handler
lo_events = lo_alv->get_event( ).
lo_handler = NEW #( ).
SET HANDLER lo_handler->on_double_click FOR lo_events.
SET HANDLER lo_handler->on_link_click FOR lo_events.
lo_alv->display( ).
CATCH cx_salv_msg.
ENDTRY.

10. Hotspot (Clickable Cells)

TRY.
lo_column = lo_columns->get_column( 'ID' ).
" Mark column as hotspot (link)
lo_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
CATCH cx_salv_not_found.
ENDTRY.

11. Set Colors

TYPES: BEGIN OF ty_colored_line,
id TYPE i,
name TYPE string,
status TYPE c LENGTH 1,
t_color TYPE lvc_t_scol, " Color information
END OF ty_colored_line.
DATA: lt_data TYPE TABLE OF ty_colored_line,
ls_color TYPE lvc_s_scol.
" Fill data with colors
lt_data = VALUE #(
( id = 1 name = 'OK' status = 'G' )
( id = 2 name = 'Error' status = 'R' )
( id = 3 name = 'Warning' status = 'Y' )
).
" Set colors
LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<ls_line>).
CLEAR ls_color.
ls_color-fname = 'STATUS'.
CASE <ls_line>-status.
WHEN 'G'.
ls_color-color-col = col_positive. " Green
WHEN 'R'.
ls_color-color-col = col_negative. " Red
WHEN 'Y'.
ls_color-color-col = col_total. " Yellow
ENDCASE.
APPEND ls_color TO <ls_line>-t_color.
ENDLOOP.
" ALV with colors
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_data
).
" Assign color column
lo_columns = lo_alv->get_columns( ).
lo_columns->set_color_column( 'T_COLOR' ).
" Hide color column
lo_column = lo_columns->get_column( 'T_COLOR' ).
lo_column->set_visible( abap_false ).
lo_alv->display( ).
CATCH cx_salv_not_found cx_salv_msg.
ENDTRY.

12. Save/Load Layout

DATA: lo_layout TYPE REF TO cl_salv_layout,
ls_key TYPE salv_s_layout_key.
" Define layout key
ls_key-report = sy-repid.
lo_layout = lo_alv->get_layout( ).
lo_layout->set_key( ls_key ).
" User can save layout
lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
" Default layout
lo_layout->set_default( abap_true ).

13. Add Custom Buttons

" Only possible in fullscreen mode!
DATA: lo_functions TYPE REF TO cl_salv_functions.
" Set status GUI (for custom buttons)
lo_alv->set_screen_status(
pfstatus = 'ZSTATUS'
report = sy-repid
set_functions = lo_alv->c_functions_all
).
" Event handler for user command
CLASS lcl_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_user_command
FOR EVENT added_function OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS.
CLASS lcl_handler IMPLEMENTATION.
METHOD on_user_command.
CASE e_salv_function.
WHEN 'ZREFRESH'.
" Refresh data
refresh_data( ).
lo_alv->refresh( ).
WHEN 'ZEXPORT'.
" Custom export
export_data( ).
ENDCASE.
ENDMETHOD.
ENDCLASS.
" Register handler
DATA: lo_events TYPE REF TO cl_salv_events.
lo_events = lo_alv->get_event( ).
SET HANDLER lo_handler->on_user_command FOR lo_events.

14. ALV in Container (Screen)

" For screen programming
DATA: lo_container TYPE REF TO cl_gui_custom_container.
" Create container (in screen: Custom Control 'CC_ALV')
lo_container = NEW #( container_name = 'CC_ALV' ).
TRY.
" ALV in container
cl_salv_table=>factory(
EXPORTING r_container = lo_container
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_data
).
" Configuration...
lo_alv->display( ).
CATCH cx_salv_msg.
ENDTRY.

15. Refresh Data

" Data was changed
lt_customers = VALUE #(
( id = 1 name = 'New' city = 'Berlin' )
( id = 2 name = 'Updated' city = 'Munich' )
).
" Refresh ALV
lo_alv->refresh( ).
" Keep scroll position
lo_alv->refresh(
s_stable = VALUE lvc_s_stbl( row = abap_true col = abap_true )
).

16. Editable ALV

" Unfortunately not directly possible with CL_SALV_TABLE!
" For editable ALV: Use CL_GUI_ALV_GRID
" Workaround: Read data from table after display
" (table was passed as CHANGING)

17. Export Functions

DATA: lo_functions TYPE REF TO cl_salv_functions_list.
lo_functions = lo_alv->get_functions( ).
" Enable Excel export
lo_functions->set_export_spreadsheet( abap_true ).
" XML export
lo_functions->set_export_xml( abap_true ).
" All export options
lo_functions->set_group_export( abap_true ).

18. Complete Example

REPORT z_alv_complete.
TYPES: BEGIN OF ty_order,
order_id TYPE i,
customer TYPE string,
amount TYPE p DECIMALS 2,
currency TYPE waers,
order_date TYPE d,
status TYPE c LENGTH 1,
END OF ty_order.
DATA: lt_orders TYPE TABLE OF ty_order,
lo_alv TYPE REF TO cl_salv_table.
CLASS lcl_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_double_click
FOR EVENT double_click OF cl_salv_events_table
IMPORTING row column.
ENDCLASS.
CLASS lcl_handler IMPLEMENTATION.
METHOD on_double_click.
READ TABLE lt_orders INTO DATA(ls_order) INDEX row.
IF sy-subrc = 0.
MESSAGE |Order { ls_order-order_id }: { ls_order-amount } { ls_order-currency }| TYPE 'I'.
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" Test data
lt_orders = VALUE #(
( order_id = 1001 customer = 'Miller' amount = '1500.00' currency = 'EUR' order_date = '20241101' status = 'A' )
( order_id = 1002 customer = 'Smith' amount = '2300.50' currency = 'EUR' order_date = '20241105' status = 'A' )
( order_id = 1003 customer = 'Weber' amount = '890.00' currency = 'EUR' order_date = '20241110' status = 'O' )
( order_id = 1004 customer = 'Baker' amount = '3200.00' currency = 'EUR' order_date = '20241112' status = 'A' )
( order_id = 1005 customer = 'Klein' amount = '450.75' currency = 'EUR' order_date = '20241115' status = 'X' )
).
TRY.
" Create ALV
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_alv
CHANGING t_table = lt_orders
).
" Enable toolbar
DATA(lo_functions) = lo_alv->get_functions( ).
lo_functions->set_all( abap_true ).
" Configure columns
DATA(lo_columns) = lo_alv->get_columns( ).
lo_columns->set_optimize( abap_true ).
DATA(lo_col_id) = lo_columns->get_column( 'ORDER_ID' ).
lo_col_id->set_long_text( 'Order Number' ).
lo_col_id->set_cell_type( if_salv_c_cell_type=>hotspot ).
DATA(lo_col_cust) = lo_columns->get_column( 'CUSTOMER' ).
lo_col_cust->set_long_text( 'Customer' ).
DATA(lo_col_amt) = lo_columns->get_column( 'AMOUNT' ).
lo_col_amt->set_long_text( 'Amount' ).
DATA(lo_col_date) = lo_columns->get_column( 'ORDER_DATE' ).
lo_col_date->set_long_text( 'Order Date' ).
" Sum for amount
DATA(lo_agg) = lo_alv->get_aggregations( ).
lo_agg->add_aggregation( columnname = 'AMOUNT' aggregation = if_salv_c_aggregation=>total ).
" Sorting
DATA(lo_sorts) = lo_alv->get_sorts( ).
lo_sorts->add_sort( columnname = 'ORDER_DATE' sequence = if_salv_c_sort=>sort_down ).
" Display settings
DATA(lo_display) = lo_alv->get_display_settings( ).
lo_display->set_striped_pattern( abap_true ).
lo_display->set_list_header( 'Order Overview' ).
" Events
DATA(lo_events) = lo_alv->get_event( ).
DATA(lo_handler) = NEW lcl_handler( ).
SET HANDLER lo_handler->on_double_click FOR lo_events.
" Display
lo_alv->display( ).
CATCH cx_salv_msg cx_salv_not_found cx_salv_existing cx_salv_data_error INTO DATA(lx_error).
WRITE: / lx_error->get_text( ).
ENDTRY.

Important Classes

ClassDescription
CL_SALV_TABLEMain ALV class
CL_SALV_COLUMNS_TABLEColumn collection
CL_SALV_COLUMNIndividual column
CL_SALV_FUNCTIONS_LISTToolbar functions
CL_SALV_DISPLAY_SETTINGSDisplay settings
CL_SALV_SORTSSorting
CL_SALV_FILTERSFilters
CL_SALV_AGGREGATIONSAggregations
CL_SALV_EVENTS_TABLEEvent handling
CL_SALV_LAYOUTLayout management

Important Notes / Best Practice

  • CL_SALV_TABLE is the modern ALV API – prefer it over REUSE_ALV*.
  • factory() creates the ALV object – CHANGING for the data table.
  • Column optimization with set_optimize( abap_true ) for automatic widths.
  • Toolbar with get_functions( )->set_all( abap_true ) to enable.
  • Events registered via SET HANDLER.
  • Hotspots make cells clickable (set_cell_type).
  • Colors require a color column of type LVC_T_SCOL.
  • Editable only possible with CL_GUI_ALV_GRID.
  • Container for screen integration.
  • Layout allows users to save their settings.