When you want to display a simple ALV, most of the time you will refer your old program with ALV and copy the code. Here is a sample that reduces the line of code and allows you to create ALV quickly.

This used below functionalities from factory classes.

  • Sets all ALV functions on the toolbar
  • Optimizes the column width
  • Sets stiped pattern
  • Displays ALV
SELECT * FROM sbook INTO TABLE @DATA(bookings).

  "Create ALV table object for the output data table
  cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lo_tab)
                          CHANGING  t_table      = bookings ).
  lo_tab->get_functions( )->set_all( ).
  lo_tab->get_columns( )->set_optimize( ).
  lo_tab->get_display_settings( )->set_striped_pattern( abap_true ).

  lo_tab->display( ).

  CATCH cx_root.   
    MESSAGE 'Error in ALV creation' TYPE 'E'.

And you get the below output.

Ensure that when you create the table type, you use data element reference and not types like CHAR10 so that the field labels are displayed correctly.

If you still need to modify the field labels. Use below additional code. Good way to do this is create a method.

DATA(lo_columns) = lo_tab->get_columns( ). 
DATA(lo_column) = lo_columns->get_column( 'FIELD_NAME' ).

lo_column->set_long_text( 'LONG_TEXT').
lo_column->set_medium_text( 'MEDIUM_TEXT' ).
lo_column->set_short_text( 'SHORT_TEXT' ).

Visit ABAP Code Samples page for more code samples.

If you like the content, please subscribe…

Join 2,647 other followers