W Pub: ABAP Rut ALV Demo F

Copiado de SAP4TECH.NET

Create ALV ABAP is very comment requirment for an ABAP developer. This post will give the fastest way and the most easy way to create and display ALV in ABAP. Create ALV in ABAP

The easiest way to create and display ALV in ABAP is to used the class CL_SALV_TABLE. This class CL_SALV_TABLE offers a very powerful method to create AVL ABAP.

For example,

To initialize the ALV, use the CL_SALV_TABLE=>FACTORY method.
no need anymore to build manually your FIELDCALATOG
or no need to create a SAP DDIC table/structure to retrieve the FIELDCATALOG
it is done by passing juste an internal table.
To display, just call the method CL_SALV_TABLE=>DISPLAY
no need to create a new screen
display method will use the current screen

Create ALV ABAP Sample code

Here a sample code to display an ABAP internal in ALV with:

A basic layout setting including ABAP ALV title
Optimized Columns size
Renaming of Columns text
and a tool bar

First, let start with all the ABAP Declaration to make

  1.   "-----------------------------------------------------------"
  2.   " Data declaration for ABAP ALV
  3.   "-----------------------------------------------------------"
  4.   DATA lo_alv               TYPE REF TO cl_salv_table.
  5.   DATA lex_message          TYPE REF TO cx_salv_msg.
  6.   DATA lo_layout_settings   TYPE REF TO cl_salv_layout.
  7.   DATA lo_layout_key        TYPE        salv_s_layout_key.
  8.   DATA lo_columns           TYPE REF TO cl_salv_columns_table.
  9.   DATA lo_column            TYPE REF TO cl_salv_column.
  10.   DATA lex_not_found        TYPE REF TO cx_salv_not_found.
  11.   DATA lo_functions         TYPE REF TO cl_salv_functions_list.
  12.   DATA lo_display_settings  TYPE REF TO cl_salv_display_settings.

Second, the ABAP Code to create and display an ALV the fast way.

  1. "-----------------------------------------------------------"
  2.   TRY.
  3.       cl_salv_table=>factory(
  4.         IMPORTING
  5.           r_salv_table = lo_alv
  6.         CHANGING
  7.           t_table      = lt_itab ).
  8.       "@ the ABAP Internal Table to display in ALV with ABAP
  9.       "@ LT_ITAB to be defined and filled of course
  10.     CATCH cx_salv_msg INTO lex_message.
  11.       " error handling
  12.   ENDTRY.
  13.  
  14.   " Set the ALV Layouts
  15.   "-----------------------------------------------------------"
  16.   lo_layout_settings   = lo_alv->get_layout( ).
  17.   lo_layout_key-report = sy-repid.
  18.   lo_layout_settings->set_key( lo_layout_key ).
  19.   lo_layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
  20.  
  21.   " set the ALV Toolbars
  22.   "-----------------------------------------------------------"
  23.   lo_functions = lo_alv->get_functions( ).
  24.   lo_functions->set_all( ).
  25.  
  26.   " Optimize ALV Columns size
  27.   "-----------------------------------------------------------"
  28.   lo_columns = lo_alv->get_columns( ).
  29.   lo_columns->set_optimize( ).
  30.  
  31.   " Set Zebra Lines display
  32.   "-----------------------------------------------------------"
  33.   lo_display_settings = lo_alv->get_display_settings( ).
  34.   lo_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).
  35.  
  36.   " Set ALV Header title
  37.   "-----------------------------------------------------------"
  38.   lo_display_settings->set_list_header( 'Your ALV Title' ).
  39.  
  40.   " Hide ALV Columns from Display.
  41.   "-----------------------------------------------------------"
  42.   TRY.
  43.       lo_column = lo_columns->get_column( 'NAME2' ).
  44.       lo_column->set_visible( if_salv_c_bool_sap=>false ).
  45.     CATCH cx_salv_not_found INTO lex_not_found.
  46.       " write some error handling
  47.   ENDTRY.
  48.  
  49.  
  50.   " Change ALV Columns Name ( Short, medium and Long text)
  51.   "-----------------------------------------------------------"
  52.   TRY.
  53.       lo_column = lo_columns->get_column( 'XREF1' ).
  54.       lo_column->set_short_text( 'Vendor' ).
  55.       lo_column->set_medium_text( 'Vendor' ).
  56.       lo_column->set_long_text( 'Vendor Reference' ).
  57.     CATCH cx_salv_not_found INTO lex_not_found.
  58.       " write some error handling
  59.   ENDTRY.
  60.  
  61.   " Display the ALV in ABAP in the whole main screen
  62.   "-----------------------------------------------------------"
  63.   lo_alv->display( ).