Display Data in ALV

Summary: This tutorial shows you how to quickly display data in ALV using new ABAP classes such as cl_savl_table.

To display data in an ALV report, you need an internal table which is called the output table to provide data for the ALV.

The internal table can be based on a structure in the ABAP dictionary or a user-defined structure in the report. If it is the first case, you don’t have to set the column heading for ALV because the ALV will use field labels of data elements of the structure’s components.

*---------------------------------------------------------------------*
* Report  YALV_DEMO1
*
*---------------------------------------------------------------------*
* Display flight information in ALV
*---------------------------------------------------------------------*

REPORT  yalv_demo1.

DATA: go_alv      TYPE REF TO   cl_salv_table,
      go_columns  TYPE REF TO   cl_salv_columns,
      go_funcs    TYPE REF TO   cl_salv_functions,
      go_ex       TYPE REF TO        CX_ROOT,
      gt_sflight  TYPE TABLE OF sflight.

START-OF-SELECTION.

  " select data from sflight table
  SELECT * FROM sflight UP TO 100 ROWS
  INTO TABLE gt_sflight.

  IF sy-subrc <> 0.
    MESSAGE 'No data found' TYPE 'I'.
    EXIT.
  ENDIF.

  TRY .
      cl_salv_table=>factory(
         IMPORTING
           r_salv_table   = go_alv
         CHANGING
           t_table        = gt_sflight
        ).

      " set column optimized
      go_columns = go_alv->get_columns( ).
      go_columns->set_optimize( ).

      " set functions
      go_funcs = go_alv->get_functions( ).
      go_funcs->set_all( ).

      go_alv->display( ).
    CATCH cx_salv_msg INTO go_ex.
      MESSAGE go_ex TYPE 'E'.
  ENDTRY.