Create ALV with class CL_SALV_TABLE

Create ALV with class CL_SALV_TABLE

Example of implementation in SE38 : SALV_DEMO_TABLE_SELECTIONS.

Simply display ALV table


  DATA : go_alv_table TYPE REF TO CL_SALV_TABLE.

  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_alv_table
        CHANGING
          t_table      = gt_table_to_display ).
    CATCH cx_salv_msg.
  ENDTRY.

  " Optimize columns width
  DATA(lo_columns) = go_alv_table->get_columns( ).
  lo_columns->set_optimize( ).

  " Apply zebra style to lv_rows
  DATA(lo_display) = go_alv_table->get_display_settings( ).
  lo_display->set_striped_pattern( cl_salv_display_settings=>true ).

Add ALV toolbar

Add ALV default toolbar


  " ---------------------------------------------------------
  " Création de la Toolbar
  " ---------------------------------------------------------
  DATA(lo_functions) = go_alv_table->get_functions( ).

  lo_functions->set_all( ).

Add custom toolbar with custom buttons


  " ---------------------------------------------------------
  " Création de la Toolbar
  " ---------------------------------------------------------
  DATA(lo_functions) = go_alv_table->get_functions( ).

  " The GUI status ALV_STATUS must be created
  go_alv_table->set_screen_status(
    pfstatus      =  'ALV_STATUS'
    report        =  sy-repid
    set_functions = go_alv_table->c_functions_all ).
  " ---------------------------------------------------------

Code to display or hide a toolbar button :


  lo_functions = go_alv_table->get_functions( ).
  DATA(lt_func_list) = lo_functions->get_functions( ).
  LOOP AT lt_func_list INTO DATA(ls_func_list).
    IF ls_func_list-r_function->get_name( ) = 'BATCH_RECA'.
      " Hide the button :
      ls_func_list-r_function->set_visible( ' ' ).
      EXIT.
    ENDIF.
  ENDLOOP.

Declare handlers

Example with 3 handlers,
on_user_command ==> When user click on toolbar button for instance
on_double_click ==> Double click on a cell
on_link_click ==> Simple click on a cell

Declare a local class to receive Events :


*&---------------------------------------------------------------------*
" Example of local class for ALV with 3 events
*&---------------------------------------------------------------------*
CLASS lcl_alv_handle_events DEFINITION.
  PUBLIC SECTION.
    METHODS:
      on_user_command FOR EVENT added_function OF cl_salv_events
        IMPORTING e_salv_function.

      on_double_click FOR EVENT double_click OF cl_salv_events_table
        IMPORTING row column,

      on_link_click FOR EVENT link_click OF cl_salv_events_table
        IMPORTING row column.
ENDCLASS.                    "lcl_handle_events DEFINITION

CLASS lcl_alv_handle_events IMPLEMENTATION.
  METHOD on_user_command.
    PERFORM handle_user_command USING e_salv_function.
  ENDMETHOD.                    "on_user_command

  METHOD on_double_click.
    PERFORM show_cell_info USING 0 row column TEXT-i07.
  ENDMETHOD.                    "on_double_click

  METHOD on_link_click.
    PERFORM show_cell_info USING 0 row column TEXT-i06.
  ENDMETHOD.                    "on_single_click
ENDCLASS.                    "lcl_handle_events IMPLEMENTATION

Plug ALV handlers with local class :


  DATA(lo_alv_table_event) = go_alv_table->get_event( ).
  DATA(lo_lcl_handler_event) = NEW lcl_alv_handle_events( ).

  SET HANDLER lo_lcl_handler_event->on_user_command FOR lo_alv_table_event.

Refresh ALV data


  go_alv_table->refresh( ).

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply