is a control which we can use to increase the readability and the functionality of reports in SAP.
it allows user to select the columns that they want to display or hide, for sorting, arrange the order of the columns, group by function where user can sort and group the similar data, and get the total column by column , etc...
its also make the developers life more easier. They have to get the relevent data acording to the selection criteria and pass it to a relevent ALV function ( most of the time its 'REUSE_ALV_GRID_DISPLAY' ) with the correct parameters. Then the function will be handle all the other functions such as sorting, sub totaling etc...
how to use ALV to display a simple report in SAP using ABAP.
*Required Declarations.
DATA: l_alv TYPE REF TO cl_gui_alv_grid.
DATA : formatter TYPE i VALUE 0.
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
gd_layout TYPE slis_layout_alv,
gd_repid LIKE sy-repid,
gt_sort TYPE slis_t_sortinfo_alv,
gt_events TYPE slis_t_event,
gd_prntparams TYPE slis_print_alv.
*displaying ALV
DEFINE m_fieldcat.
ls_fieldcat-col_pos = 0.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
ls_fieldcat-do_sum = &3.
ls_fieldcat-reptext_ddic = &4.
append ls_fieldcat to lt_fieldcat.
clear ls_fieldcat.
END-OF-DEFINITION.
DEFINE m_sort.
add 1 to ls_sort-spos.
ls_sort-fieldname = &1.
ls_sort-up = 'X'.
ls_sort-subtot = &2.
ls_sort-expa = &3.
append ls_sort to lt_sort.
END-OF-DEFINITION.
DATA:
ls_fieldcat TYPE slis_fieldcat_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
lt_sort TYPE slis_t_sortinfo_alv,
ls_sort TYPE slis_sortinfo_alv,
ls_layout TYPE slis_layout_alv,
ls_title TYPE slis_layout_alv_spec1,
lt_sp_group TYPE slis_t_sp_group_alv,
ls_col TYPE slis_layout_alv_spec,
ls_sp_group TYPE slis_sp_group_alv.
m_fieldcat 'CO_CODE' '' '' 'Country Code' .
m_fieldcat 'COUNTRY' '' '' 'Country' .
m_fieldcat '
this is the field catelogue that you declare before. you have to pass the correcr parameter .
(DEFINE m_fieldcat.......................)
m_sort 'CO_CODE' 'X' ' ' .
m_sort 'FF_CODE' 'X' ' ' .
if you want to sort it at the first time its displaying, define the fields that you want to sort.
this is optional. and this the m_sort that we defined eirlier.
(DEFINE m_sort............................)
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-totals_text = 'Totals'(256).
gd_layout-coltab_fieldname = 'CELL_COLOUR'.
* perform build_events.
gd_prntparams-reserve_lines = '4'. "Lines reserved for footer
gd_prntparams-no_coverpage = 'X'.
gd_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = gd_repid
is_layout = gd_layout
it_fieldcat = lt_fieldcat[]
i_grid_title = '
it_sort = lt_sort
it_events = gt_events
is_print = gd_prntparams
TABLES
t_outtab = <>
EXCEPTIONS
program_error = 1
OTHERS = 2.
this will show the final ALV .
you need to change the field names and the itab name of the above code.
this is an example code...
*&---------------------------------------------------------------------*
*& Report ZTESTMARA
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTESTMARA.
tables : MARA.
DATA : BEGIN OF itabrec occurs 0.
INCLUDE STRUCTURE mara.
DATA : END OF itabrec.
************ ALV declarations****************
DATA: l_alv TYPE REF TO cl_gui_alv_grid.
DATA : formatter TYPE i VALUE 0.
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH
HEADER LINE,
gd_layout TYPE slis_layout_alv,
gd_repid LIKE sy-repid,
gt_sort TYPE slis_t_sortinfo_alv,
gt_events TYPE slis_t_event,
gd_prntparams TYPE slis_print_alv.
*********************************************
** Selection options..**********************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS tmatnr FOR mara-matnr .
SELECTION-SCREEN END OF BLOCK b1.
********************************************************
*** Program logic****************************
perform getData.
perform showALV.
*********************************************
*&---------------------------------------------------------------------*
*& Form getData
*&---------------------------------------------------------------------*
* get the data from MARA
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form getData .
select * from mara into table itabrec where
matnr in tmatnr.
endform. " getData
*&---------------------------------------------------------------------*
*& Form showALV
*&---------------------------------------------------------------------*
* Display ALV
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form showALV .
DEFINE m_fieldcat.
ls_fieldcat-col_pos = 0.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
ls_fieldcat-do_sum = &3.
ls_fieldcat-reptext_ddic = &4.
append ls_fieldcat to lt_fieldcat.
clear ls_fieldcat.
END-OF-DEFINITION.
DEFINE m_sort.
add 1 to ls_sort-spos.
ls_sort-fieldname = &1.
ls_sort-up = 'X'.
ls_sort-subtot = &2.
ls_sort-expa = &3.
append ls_sort to lt_sort.
END-OF-DEFINITION.
DATA:
ls_fieldcat TYPE slis_fieldcat_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
lt_sort TYPE slis_t_sortinfo_alv,
ls_sort TYPE slis_sortinfo_alv,
ls_layout TYPE slis_layout_alv,
ls_title TYPE slis_layout_alv_spec1,
lt_sp_group TYPE slis_t_sp_group_alv,
ls_col TYPE slis_layout_alv_spec,
ls_sp_group TYPE slis_sp_group_alv.
m_fieldcat 'MATNR' '' '' 'Material Number' .
m_fieldcat 'MEINS' '' '' 'Base Unit of Measure' .
m_sort 'MATNR' 'X' ' ' .
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-totals_text = 'Totals'(256).
gd_layout-coltab_fieldname = 'CELL_COLOUR'.
* perform build_events.
gd_prntparams-reserve_lines = '4'. "Lines reserved for footer
gd_prntparams-no_coverpage = 'X'.
gd_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = gd_repid
is_layout = gd_layout
it_fieldcat = lt_fieldcat[]
i_grid_title = 'Materials....'
* i_save = 'A'
it_sort = lt_sort
it_events = gt_events
is_print = gd_prntparams
TABLES
t_outtab = itabrec
EXCEPTIONS
program_error = 1
OTHERS = 2.
endform. " showALV
**** make sure that you have typed the field names in capital letters , when you defined the field catalogue and sorting catalogue...
