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
"-----------------------------------------------------------"
" Data declaration for ABAP ALV
"-----------------------------------------------------------"
DATA lo_alv TYPE REF TO cl_salv_table.
DATA lex_message TYPE REF TO cx_salv_msg.
DATA lo_layout_settings TYPE REF TO cl_salv_layout.
DATA lo_layout_key TYPE salv_s_layout_key.
DATA lo_columns TYPE REF TO cl_salv_columns_table.
DATA lo_column TYPE REF TO cl_salv_column.
DATA lex_not_found TYPE REF TO cx_salv_not_found.
DATA lo_functions TYPE REF TO cl_salv_functions_list.
DATA lo_display_settings TYPE REF TO cl_salv_display_settings.
Second, the ABAP Code to create and display an ALV the fast way.
"-----------------------------------------------------------"
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = lt_itab ).
"@ the ABAP Internal Table to display in ALV with ABAP
"@ LT_ITAB to be defined and filled of course
CATCH cx_salv_msg INTO lex_message.
" error handling
ENDTRY.
" Set the ALV Layouts
"-----------------------------------------------------------"
lo_layout_settings = lo_alv->get_layout( ).
lo_layout_key-report = sy-repid.
lo_layout_settings->set_key( lo_layout_key ).
lo_layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
" set the ALV Toolbars
"-----------------------------------------------------------"
lo_functions = lo_alv->get_functions( ).
lo_functions->set_all( ).
" Optimize ALV Columns size
"-----------------------------------------------------------"
lo_columns = lo_alv->get_columns( ).
lo_columns->set_optimize( ).
" Set Zebra Lines display
"-----------------------------------------------------------"
lo_display_settings = lo_alv->get_display_settings( ).
lo_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).
" Set ALV Header title
"-----------------------------------------------------------"
lo_display_settings->set_list_header( 'Your ALV Title' ).
" Hide ALV Columns from Display.
"-----------------------------------------------------------"
TRY.
lo_column = lo_columns->get_column( 'NAME2' ).
lo_column->set_visible( if_salv_c_bool_sap=>false ).
CATCH cx_salv_not_found INTO lex_not_found.
" write some error handling
ENDTRY.
" Change ALV Columns Name ( Short, medium and Long text)
"-----------------------------------------------------------"
TRY.
lo_column = lo_columns->get_column( 'XREF1' ).
lo_column->set_short_text( 'Vendor' ).
lo_column->set_medium_text( 'Vendor' ).
lo_column->set_long_text( 'Vendor Reference' ).
CATCH cx_salv_not_found INTO lex_not_found.
" write some error handling
ENDTRY.
" Display the ALV in ABAP in the whole main screen
"-----------------------------------------------------------"
lo_alv->display( ).